From 9184b486730736a57432b3de4f3148e56dd4276f Mon Sep 17 00:00:00 2001 From: Jeroen Schweitzer Date: Tue, 14 Jul 2026 15:37:00 +0200 Subject: [PATCH] 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: '). Null content is now blanked for any role. Co-Authored-By: Claude Fable 5 --- src/ollama/provider.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ollama/provider.py b/src/ollama/provider.py index d93485b..1ec7fc1 100644 --- a/src/ollama/provider.py +++ b/src/ollama/provider.py @@ -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: ". 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)