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)