Skip to content

Conversation

@dsfaccini
Copy link
Contributor

@dsfaccini dsfaccini commented Dec 2, 2025

Closes #2799

Adds tool_choice to ModelSettings, letting users control how the model interacts with function tools.

Currently Pydantic AI internally decides whether to use tool_choice='auto' or 'required' based on output configuration, but users have no way to override this. The workaround was using extra_body={'tool_choice': 'none'} which is provider-specific and doesn't work everywhere.

This PR allows the user to set tool_choice to:

  • 'auto' - model decides whether to call tools
  • 'required' - model must call a tool
  • 'none' - model can't use function tools
  • ['tool_a', 'tool_b'] - model must use one of these specific tools

One important distinction: this only affects function tools (the ones you register on the agent), not output tools (used internally for structured output). So if you have an agent with output_type=SomeModel
and you set tool_choice='none', the output tool stays available - you'll just get a warning about it.

Implementation is spread across all model providers since each has its own API format for tool_choice.
Added a resolve_tool_choice utility that handles validation (checking tool names exist, warning about
conflicts with output tools) and returns a normalized representation that each provider then maps to their specific format.

Bedrock is a bit of a special case - it doesn't support 'none' at all, so we fall back to 'auto' with a warning. Anthropic has a constraint where 'required' and specific tool selection don't work with thinking/extended thinking enabled.

TODO

  • document this somewhere in the docs

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.

Allow setting tool_choice

1 participant