Skip to content

feat(gemini): implement tool_choice support#1778

Open
jb-delafosse wants to merge 2 commits intostrands-agents:mainfrom
jb-delafosse:feat/gemini-tool-choice
Open

feat(gemini): implement tool_choice support#1778
jb-delafosse wants to merge 2 commits intostrands-agents:mainfrom
jb-delafosse:feat/gemini-tool-choice

Conversation

@jb-delafosse
Copy link

@jb-delafosse jb-delafosse commented Feb 27, 2026

Description

Implement tool_choice parameter support for GeminiModel, 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 sets tool_choice = {"any": {}} and retries. Previously, Gemini ignored this parameter, causing StructuredOutputException. This PR fixes that.

note : This feature is already correctly implemented in the typescript sdk

Changes

  • Add _map_tool_choice() helper method to convert Strands ToolChoice to Gemini's ToolConfig
  • 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

Strands ToolChoice Gemini FunctionCallingConfigMode
{"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_choice parameter was already documented in the stream() method signature.

Type of Change

New feature

Testing

  • I ran hatch run prepare (via uv run pytest and uv run mypy)
  • Added parametrized unit tests for _map_tool_choice() covering all cases
  • Added integration tests verifying tool_choice passes through to Gemini API requests
  • Added test verifying tool_choice is ignored when no tools are provided

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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

[FEATURE] Implement Gemini Tool Choice

1 participant