Skip to content

fix(ollama): keep provider-assigned tool call ids in streamed tool calls - #46587

Open
choksi2212 wants to merge 6 commits into
BerriAI:mainfrom
choksi2212:fix/ollama-stream-tool-call-ids-46543
Open

choksi2212 wants to merge 6 commits into
BerriAI:mainfrom
choksi2212:fix/ollama-stream-tool-call-ids-46543

Conversation

@choksi2212

@choksi2212 choksi2212 commented Oct 15, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • ollama_chat/ streaming replaced the tool call ids Ollama sends with freshly generated uuids
  • callers matching tool results by id, or logging ids, saw ids the provider never sent

How 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

  1. They run 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)
  2. Ollama's stream tags each parallel tool call with an id, e.g. call_8uaqob2r and call_nyjtbrz4
  3. The streamed deltas instead carry fresh uuids like 434713cc-9e35-486e-b97e-27cb063478e9, different on every run
  4. Their executor matches tool results to calls by id, so results never line up with the ids the model referenced, and logs show ids Ollama never sent

After: the same request surfaces the ids Ollama sent

  1. Same litellm.completion call with stream=True and tools against local Ollama
  2. Deltas for the two parallel calls carry call_8uaqob2r and call_nyjtbrz4 exactly as Ollama sent them
  3. Tool results matched back by id land on the right call, and ids in logs agree with the provider

Relevant issues

Fixes #46543

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally: 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)
  • My PR passes all required CI/CD checks: verified against the actual gate commands locally - ruff check on the changed litellm/ file passes, ruff check --config ruff-tests.toml tests passes on the changed test file, and the diff is format-clean for litellm/**. 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 on chunk_parser, one SIM102 over the merge-base ceilings), folded into a single or-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 sanctioned mutable-ok suppression with reason (f9cf78c). The file's strict and ruff counts now equal the merge-base baseline
  • My PR's scope is as isolated as possible: one behavior (the id overwrite), the delta-index quirk ([Bug]: ollama_chat streaming emits index=0 for every tool call, merging parallel tool calls into malformed JSON #33678) is deliberately untouched
  • Automated review findings will be addressed as they register (first PR from this fork, workflows may need the one-time approval)

Screenshots / Proof of Fix

Setup shared by both runs: the replay feeds the /api/chat response lines the issue records from a real Ollama v0.40.3 + qwen3.5:0.8b session (fixture published by the reporter at kartsan03/wiretruth, suite/fixtures/ollama/tools.parallel-calls-stream.json: two parallel get_weather calls with ids call_8uaqob2r / call_nyjtbrz4, then the done chunk) line by line through OllamaChatCompletionResponseIterator.chunk_parser and 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 test test_streaming_preserves_provider_tool_call_ids embeds the same chunks.

Before (5088b0c)

  1. Replay the recorded lines through the iterator:
    LiteLLM emitted:
      id='35e782cd-fe4b-4ecf-b56f-8a056c36d68b' name='get_weather'
      id='085bc820-e0ee-4095-8d2c-59067af51b8d' name='get_weather'
    VERDICT: FAIL - provider ids REPLACED (bug reproduced)
    
  2. pytest tests/unit/llms/ollama/test_ollama_chat_transformation.py -k ToolCallIds -> test_streaming_preserves_provider_tool_call_ids FAILS with Expected Ollama's id 'call_8uaqob2r' to survive the stream, got '7791c425-4233-41db-8fc9-60c63d45ab73'; the two fallback-guard tests pass

After (80a0cd6)

  1. Same replay of the same recorded lines:
    LiteLLM emitted:
      id='call_8uaqob2r' name='get_weather'
      id='call_nyjtbrz4' name='get_weather'
    VERDICT: PASS - provider ids preserved
    
  2. pytest tests/unit/llms/ollama/test_ollama_chat_transformation.py -k ToolCallIds -> 3 passed; full tests/unit/llms/ollama/ -> 122 passed

Type

🐛 Bug Fix

Caveats

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

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>
@greptile-apps

greptile-apps Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Automatic reviews are disabled for this repository.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing choksi2212:fix/ollama-stream-tool-call-ids-46543 (ea69576) with main (a2f7926)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (71e508b) during the generation of this report, so a2f7926 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

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.

This branch has not been deployed

No deployments
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.

ollama_chat streaming replaces the tool call ids Ollama sends with random UUIDs

2 participants