Skip to content

fix(py/plugins/ollama): send tool calls and tool names in chat messages - #169

Merged
hilariie merged 3 commits into
mainfrom
fix/ollama-tool-requests
Nov 23, 2026
Merged

hilariie merged 3 commits into
mainfrom
fix/ollama-tool-requests

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

Moved from genkit-ai/genkit#7279. Fixes genkit-ai/genkit#7277.

A tool loop on an Ollama model now finishes. Before this change, the assistant turn that asked for a tool went back to Ollama with no tool_calls, and the tool results went back with no tool_name. The model then saw an answer to a call it never made, so it called the tool again on every turn until it hit the cap.

response = await ai.generate(prompt='What time is it?', tools=['getCurrentTime'], max_turns=5)
# before: getCurrentTime is called on every turn
response.finish_reason   # 'aborted'
response.finish_message  # 'Exceeded maximum tool call iterations (5)'
# after: one tool call, then the answer
response.text            # 'It is 18:00.'

Changes

  • Tool request parts are sent as the assistant message's tool_calls. Non-object tool input is sent as {}, because Ollama's arguments must be an object.
  • Each tool response is sent as its own tool message with tool_name, instead of being merged into one unnamed message. Non-string output is sent as JSON, and non-ASCII characters are kept as they are rather than escaped.

Breaking changes

None.

Anoshor and others added 2 commits November 23, 2026 11:27
build_chat_messages ignored tool_request parts, so the assistant turn that
asked for a tool reached Ollama with no tool_calls, and folded every tool
response into one message with no tool_name. The model then saw a tool
result for a call it never made and called the tool again until max_turns.

Send tool requests as tool_calls, and each tool response as its own tool
message with tool_name, encoding non-string output as JSON, as the JS
Ollama plugin does.

Fixes genkit-ai/genkit#7277

Upstream-Commit: genkit-ai/genkit@a60c918
Upstream-PR: genkit-ai/genkit#7279

@cabljac cabljac 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.

LGTM with nits

@hilariie
hilariie merged commit 1f24d1a into main Nov 23, 2026
20 checks passed
@hilariie
hilariie deleted the fix/ollama-tool-requests branch November 23, 2026 13:36
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 2026
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.

[PY] genkit-ollama drops tool requests from chat messages, so tool calls loop until max_turns

3 participants