Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions libs/partners/ollama/langchain_ollama/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def _parse_json_string(
OutputParserException: If the string is invalid and ``skip=False``.

"""
# Handle empty strings gracefully
if not json_string.strip():
if skip:
return json_string
# Return empty dict for empty strings in tool calls
return {}

try:
return json.loads(json_string)
except json.JSONDecodeError:
Expand Down Expand Up @@ -939,6 +946,21 @@ def _iterate_over_stream(
)
continue

# Handle structured output scenarios where content might be empty
# but tool_calls are present
has_tool_calls = "message" in stream_resp and stream_resp[
"message"
].get("tool_calls")

# Skip empty content chunks unless they have tool
# calls or are final chunks
if (
not content.strip()
and not has_tool_calls
and not stream_resp.get("done")
):
continue

if stream_resp.get("done") is True:
generation_info = dict(stream_resp)
if "model" in generation_info:
Expand Down
Loading
Loading