Skip to content

Add: structured output support via StructuredOutput type #2133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

fswair
Copy link
Contributor

@fswair fswair commented Jul 4, 2025

Add: StructuredOutput decorator for structured output in Pydantic models
Add: Type alias structured for decorator usage convenience
Add: New entities to __all__ in output.py and _output.py
Add: Imports for new entities in __init__.py
Add: Tests for StructuredOutput with TestModel and decorator pattern

A factory type that enables structured output behavior in Pydantic models.

Example usage:
    from pydantic_ai import Agent
    from pydantic import BaseModel
    from pydantic_ai.output import structured
    from pydantic_ai.output import StructuredOutput
    
    # structured is a type alias for StructuredOutput
    # StructuredOutput(Model) is equivalent to @structured <ModelDef>
    
    @structured
    class FooBar(BaseModel):
        foo: str
        bar: int
    
    class BarBaz(BaseModel):
        bar: str
        baz: int
    
    agent  =  Agent()
    result =  agent.run_sync(
        "Generate a sample object",
        output_type=[FooBar, StructuredOutput(BarBaz)]
    )
    
    assert isinstance(result.output, dict)

fswair added 3 commits June 23, 2025 17:43
Handled CLI exceptions, printed tracebacks to the terminal, and ensured program flow continued.
Add: StructuredOutput decorator for structured output in Pydantic models
Add: Type alias `structured` for decorator usage convenience
Add: New entities to `__all__` in `output.py` and `_output.py`
Add: Imports for new entities in `__init__.py`
Add: Tests for StructuredOutput with `TestModel` and decorator pattern
@fswair
Copy link
Contributor Author

fswair commented Jul 4, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant