fix: handle multiple tool calls in tools examples#634
Open
Krishnachaitanyakc wants to merge 1 commit intoollama:mainfrom
Open
fix: handle multiple tool calls in tools examples#634Krishnachaitanyakc wants to merge 1 commit intoollama:mainfrom
Krishnachaitanyakc wants to merge 1 commit intoollama:mainfrom
Conversation
The tools.py and async-tools.py examples only sent the last tool call result back to the model, discarding results from earlier tool calls. This fix appends each tool result as a separate message inside the loop, matching the pattern already used in multi-tool.py and gpt-oss-tools.py. Fixes ollama#476
Pawansingh3889
left a comment
There was a problem hiding this comment.
Good catch — I ran into this exact issue when building a multi-tool agent with Ollama. The original code only appended the last tool result because the loop variable got overwritten on each iteration.
The fix correctly:
- Appends the assistant message (with tool_calls) before iterating
- Appends each tool result inside the loop as a separate message
- Adds an
"Function not found"fallback for missing functions
One minor thought: the fallback string "Function not found" could be more descriptive — something like f"Error: function {tool.function.name} not available" would help the model self-correct in the next turn.
Tested the same pattern in my own project and it works correctly. LGTM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tools.pyandasync-tools.pyexamples so that all tool call results are sent back to the model, not just the last onetoolmessage after the loop using the last value ofoutput, which meant earlier tool call results were silently discardedmulti-tool.pyandgpt-oss-tools.pyFixes #476
Changes
examples/tools.py: Movemessages.append(response.message)before the loop; append each tool result inside the loopexamples/async-tools.py: Same fix applied to the async variantTest plan
ruff checkpasses on both changed filestools.pywith a prompt that triggers multiple tool calls (e.g., "What is three plus one, then subtract two?") and confirm all results are sent to the model