Skip to content

fix(py/openai,anthropic): relax tool_choice='required' to 'auto' after a tool turn - #160

Merged
huangjeff5 merged 1 commit into
genkit-ai:mainfrom
pcbeingused33:fix/tool-choice-required-followup
Dec 1, 2026
Merged

huangjeff5 merged 1 commit into
genkit-ai:mainfrom
pcbeingused33:fix/tool-choice-required-followup

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Fixes #112

ai.generate(..., tools=[...], tool_choice='required') kept sending tool_choice='required' (OpenAI) / {'type': 'any'} (Anthropic) on every follow-up turn after a tool responded. The model could never return text, so the run looped until max_turns instead of finishing, the same problem #107 fixed for Gemini.

Fix: a small _effective_tool_choice(request) in genkit-openai and genkit-anthropic, mirroring the one in genkit-google-genai: 'required' becomes 'auto' when a tool response follows the most recent model turn in request.messages; the first call of a user turn (including a new user turn after an earlier tool round) still forces a tool. Other values pass through unchanged. The thinking-mode check in Anthropic still looks at the caller's original tool_choice.

Test: test_required_tool_choice_persists_on_followup_turn pinned the old behaviour; it is now test_required_tool_choice_relaxes_to_auto_on_followup_turn (turn 1 'required', turn 2 'auto'). Added test_generate_claude_required_tool_choice_relaxes_to_auto_after_tool_result (auto after a tool result, {'type': 'any'} again on a new user turn). Both fail on main and pass with the change. Full genkit-openai tests: 497 passed. Full genkit-anthropic tests: all pass except test_plugin_without_key_and_no_tenant_key_raises_naming_both, which also fails on main in this environment (needs no ambient API key).


Disclosure: this change was prepared with AI assistance (Claude Code).

…r a tool turn

'required' forced a tool call on every turn, so a run with tool_choice='required'
never produced a final answer and looped until max_turns. Send it only on the
first call of a user turn, as the Gemini plugin already does.

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified unit tests across genkit-openai and genkit-anthropic (99 passed). Mirrors _effective_tool_choice from genkit-google-genai (#107) and fixes the tool_choice='required' loop after tool results. Looks good!

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 30, 2026 21:21
@huangjeff5
huangjeff5 merged commit 86d259f into genkit-ai:main Dec 1, 2026
20 checks passed
@huangjeff5 huangjeff5 mentioned this pull request Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(openai,anthropic): relax tool_choice='required' to 'auto' after a tool turn

2 participants