fix(ollama): forward tool_calls and tool_call_id in transform_request - #26122
Conversation
e2efe96 to
fc3a88d
Compare
Greptile SummaryThis PR fixes two bugs in the Ollama chat transformation: Confidence Score: 5/5Safe to merge — targeted bug fix with full regression test coverage and no side effects. The change is minimal and surgical: it corrects two clear omissions (missing tool_calls assignment and dropped tool_call_id) and removes an existing no-op mutation. Both fixes are covered by new unit tests. No security concerns, no backwards-incompatible changes, and no modifications to mocks that could mask regressions. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/ollama/chat/transformation.py | Correctly assigns translated tool_calls to ollama_message and forwards tool_call_id; removed the prior no-op in-place mutation of m. |
| litellm/types/llms/ollama.py | Adds tool_call_id field to OllamaChatCompletionMessage TypedDict; minimal, correct change. |
| tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py | Adds two regression tests for the fixed bugs; both are unit tests with no real network calls, following repository conventions. |
Sequence Diagram
sequenceDiagram
participant Client
participant LiteLLM as LiteLLM (transform_request)
participant Ollama
Client->>LiteLLM: messages=[user, assistant(tool_calls), tool(tool_call_id)]
Note over LiteLLM: Translate tool_calls → OllamaToolCall format
Note over LiteLLM: Assign new_tools to ollama_message["tool_calls"] ✅ (was missing)
Note over LiteLLM: Forward tool_call_id to ollama_message ✅ (was dropped)
LiteLLM->>Ollama: {role:assistant, tool_calls:[…]}, {role:tool, tool_call_id:"…"}
Ollama-->>LiteLLM: Response (no re-issued duplicate call)
LiteLLM-->>Client: Final answer
Reviews (3): Last reviewed commit: "fix(ollama): forward tool_calls and tool..." | Re-trigger Greptile
fc3a88d to
619c0cb
Compare
|
Tested with LiteLLM v1.83.7.rc.1 and Ollama 0.21.2 - problem is still visible |
|
@roxlukas My fix is not in https://github.com/BerriAI/litellm/releases/tag/v1.83.7.rc.1 do you mean that you applied my PR on top of that tag and it still is not working? I just tested it again:
Ollama has no record a tool was ever called. The loop is inevitable.
Identical call, but with the tools showing. If you did apply my PR on top of that branch, tell me more about what you are seeing and I can try to reproduce. I also rebased it. Thanks |
619c0cb to
6bef441
Compare
tool_calls on assistant messages were translated to OllamaToolCall format
but never copied into the outgoing OllamaChatCompletionMessage, so Ollama
received {role: assistant, content: ''} with no tool_calls. The model
then had no record of having made a tool call, causing it to re-issue
the identical call on every turn (infinite loop).
Similarly, tool_call_id on role:tool messages was silently dropped.
Ollama uses this field to resolve the tool name from conversation history.
Also add tool_call_id to OllamaChatCompletionMessage TypedDict.
Fixes BerriAI#26094
6bef441 to
6eafefe
Compare
Low: Bug fix for Ollama tool call forwardingThis PR fixes a bug where Status: 0 open Posted by Veria AI · 2026-04-24T22:43:52.062Z |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
cd88dde
into
BerriAI:litellm_oss_branch
…#26122) tool_calls on assistant messages were translated to OllamaToolCall format but never copied into the outgoing OllamaChatCompletionMessage, so Ollama received {role: assistant, content: ''} with no tool_calls. The model then had no record of having made a tool call, causing it to re-issue the identical call on every turn (infinite loop). Similarly, tool_call_id on role:tool messages was silently dropped. Ollama uses this field to resolve the tool name from conversation history. Also add tool_call_id to OllamaChatCompletionMessage TypedDict. Fixes #26094
…BerriAI#26122) tool_calls on assistant messages were translated to OllamaToolCall format but never copied into the outgoing OllamaChatCompletionMessage, so Ollama received {role: assistant, content: ''} with no tool_calls. The model then had no record of having made a tool call, causing it to re-issue the identical call on every turn (infinite loop). Similarly, tool_call_id on role:tool messages was silently dropped. Ollama uses this field to resolve the tool name from conversation history. Also add tool_call_id to OllamaChatCompletionMessage TypedDict. Fixes BerriAI#26094
Summary
transform_requesttranslatedtool_callson assistant messages toOllamaToolCallformat but never copied them into the outgoingOllamaChatCompletionMessage— Ollama received{role: assistant, content: ''}with notool_callstool_call_idonrole: toolmessages was also silently dropped; Ollama uses this field to resolve the tool function name from conversation historytool_call_idtoOllamaChatCompletionMessageTypedDictFixes #26094 (reported via ollama/ollama#15719)
Test plan
TestOllamaToolCallTransformation::test_transform_request_preserves_tool_calls— assertstool_callssurvive the transform on assistant messagesTestOllamaToolCallTransformation::test_transform_request_forwards_tool_call_id— assertstool_call_idis forwarded on tool response messagestest_ollama_chat_transformation.pysuite: 24/24 pass