Skip to content

fix(py): apply tool_choice='required' to the first turn only - #7111

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-tool-choice-required
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-tool-choice-required

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

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 until max_turns.

@ai.tool()
async def lookup_order(order_id: str) -> str:
    return 'shipped 2026-11-03'

response = await ai.generate(
    prompt='Look up order #4821.',
    tools=[lookup_order],
    tool_choice='required',
)

# --- BEFORE ---
# Every turn re-sent tool_choice='required', so the model could never reply in text.
print(response.finish_reason, response.finish_message)
# => aborted Exceeded maximum tool call iterations (50)
print(repr(response.text), len(response.messages))
# => '' ~100 (50 forced tool rounds)

# --- AFTER ---
# Turn 1 sends 'required', turn 2 sends 'auto'.
print(response.finish_reason)
# => stop
print(response.text)
# => Order #4821 shipped on Nov 3.

Main Changes

  • 'required' applies to the opening call only: Previously, run_tools_or_stop copied the caller's options into the next turn unchanged, so tool_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 at max_turns with finish_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.
  • Resume closes the round the same way: resolve_resume_options also sends 'auto' after an interrupted tool is answered via resume. The interrupted call already made the forced tool call; re-forcing on resume would bring the loop back.
  • 'auto', 'none', unset, and return_tool_requests are unchanged: Only 'required' is relaxed. With return_tool_requests=True the loop stops after the first call, so the caller still gets the forced tool request and response.request.tool_choice == 'required'.
  • Reverses the '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 until max_turns" in openai_tool_choice_test.py. That test now asserts 'auto' on the follow-up body. The veneer_test.py echo tests use 'auto' instead of 'required': they check that tool_choice survives into response.request on 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 carry toolChoice into 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 with finish_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).

'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.
@huangjeff5
huangjeff5 marked this pull request as draft November 6, 2026 18:10

@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 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.

@huangjeff5
huangjeff5 marked this pull request as ready for review November 6, 2026 18:16
@huangjeff5

Copy link
Copy Markdown
Contributor Author

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.

@huangjeff5 huangjeff5 closed this Nov 6, 2026
@huangjeff5
huangjeff5 deleted the fix/py-tool-choice-required branch November 6, 2026 18:18
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.

1 participant