Skip to content

fix: preserve Ollama streaming tool calls#1948

Open
andreivince wants to merge 1 commit into
kagent-dev:mainfrom
andreivince:fix/ollama-streaming-tool-calls
Open

fix: preserve Ollama streaming tool calls#1948
andreivince wants to merge 1 commit into
kagent-dev:mainfrom
andreivince:fix/ollama-streaming-tool-calls

Conversation

@andreivince
Copy link
Copy Markdown

Closes #1922.

Ollama can emit tool_calls before the final done=true chunk when streaming. The adapter was only reading chunk.message.tool_calls from the final chunk, so a real streamed tool call could turn into an empty final ADK response.

I changed the streaming path to accumulate tool calls across chunks, then include them when building the final LlmResponse. I also moved the Ollama tool-call to ADK part conversion into one helper so streaming and non-streaming stay consistent.

I verified this two ways:

  • Added a regression test where the tool call arrives on a non-final chunk and the done chunk has no tool calls. This failed before the fix and passes after it.
  • Reproduced against a live local Ollama server with qwen3:0.6b. Raw Ollama emitted get_temperature({"city": "Tokyo"}) on done=false, then a final done=true chunk with no tool calls. Before the fix, kagent returned function_calls=0; after the fix, kagent returned the expected get_temperature function call.

Checks run:

  • uv run pytest packages/kagent-adk/tests/unittests/models/test_ollama.py -q
  • uv run pytest packages/kagent-adk/tests/unittests/models --ignore=packages/kagent-adk/tests/unittests/models/test_tls_e2e.py -q
  • uv run ruff check packages/kagent-adk/src/kagent/adk/models/_ollama.py packages/kagent-adk/tests/unittests/models/test_ollama.py

One note: the full model test directory has unrelated local TLS E2E failures because the cert fixture files are missing in my checkout. The non-TLS model tests passed.

Copilot AI review requested due to automatic review settings May 31, 2026 04:01
@github-actions github-actions Bot added the bug Something isn't working label May 31, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes a bug in the Ollama streaming path where tool calls emitted in non-final chunks were dropped because only the final (done) chunk's tool_calls were inspected. Now tool calls are accumulated across all streamed chunks before being added to the final response.

Changes:

  • Extract the tool-call → types.Part conversion into a helper _convert_tool_call_to_part.
  • Accumulate tool_calls across all streamed chunks and append them on the done chunk.
  • Add a unit test covering tool-call accumulation when the tool call arrives before the done chunk.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/kagent-adk/src/kagent/adk/models/_ollama.py Refactors tool-call conversion into a helper and fixes streaming to aggregate tool calls across chunks.
python/packages/kagent-adk/tests/unittests/models/test_ollama.py Adds a streaming test verifying tool calls from earlier chunks are included in the final response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@EItanya
Copy link
Copy Markdown
Contributor

EItanya commented Jun 1, 2026

Thanks so much for the contribution, you will need to sign your commits to pass DCO before we can merge

Copy link
Copy Markdown
Contributor

@jmhbh jmhbh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm after commits are signed.

Signed-off-by: andreivince <andreivince21@gmail.com>
@andreivince andreivince force-pushed the fix/ollama-streaming-tool-calls branch from 708d9fc to 740d320 Compare June 1, 2026 21:36
@andreivince
Copy link
Copy Markdown
Author

Thanks @EItanya, signed the commit and force-pushed the clean branch. @jmhbh DCO is green now too.

@andreivince andreivince requested a review from jmhbh June 1, 2026 22:30
@andreivince
Copy link
Copy Markdown
Author

Looks like the remaining build (app) failure is a CI flake. It fails in the arm64 Docker base layer with a QEMU illegal-instruction error before kagent source is copied in.

This PR only touches the Ollama adapter and its unit test, and the Python lint/tests are green. Could someone rerun the failed build jobs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Ollama streaming adapter drops tool_calls emitted before the done chunk

4 participants