Repository navigation
fix(py/openai,anthropic): relax tool_choice='required' to 'auto' after a tool turn - #160
Merged
huangjeff5 merged 1 commit intoDec 1, 2026
Conversation
…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
approved these changes
Nov 30, 2026
huangjeff5
left a comment
Collaborator
There was a problem hiding this comment.
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!
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.
Fixes #112
ai.generate(..., tools=[...], tool_choice='required')kept sendingtool_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 untilmax_turnsinstead of finishing, the same problem #107 fixed for Gemini.Fix: a small
_effective_tool_choice(request)ingenkit-openaiandgenkit-anthropic, mirroring the one ingenkit-google-genai:'required'becomes'auto'when a tool response follows the most recent model turn inrequest.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 originaltool_choice.Test:
test_required_tool_choice_persists_on_followup_turnpinned the old behaviour; it is nowtest_required_tool_choice_relaxes_to_auto_on_followup_turn(turn 1'required', turn 2'auto'). Addedtest_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 onmainand pass with the change. Fullgenkit-openaitests: 497 passed. Fullgenkit-anthropictests: all pass excepttest_plugin_without_key_and_no_tenant_key_raises_naming_both, which also fails onmainin this environment (needs no ambient API key).Disclosure: this change was prepared with AI assistance (Claude Code).