AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ChatOllama Omits Tool_name In Tool Messages, Causing Tool Results To Be Labeled As Unknown

When using ChatOllama with tool calling, ToolMessages are sent without the required tool_name field, so Ollama renders them as coming from an unknown tool. This breaks tool result matching for models that rely on the name, like Gemma 4.

highConfidence 92%Langchain-OllamaAffected V1.1.0

Origin Analysis

The `_convert_messages_to_ollama_messages` function in langchain-ollama includes a `tool_call_id` field on tool messages and an `id` field on assistant tool calls, but the `ollama` Python client's `Message` model does not declare these fields and silently drops them. The Ollama API expects a `tool_name` field on tool messages, which LangChain never populates. This is a design mismatch between LangChain's internal representation and the Ollama client's schema.
1. Install langchain-ollama and ollama environment. 2. Create a ChatOllama instance with a mock transport that captures the request. 3. Invoke the model with a HumanMessage, an AIMessage containing tool calls (with names and ids), and corresponding ToolMessages that have tool_call_id and name. 4. Inspect the captured request messages with role 'tool'; they lack the 'tool_name' field, causing Ollama to assign 'unknown'.

Fixing Code Block

Edge Case Audit

If a ToolMessage has neither a name nor a corresponding assistant tool call (e.g., hand-built without matching id), the tool_name will be omitted, potentially still resulting in 'unknown'. Additionally, this fix removes the 'id' field from assistant tool calls; while Ollama does not require it, some models or future versions might expect an id for correlation, which could break parallel tool call matching. Rollback: revert to the original `_convert_messages_to_ollama_messages` that included tool_call_id and id fields. Test in environments where tool messages may lack names and with models that rely on id-based matching. Ensure the Ollama client version remains within 0.6.x range; upgrading to newer clients that add id support may require revisiting this fix.

Ecosystem Topology