Skip to content

Python: fix(ollama): send the tool name on tool result messages - #8815

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
anishmehta24:fix/ollama-tool-result-name
Sep 29, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 2 commits into
microsoft:mainfrom
anishmehta24:fix/ollama-tool-result-name

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Motivation & Context

OllamaChatClient._format_tool_message sets tool_name from getattr(item, "name", ""), but function result content has no name (Content.from_function_result only takes the call_id). So in a normal tool-calling run every tool message goes to Ollama with an empty tool name:

msgs = [
    Message(role="user", contents=[Content.from_text("weather?")]),
    Message(role="assistant", contents=[Content.from_function_call(call_id="c1", name="get_weather", arguments={"city": "Paris"})]),
    Message(role="tool", contents=[Content.from_function_result(call_id="c1", result="sunny")]),
]
OllamaChatClient(model="m")._prepare_messages_for_ollama(msgs)[-1]
# role='tool' content='sunny' ... tool_name=''

Ollama's chat API has no tool call ids, so tool_name is the only thing tying a result back to the tool that produced it, and the model templates use it when rendering tool results.

Description & Review Guide

  • What are the major changes? _prepare_messages_for_ollama builds a call_id -> name map from the function calls in the conversation and passes it to _format_tool_message, which uses it when the result item has no name of its own.
  • What is the impact of these changes? Tool messages sent to Ollama carry the name of the tool that was called.
  • What do you want reviewers to focus on? Whether looking the name up across the whole message list is fine here, versus threading it some other way.

Related Issue

No existing issue.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Test: test_tool_message_gets_name_from_matching_function_call in packages/ollama/tests/test_ollama_chat_client.py; it fails on main. The ollama tests pass (38 passed, 5 skipped), and ruff check/format and pyright are clean.

Function results don't carry the tool name, so every tool message was
sent to Ollama with tool_name=''. Look the name up from the function
call with the same call_id.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 29, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 502f7d5ddb44
Model: gpt-5.6-sol-fast

Overview

The PR correctly derives Ollama's required tool name from function-call history, preserves result ordering, and adds coverage for reordered results with distinct IDs. Fresh UUIDs mitigate the issue for ordinary Ollama-originated calls, but the conversation-global lookup breaks valid replayed or caller-supplied histories when a call_id is reused across completed occurrences. That residual correlation defect can send historical tool output under the wrong tool identity.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/ollama/agent_framework_ollama/_chat_client.py

Comment thread python/packages/ollama/agent_framework_ollama/_chat_client.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Anish Mehta (@anishmehta24) Thanks — the reused call_id occurrence fix and regression address the prior review feedback, the dedicated review found no additional issues, and all reported checks are green.

Merged via the queue into microsoft:main with commit 8258db8 Sep 29, 2026
44 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — d337592a Deployed Sep 29, 2026 by anishmehta24 via add_label #23948
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants