Skip to content

[BUG] Tool is not called with structured output #872

@SZubarev

Description

@SZubarev

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

Python Version

3.12.11

Operating System

macOS 15.6.1

Installation Method

pip

Steps to Reproduce

from strands import Agent, tool
from pydantic import BaseModel, Field

# Define a Pydantic model for the desired output structure
class UserInfo(BaseModel):
    id: str = Field(description="A unique identifier for the user")
    name: str = Field(description="The full name of the user")
    age: int = Field(description="The age of the user in years", ge=0, le=150)
    city: str = Field(description="The city where the user currently lives")


@tool
def generate_user_id() -> str:
    """Generate a unique user identifier

    Creates a universally unique identifier

    Returns:
        str: A unique user identifier
    """
    print("Generating user ID")
    return "User_12345"


def main():
    agent = Agent(
        model="us.anthropic.claude-sonnet-4-20250514-v1:0", tools=[generate_user_id]
    )

    print(agent.model.config)

    # Use structured_output to guide the agent to return a UserInfo object
    user_data = agent.structured_output(
        prompt="Generate information about random users. Use generate_user_id() tool to generate user identifier",
        output_model=UserInfo,
    )

    # user_data will be an instance of UserInfo, which can be easily converted to JSON
    print(user_data.model_dump_json())


if __name__ == "__main__":
    main()

Expected Behavior

We should see tool called and "User_12345" is used for UserID.

Actual Behavior

Random ID or "" is generated

Additional Context

No response

Possible Solution

No response

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-structured-outputRelated to the structured output apibugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions