feat(gemini): implement tool_choice support#1778
Open
jb-delafosse wants to merge 2 commits intostrands-agents:mainfrom
Open
feat(gemini): implement tool_choice support#1778jb-delafosse wants to merge 2 commits intostrands-agents:mainfrom
jb-delafosse wants to merge 2 commits intostrands-agents:mainfrom
Conversation
Add tool_choice parameter support to GeminiModel, enabling Strands'
structured output retry mechanism to work correctly with Gemini.
- Add _map_tool_choice() helper to convert Strands ToolChoice to
Gemini's ToolConfig with FunctionCallingConfigMode
- Update _format_request_config() to accept and apply tool_choice
- Update _format_request() to pass tool_choice through
- Update stream() to pass tool_choice to request formatting
Mapping:
- {"auto": {}} -> FunctionCallingConfigMode.AUTO
- {"any": {}} -> FunctionCallingConfigMode.ANY
- {"tool": {"name": "X"}} -> FunctionCallingConfigMode.ANY with
allowed_function_names=["X"]
Closes strands-agents#1129
Add comprehensive tests for the new tool_choice functionality: - test_map_tool_choice: Parametrized test covering None, auto, any, and specific tool cases - test_stream_request_with_tool_choice: Verify tool_choice passes through to request config - test_stream_request_with_tool_choice_specific_tool: Verify specific tool name is included in allowed_function_names - test_stream_request_tool_choice_ignored_without_tools: Verify tool_choice is ignored when no tools are provided
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.
Description
Implement
tool_choiceparameter support forGeminiModel, enabling Strands' structured output retry mechanism to work correctly with Gemini.When using
Agent(structured_output_model=MyModel), Strands registers the Pydantic model as a tool and expects the model to call it. If the model returns text instead, Strands setstool_choice = {"any": {}}and retries. Previously, Gemini ignored this parameter, causingStructuredOutputException. This PR fixes that.note : This feature is already correctly implemented in the typescript sdk
Changes
_map_tool_choice()helper method to convert Strands ToolChoice to Gemini's ToolConfig_format_request_config()to accept and apply tool_choice_format_request()to pass tool_choice throughstream()to pass tool_choice to request formattingMapping
{"auto": {}}AUTO{"any": {}}ANY{"tool": {"name": "X"}}ANY+allowed_function_names=["X"]Related Issues
Closes #1129
Documentation PR
N/A - No documentation changes required. The
tool_choiceparameter was already documented in thestream()method signature.Type of Change
New feature
Testing
hatch run prepare(viauv run pytestanduv run mypy)_map_tool_choice()covering all casesChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.