Skip to content

fix(ollama): forward tool_calls and tool_call_id in transform_request - #26122

Merged
krrish-berri-2 merged 1 commit into
BerriAI:litellm_oss_branchfrom
mverrilli:fix/ollama-tool-calls-dropped-in-transform
Apr 25, 2026
Merged

fix(ollama): forward tool_calls and tool_call_id in transform_request#26122
krrish-berri-2 merged 1 commit into
BerriAI:litellm_oss_branchfrom
mverrilli:fix/ollama-tool-calls-dropped-in-transform

Conversation

@mverrilli

Copy link
Copy Markdown
Contributor

Summary

  • transform_request translated tool_calls on assistant messages to OllamaToolCall format but never copied them into the outgoing OllamaChatCompletionMessage — Ollama received {role: assistant, content: ''} with no tool_calls
  • The model had no record of having made a tool call and re-issued the same call on every turn, causing an infinite loop
  • tool_call_id on role: tool messages was also silently dropped; Ollama uses this field to resolve the tool function name from conversation history
  • Added tool_call_id to OllamaChatCompletionMessage TypedDict

Fixes #26094 (reported via ollama/ollama#15719)

Test plan

  • TestOllamaToolCallTransformation::test_transform_request_preserves_tool_calls — asserts tool_calls survive the transform on assistant messages
  • TestOllamaToolCallTransformation::test_transform_request_forwards_tool_call_id — asserts tool_call_id is forwarded on tool response messages
  • Full test_ollama_chat_transformation.py suite: 24/24 pass

@CLAassistant

CLAassistant commented Apr 20, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@mverrilli
mverrilli force-pushed the fix/ollama-tool-calls-dropped-in-transform branch from e2efe96 to fc3a88d Compare April 20, 2026 17:48
@greptile-apps

greptile-apps Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes two bugs in the Ollama chat transformation: tool_calls on assistant messages were translated to OllamaToolCall format but never written into the outgoing OllamaChatCompletionMessage, and tool_call_id on role: tool messages was silently dropped. Both fields are now correctly forwarded and OllamaChatCompletionMessage TypedDict has been extended with tool_call_id. Tests cover both regression cases and make no real network calls.

Confidence Score: 5/5

Safe 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.

Important Files Changed

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
Loading

Reviews (3): Last reviewed commit: "fix(ollama): forward tool_calls and tool..." | Re-trigger Greptile

@mverrilli
mverrilli force-pushed the fix/ollama-tool-calls-dropped-in-transform branch from fc3a88d to 619c0cb Compare April 20, 2026 17:52
@roxlukas

Copy link
Copy Markdown

Tested with LiteLLM v1.83.7.rc.1 and Ollama 0.21.2 - problem is still visible

@mverrilli

mverrilli commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

@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:

  • Pre-fix payload (v1.83.7.rc.1, what you tested) is demonstrably broken:
{"role":"assistant","content":"","images":[]}          ← tool_calls GONE
{"role":"tool","content":"...","images":[]}           ← tool_call_id GONE

Ollama has no record a tool was ever called. The loop is inevitable.

  • Transform output matches Ollama's native format (post-fix):
{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_weather","arguments":{"city":"Warsaw"}}}]}
{"role":"tool","content":"...","tool_call_id":"call_abc"}

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

@mverrilli
mverrilli force-pushed the fix/ollama-tool-calls-dropped-in-transform branch from 619c0cb to 6bef441 Compare April 24, 2026 22:41
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
@mverrilli
mverrilli force-pushed the fix/ollama-tool-calls-dropped-in-transform branch from 6bef441 to 6eafefe Compare April 24, 2026 22:42
@veria-ai

veria-ai Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Low: Bug fix for Ollama tool call forwarding

This PR fixes a bug where tool_calls and tool_call_id fields were being dropped during request transformation for the Ollama provider. The changes are mechanical — values are passed through typed constructors into a TypedDict that gets JSON-serialized for the Ollama API request. No security concerns.


Status: 0 open
Risk: 1/10

Posted by Veria AI · 2026-04-24T22:43:52.062Z

@codecov

codecov Bot commented Apr 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@krrish-berri-2
krrish-berri-2 merged commit cd88dde into BerriAI:litellm_oss_branch Apr 25, 2026
43 of 44 checks passed
@mverrilli
mverrilli deleted the fix/ollama-tool-calls-dropped-in-transform branch April 25, 2026 21:26
Sameerlite pushed a commit that referenced this pull request Apr 27, 2026
…#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
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants