Skip to content

[FEATURE] Add support for Bedrock/Anthropic ToolChoice to Structured output #256

Open
@Unshure

Description

@Unshure

Problem Statement

The structured_output implementation in the Bedrock and Anthropic model provider call the model with a hard coded tool_choice: auto: https://github.com/strands-agents/sdk-python/blob/main/src/strands/models/bedrock.py#L206

Proposed Solution

To help get better results out of the model, we should be able to configure this ToolChoice to specify the newly added structure output tool: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_SpecificToolChoice.html

This can be done by creating a private class variable at initialization for toolChoice, and in this function we temporarily override it:

__init__(...):
    self._tool_choice = {"auto": {}}
...

# https://github.com/strands-agents/sdk-python/blob/main/src/strands/models/bedrock.py#L191
    "toolChoice": self._tool_choice,

...

    temp_tool_choice = self._tool_choice
    self._tool_choice = {"tool" : {"name" : tool_spec["name"]}}
    response = self.converse(messages=prompt, tool_specs=[tool_spec])
    self._tool_choice = temp_tool_choice

Use Case

Get better result of the specified pydantic model being output by the LLM as a tool use

Alternatives Solutions

N/A

Additional Context

N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions