Repository navigation
fix(py): apply tool_choice='required' to the first turn only - #7111
huangjeff5 wants to merge 1 commit into
Conversation
'required' was re-sent on every turn of the tool loop, so a model that obeys it could never answer in text and the loop ran to max_turns. A closed tool round (normal loop or resume) now downgrades 'required' to 'auto'. 'auto', 'none', unset, and return_tool_requests are unchanged.
There was a problem hiding this comment.
Code Review
This pull request updates the generation loop behavior so that a tool_choice of 'required' is relaxed to 'auto' on subsequent turns after a tool round has been completed. This prevents the model from being forced into an infinite tool-calling loop and allows it to provide a final text response. The change is implemented via a new helper function tool_choice_after_tool_round in _generate.py and is applied during both standard generation turns and when resuming from an interrupt. Corresponding tests have been updated and added to verify this behavior. I have no feedback to provide on these changes.
|
Closing: #6696 intentionally keeps 'required' on every turn to match JS and Go (and Anthropic / Go compat_oai). Use return_tool_requests=True to get the forced call back without looping. |
tool_choice='required'now forces only the model call that opens the tool loop. After the tools run, the next call gets'auto', so the model can answer instead of calling tools untilmax_turns.Main Changes
'required'applies to the opening call only: Previously,run_tools_or_stopcopied the caller's options into the next turn unchanged, sotool_choice='required'went out on every model call. A model that obeys'required'must call a tool and cannot return text, so the loop always ended atmax_turnswithfinish_reason='aborted'and an empty answer. A closed tool round now downgrades'required'to'auto', because "use a tool before answering" is satisfied once a tool has run, and'auto'still lets the model chain more tools.resolve_resume_optionsalso sends'auto'after an interrupted tool is answered viaresume. The interrupted call already made the forced tool call; re-forcing on resume would bring the loop back.'auto','none', unset, andreturn_tool_requestsare unchanged: Only'required'is relaxed. Withreturn_tool_requests=Truethe loop stops after the first call, so the caller still gets the forced tool request andresponse.request.tool_choice == 'required'.'required'rule from fix(py/plugins)!: OpenAI tool_choice after tool turns, integer topK, evaluator typing #6696: fix(py/plugins)!: OpenAI tool_choice after tool turns, integer topK, evaluator typing #6696 pinned "'required'forces a tool call every turn untilmax_turns" inopenai_tool_choice_test.py. That test now asserts'auto'on the follow-up body. Theveneer_test.pyecho tests use'auto'instead of'required': they check thattool_choicesurvives intoresponse.requeston later-turn exits, which'required'no longer does by design.Cross-SDK
JS (
generate/action.ts:nextRequest = {...rawRequest, messages}) and Go (handleToolRequests:newReq := *req) both carrytoolChoiceinto every turn unchanged, so they have the same loop. Follow-up: apply the same downgrade there.Tests
test_tool_choice_required_forces_only_the_first_model_call: requests see['required', 'auto'], one tool run, the model's text returned withfinish_reason='stop'.test_tool_choice_other_than_required_carries_across_tool_rounds('auto','none',None): value reaches both calls unchanged.test_tool_choice_required_with_return_tool_requests_returns_first_call: one forced call, tool not run, echoed request keeps'required'.test_tool_choice_required_resume_lets_model_answer: resume sends'auto'and finishes with text.The first and last fail on
main(51 forced calls, then aborted).