Skip to content

Conversation

@anwschne
Copy link

@anwschne anwschne commented Nov 5, 2025

Fix type inference for
AgentResult.structured_output" --body "

Summary

  • Add generic type parameters to Agent and AgentResult for proper type inference of structured_output
  • Resolves issue where AgentResult.structured_output returns BaseModel | None instead of the specific model type
  • Eliminates need for manual type casting when using structured outputs

Changes

  • Updated Agent class to accept generic type parameter TStructuredOutput
  • Updated AgentResult to use generic type parameter for structured_output field
  • Type inference now correctly propagates from Agent[TestOutputModel] to agent_response.structured_output

Related Issues

#1114

Type of Change

Bug fix

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.

…nference

Add generic type parameters to Agent, AgentResult, and AgentResultEvent

to enable proper type inference for structured output fields. This allows

type checkers and IDEs to validate field access on specific Pydantic model

types without requiring explicit type casting.

Changes:

- Add Generic[T] base class to AgentResult with T bound to BaseModel

- Add Generic[T] base class to Agent with structured_output_model: Type[T]

- Update Agent invocation methods to return AgentResult[T]

- Add Generic[T] base class to AgentResultEvent for type consistency

- Add comprehensive docstrings with type parameter documentation

- Add test suite for type inference validation (9 new tests)

- Add reproduction test demonstrating the issue (6 tests)

- Update existing tests to work with generic types

Benefits:

- Type-safe field access on structured_output without explicit casting

- IDE autocomplete works correctly on structured output fields

- mypy can validate field access at development time

- Backward compatible - existing code without annotations continues to work

- No runtime overhead - purely type system enhancement

Type inference flow:

  agent: Agent[UserProfile] = Agent(

      model=model,

      structured_output_model=UserProfile

  )

  result = agent('prompt')  # AgentResult[UserProfile]

  if result.structured_output is not None:

      name: str = result.structured_output.name  # Type-safe access

Tested across Python 3.10, 3.11, 3.12, 3.13 - all 1489 tests pass.
@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@afarntrog
Copy link
Contributor

Hi @anwschne thanks for creating this PR! We are not inclined to make the agent class generic right now to maintain backwards compatibly. Thanks!

@afarntrog afarntrog closed this Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants