fix(ollama): preserve tool call IDs to fix repeated same-tool calls#3321
Conversation
351b59d to
7c71d37
Compare
Coverage report (falkordb)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Coverage report (ollama)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
Quick reproduction showing the bug and the fix Before two streaming calls to the same tool, one silently lost: After both calls preserved with distinct IDs: The root cause was that |
|
Please remove all changes unrelated to the PR. So all changes in falkdordb should be removed. |
7c71d37 to
af73962
Compare
|
@SyedShahmeerAli12 could you add information about when Ollama started returning tool call IDs (e.g. which version of ollama is this supported?). Also were you able to reproduce the bug with a live ollama server and not just through the unit tests? |
|
Ollama version: Tool call IDs were added in v0.12.10 (released Nov 5, 2025) the release notes explicitly say "Ollama will now return tool call IDs from the Live reproduction: Yes, tested with Ollama v0.24.0. The raw HTTP response confirms IDs are present: [
{"id": "call_lyywui55", "function": {"name": "get_weather", "arguments": {"city": "Paris"}}},
{"id": "call_0scw2dos", "function": {"name": "get_weather", "arguments": {"city": "London"}}}
]However, the Ollama Python SDK (0.6.1) silently drops the I'll address the inline comments about synthetic IDs and update the PR accordingly. |
|
@SyedShahmeerAli12 do you know if there is an open issue for the SDK to add the tool call ID support? |
|
No open issue found in ollama/ollama-python for this. The SDK's Also addressed the inline comments removed synthetic ID generation. The streaming accumulator now keys by |
Hey @SyedShahmeerAli12 if this is the case that the ollama python SDK ignores the tool call ID then your changes here won't have any effect right? The tool call ID will always be |
|
The PR fixes two things:
So yes, IDs are always |
43e8535 to
a3f6035
Compare
|
@SyedShahmeerAli12 looking good and thanks for the additional tests! Last comment would be to add async versions of the unit test |
|
Added |
Related Issues
Proposed Changes:
OllamaChatGeneratorwas silently dropping tool call IDs, which broke any scenario where the same tool is called more than once in a single assistant turn. In the streaming accumulator, both calls mapped to the same key (the tool name since ID wasNone), so the second call overwrote the first and it was lost.Fix: generate a synthetic ID (
call_N) for each tool call in both the streaming and non-streaming paths, and propagate it to the finalToolCallobjects.ollama_tc.get("id")is checked first so the code will use server-assigned IDs automatically if a future Ollama SDK version exposes them.Affected locations:
_convert_ollama_response_to_chatmessagenon-streaming path_build_chunkstreaming chunk builder_handle_streaming_response+_handle_streaming_response_asyncstreaming reconstructionHow did you test it?
weatherwith different cities only the second call survived before the fixtest_convert_ollama_response_to_chatmessage_with_repeated_tooltest_handle_streaming_response_repeated_tool_callsid=NoneonToolCallDeltaandToolCallobjectsNotes for the reviewer
The Ollama Python SDK (0.6.1) drops the
idfield from the server JSON response during Pydantic parsingMessage.ToolCallhas noidfield defined. Synthetic IDs (call_0,call_1, …) are the only option today. Theget("id")fallback is a forward-compatible hook for when the SDK adds the field.Checklist
fix: