Skip to content

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

Closed
Anoshor wants to merge 1 commit into
genkit-ai:mainfrom
Anoshor:fix/py-ollama-tool-requests
Closed

Anoshor wants to merge 1 commit into
genkit-ai:mainfrom
Anoshor:fix/py-ollama-tool-requests

Conversation

@Anoshor

@Anoshor Anoshor commented Nov 20, 2026

Copy link
Copy Markdown

Fixes #7277

OllamaModel.build_chat_messages in genkit-ollama ignored tool_request parts and folded every tool_response into one message without a tool_name. The assistant turn that asked for a tool reached Ollama with no tool_calls, so the model saw a tool result for a call it never made, called the tool again, and the generate loop ended with Exceeded maximum tool call iterations. Any tool use through the Python Ollama plugin was affected.

This makes the Python plugin match the JS Ollama plugin (js/plugins/ollama/src/index.ts):

  • A tool_request part becomes an entry in the message's tool_calls (function.name, function.arguments). Non-object input is sent as {}.
  • Each tool_response part becomes its own tool message with tool_name set. String output is sent as is, other output as JSON (previously str(output), a Python repr).
  • Text and images are unchanged.

Testing:

  • Added TestBuildChatMessagesWithTools: a user, tool-request, tool-response round trip, and two parallel tool calls answered in one tool message. Both fail on main and pass here.
  • uv run pytest packages/genkit-ollama/tests: 250 passed.
  • ruff check --preview, ruff format --preview --check and ty check pass on the changed files.

@huangjeff5 @pavelgj could you take a look?

Checklist:

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#7277

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the build_chat_messages method in the Genkit Ollama provider to support tool requests and tool responses, and adds corresponding unit tests. The review feedback suggests using attribute access (item.images) instead of dictionary access (item['images']) for consistency and compatibility.

)
elif text_part.media is not None:
image_value = await cls._resolve_image(text_part.media.url)
item['images'].append(ollama_api.Image(value=image_value))

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.

medium

For consistency with the rest of the method (such as item.content on line 759 and item.tool_calls on line 774), and to avoid potential compatibility issues if ollama_api.Message is a Pydantic model or class that does not support dictionary-like subscription, use attribute access (item.images) instead of dictionary access (item['images']).

Suggested change
item['images'].append(ollama_api.Image(value=image_value))
item.images.append(ollama_api.Image(value=image_value))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. I tried this, but ty check fails on it: Message.images is typed Sequence[Image] | None, which has no append, so item.images.append(...) is an unresolved-attribute error. The item['images'] line is unchanged from main (the method already used it for images), so I've left it as is to keep this PR to the tool-call fix.

@cabljac

cabljac commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for tracking this down and for the clear write-up and tests! The Python SDK has moved to genkit-ai/genkit-python, so @hilariie has carried your commit over in genkit-ai/genkit-python#169 (your authorship is kept) with a couple of small additions on top. We'll continue there, so I'm closing this one. Thanks again!

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

Labels

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

2 participants