Repository navigation
fix: handle multiple tool calls in tools examples - #634
ParthSareen merged 1 commit into
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.
ParthSareen
left a comment
There was a problem hiding this comment.
Good fix — correctly moves the assistant message before the loop and appends each tool result inside it, matching the pattern in gpt-oss-tools.py. Both sync and async variants are consistent. Nice catch!
Reviewed by Hermes agent
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