Repository navigation
fix(ollama): keep provider-assigned tool call ids in streamed tool calls - #46587
choksi2212 wants to merge 6 commits into
Conversation
Ollama's /api/chat stream carries an id on every tool call. The ollama_chat streaming chunk parser overwrote it with a freshly generated uuid whenever the function arguments looked complete, so callers matching tool results by id saw ids the provider never sent. Keep the provider-assigned id when one is present; mint a uuid only when it is missing or empty, which preserves the existing fallback for streams without ids (BerriAI#11262). Fixes BerriAI#46543 Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
|
Automatic reviews are disabled for this repository. |
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…udget The nested guard added one C901 to chunk_parser (15 -> 16) and one SIM102, which the strict-rule delta gate counts against the merge-base ceilings. Fold the fallback into a single or-expression instead: the provider id is kept when truthy and a uuid is minted only when it is missing or empty, identical behavior with no new branch. Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
Reading the provider id through an untyped dict member access added one reportUnknownMemberType over the merge-base ceiling. Route the read through an explicitly typed alias instead; behavior is unchanged. Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
…line gate The typed alias annotation is one new mutable-collection annotation (LIT001) over the merge-base ceiling. Mark it with the sanctioned mutable-ok suppression and reason; code is unchanged. Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
The basedpyright delta gate counts one new reportUnknownVariableType and one new reportUnknownMemberType for any read of the untyped provider tool-call dicts, and the line-length budget leaves no room for suppression comments on a single-line read. Route the read through a Mapping view instead: the alias assignment and the member read each carry the repo's sanctioned pyright ignore with a reason, the write stays on the tool_call subscript like the BerriAI#11262 line it replaces, and no mutable-collection annotation is introduced. Behavior is unchanged. Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
…l-call-ids-46543 Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com> # Conflicts: # tests/unit/llms/ollama/test_ollama_chat_transformation.py
|
Hi @charan-rathore — gentle nudge on this one when you have a moment. It keeps provider-assigned tool call ids in streamed Ollama tool calls (closes #46543: ids were being overwritten with generated uuids mid-stream, breaking downstream id-based aggregation). The full check matrix is green at ea69576 and the negative control (reporter's Ollama v0.40.3 stream replayed through the chunk parser) is documented in the PR body. |
TLDR
Problem this solves:
ollama_chat/streaming replaced the tool call ids Ollama sends with freshly generated uuidsHow it solves it:
User Flow
Before: a developer streaming tool calls from a local Ollama model gets tool call ids the model never sent, so id-based tool-result routing breaks
litellm.completion(model="ollama_chat/qwen3.5:0.8b", api_base="http://127.0.0.1:11434", messages=[...], tools=[...], stream=True)against their local Ollama (v0.40.3)call_8uaqob2randcall_nyjtbrz4434713cc-9e35-486e-b97e-27cb063478e9, different on every runAfter: the same request surfaces the ids Ollama sent
litellm.completioncall withstream=Trueandtoolsagainst local Ollamacall_8uaqob2randcall_nyjtbrz4exactly as Ollama sent themRelevant issues
Fixes #46543
Pre-Submission checklist
python -m pytest tests/unit/llms/ollama/ -q-> 122 passed (35 pre-existing in the touched file + 3 new), after installing the project's dev-group test deps (respx,pytest-socket,pillow, warm tiktoken cache)ruff checkon the changedlitellm/file passes,ruff check --config ruff-tests.toml testspasses on the changed test file, and the diff is format-clean forlitellm/**. The delta gates have flagged this branch twice and both are fixed in-branch: the strict-rule gate caught the first commit's nested guard (one C901 onchunk_parser, one SIM102 over the merge-base ceilings), folded into a singleor-expression in bcb0ba9; the basedpyright gate then caught the untyped id read (one reportUnknownMemberType), routed through a typed alias (f0d1761) whose one new mutable-collection annotation then carries the sanctionedmutable-oksuppression with reason (f9cf78c). The file's strict and ruff counts now equal the merge-base baselineScreenshots / Proof of Fix
Setup shared by both runs: the replay feeds the
/api/chatresponse lines the issue records from a real Ollama v0.40.3 + qwen3.5:0.8b session (fixture published by the reporter atkartsan03/wiretruth,suite/fixtures/ollama/tools.parallel-calls-stream.json: two parallelget_weathercalls with idscall_8uaqob2r/call_nyjtbrz4, then thedonechunk) line by line throughOllamaChatCompletionResponseIterator.chunk_parserand prints the emitted delta tool call ids. The model mints new ids on every generation, so the recorded stream is the deterministic form of this exact response; the unit testtest_streaming_preserves_provider_tool_call_idsembeds the same chunks.Before (5088b0c)
pytest tests/unit/llms/ollama/test_ollama_chat_transformation.py -k ToolCallIds->test_streaming_preserves_provider_tool_call_idsFAILS withExpected Ollama's id 'call_8uaqob2r' to survive the stream, got '7791c425-4233-41db-8fc9-60c63d45ab73'; the two fallback-guard tests passAfter (80a0cd6)
pytest tests/unit/llms/ollama/test_ollama_chat_transformation.py -k ToolCallIds-> 3 passed; fulltests/unit/llms/ollama/-> 122 passedType
🐛 Bug Fix
Caveats
Low
Final Attestation