Skip to content

[BUG] AgentResult.structured_output returns BaseModel | None #1114

@LeviLong01

Description

@LeviLong01

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

Python Version

3.11.14

Operating System

macOS 15.7

Installation Method

pip

Steps to Reproduce

Error:

    def some_function(self) -> TestOutputModel:
        agent = Agent(structured_output_model=TestOutputModel)
        agent_response = agent("This is a test")

        return agent_response.structured_output # mypy fails due to error: Incompatible return value type (got "BaseModel | None", expected "TestOutputModel")  [return-value]

Expected Behavior

Workaround:

    def some_function(self) -> TestOutputModel:
        agent = Agent(structured_output_model=TestOutputModel)
        agent_response = agent("This is a test")

        return cast(TestOutputModel, agent_response.structured_output)

I expect that it would behave the same as Agent.structured_output with it's return type.

Actual Behavior

Error:

    def some_function(self) -> TestOutputModel:
        agent = Agent(structured_output_model=TestOutputModel)
        agent_response = agent("This is a test")

        return agent_response.structured_output # mypy fails due to error: Incompatible return value type (got "BaseModel | None", expected "TestOutputModel")  [return-value]

Additional Context

No response

Possible Solution

Handle generic types for structured_output on the AgentResult

Related Issues

No response

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