fix(ollama): sanitize null content on every message shape

gemma thinking-only assistant turns carry content: null with no
tool_calls, slipping past the tool-call-only sanitizer and 400ing the
whole agent run ('invalid message content type: <nil>'). Null content is
now blanked for any role.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-14 15:37:00 +02:00
co-authored by Claude Fable 5
parent fccbe65ecf
commit 9184b48673
+12 -8
View File
@@ -113,14 +113,18 @@ def _sanitize_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
for msg in messages:
msg_copy = dict(msg)
# Fix null content in assistant messages with tool calls
if msg_copy.get("role") == "assistant":
if msg_copy.get("content") is None and msg_copy.get("tool_calls"):
msg_copy["content"] = ""
logger.debug(
"sanitized_null_content",
tool_call_count=len(msg_copy["tool_calls"]),
)
# Fix null content in ANY message: Ollama rejects content: null with
# "invalid message content type: <nil>". The tool-call-only assistant
# case is the common one, but gemma thinking-only turns produce
# assistant messages with null content and NO tool_calls, which
# previously slipped through and 400'd the whole agent run.
if "content" in msg_copy and msg_copy.get("content") is None:
msg_copy["content"] = ""
logger.debug(
"sanitized_null_content",
role=msg_copy.get("role"),
tool_call_count=len(msg_copy.get("tool_calls") or []),
)
sanitized.append(msg_copy)