Skip to content

[BUG] Structured Output not enforcing tool_choice for Amazon Nova and Claude models #784

@ArlindNocaj

Description

@ArlindNocaj

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

1.5.0

Python Version

3.13.1

Operating System

MacOS

Installation Method

pip

Steps to Reproduce

  1. run structured output as described in https://strandsagents.com/latest/documentation/docs/examples/python/structured_output/
  2. observe the invocation call e.g. in bedrock contain
 "toolChoice": {
                    "auto": {}
                }

instead of as https://docs.aws.amazon.com/nova/latest/userguide/tool-choice.html

tool_config = {
    "toolChoice": {
        "tool": { "name" : "MyOutputModel"}
    },
    "tools": [
        {...
{
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": "\nDetect something
                        },
                        {
                            "image": {
                                "format": "jpeg",
                                ...
                            }
                        }
                    ]
                }
            ],
            "system": [
                {
                    "text": "## System Role\nYou are a specialized AI assistant ..."
                }
            ],
            "inferenceConfig": {},
            "toolConfig": {
                "tools": [
                    {
                        "toolSpec": {
                            "name": "MyOutputModel",
                            "description": "my great tool schema",
                            "inputSchema": {
                                "json": {
                                    "type": "object",
                                    "title": "MyOutputModel",
                                    ...
                            }
                        }
                    }
                ],
                "toolChoice": {
                    "auto": {}
                }
            }
        },
    },```

### Expected Behavior

If structured output can be provided only one pydantic model it should always enforce the usage of the tool with constrained decoding: https://aws.amazon.com/blogs/machine-learning/structured-outputs-with-amazon-nova-a-guide-for-builders/

tool_config should thus use the selected tool if single tool is provided for structured_output.

tool_config = {
"toolChoice": {
"tool": { "name" : "MyOutputModel"}
},
"tools": [
{...


If that is not enforced the model could also produce a normal output and decide not to use the particular tool for some reason...

The same holds for Claude models:
https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#forcing-tool-use


### Actual Behavior

see above

### Additional Context

_No response_

### Possible Solution

* adjust tool_choice for Nova and Claude models (potentially other models as well).
* add test set for structured output to catch this error in the feature and test other models

### Related Issues

https://github.com/strands-agents/sdk-python/issues/256

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions