Repository navigation
Conversation
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
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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']).
| item['images'].append(ollama_api.Image(value=image_value)) | |
| item.images.append(ollama_api.Image(value=image_value)) |
There was a problem hiding this comment.
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.
|
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! |
Fixes #7277
OllamaModel.build_chat_messagesingenkit-ollamaignoredtool_requestparts and folded everytool_responseinto one message without atool_name. The assistant turn that asked for a tool reached Ollama with notool_calls, so the model saw a tool result for a call it never made, called the tool again, and the generate loop ended withExceeded 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):tool_requestpart becomes an entry in the message'stool_calls(function.name,function.arguments). Non-object input is sent as{}.tool_responsepart becomes its owntoolmessage withtool_nameset. String output is sent as is, other output as JSON (previouslystr(output), a Python repr).Testing:
TestBuildChatMessagesWithTools: a user, tool-request, tool-response round trip, and two parallel tool calls answered in one tool message. Both fail onmainand pass here.uv run pytest packages/genkit-ollama/tests: 250 passed.ruff check --preview,ruff format --preview --checkandty checkpass on the changed files.@huangjeff5 @pavelgj could you take a look?
Checklist: