Skip to content

Conversation

liushang1997
Copy link

@liushang1997 liushang1997 commented Aug 22, 2025

Description

The structured_output implementation in the Bedrock and Anthropic model provider call the model with a hard coded tool_choice: auto. The auto option lets the model itself decides whether to use a tool and use which tool. Instead, we want to make sure the tool is used in structured_output function for the sake of performance.

Therefore, following changes are made:

  1. Add a new parameter to Model.stream() that takes in the toolChoice, defaulting to auto;
  2. Plumb it through so that each provider uses it (if they support it)
  3. For providers that use tool calling for structured_output (Bedrock and Anthropic), pass toolChoice=any when calling stream()
  4. Add unit tests

Related Issues

#256

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

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.

@@ -197,7 +199,7 @@ def format_request(
else []
),
],
"toolChoice": {"auto": {}},
**({"toolChoice": tool_choice} if tool_choice else {}),
Copy link
Member

Choose a reason for hiding this comment

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

I think we'd want to preserve the old behavior?

Suggested change
**({"toolChoice": tool_choice} if tool_choice else {}),
**({"toolChoice": tool_choice} if tool_choice else {"auto": {}}),

LMK if I'm missing a reason not to have the old behavior

@@ -171,6 +171,27 @@ def format_request_tool_message(cls, tool_result: ToolResult) -> dict[str, Any]:
"content": [cls.format_request_message_content(content) for content in contents],
}

@classmethod
def format_request_tool_choice(cls, tool_choice: ToolChoice) -> Union[str, dict[str, Any]]:
Copy link
Member

Choose a reason for hiding this comment

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

Let's mark this as internal with an underscore prefix

@@ -290,6 +297,8 @@ async def stream(
messages: List of message objects to be processed by the model.
tool_specs: List of tool specifications to make available to the model.
system_prompt: System prompt to provide context to the model.
tool_choice: Selection strategy for tool invocation. **Note: This parameter is accepted for
Copy link
Member

Choose a reason for hiding this comment

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

If these are not implemented, if tool_choice is not None, throw instead of just relying on documentation

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.

2 participants