Skip to content

Conversation

@fswair
Copy link
Contributor

@fswair fswair commented Jul 8, 2025

Implemented StructuredDict for getting structured python dictionary as output type.

Structured Dict

A dictionary subclass that enforces a JSON schema for structured dict.

This class serves as both a container for structured data and the schema itself, making it suitable for use as an output type in agents that require validated dictionary responses.

from pydantic_ai import Agent, StructuredDict
from pydantic_ai.models.test import TestModel

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"}
    },
    "required": ["name", "age"]
}

agent = Agent(TestModel(), output_type=StructuredDict(schema))
result = agent.run_sync("Create a person")
print(result.output)
#> {'name': 'John Doe', 'age': 30}

Implemented StructuredDict for getting structured python dictionary as output type.
@hyperlint-ai
Copy link
Contributor

hyperlint-ai bot commented Jul 8, 2025

PR Change Summary

Implemented StructuredDict for structured outputs, providing a JSON schema enforcement for structured data in agents.

  • Introduced StructuredDict class for structured outputs
  • Provided an example of using StructuredDict with an Agent
  • Enhanced documentation to include details about JSON schema enforcement

Modified Files

  • docs/output.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

@DouweM DouweM self-assigned this Jul 9, 2025
…inferred correctly. Also add name and description arguments
@DouweM
Copy link
Collaborator

DouweM commented Jul 9, 2025

@fswair I pushed a commit with some tweaks to make the type of agent.output be inferred correctly as dict[str, Any] by turning StructuredDict into a function that returns a new class that subclasses dict[str, Any].

I also added name and description fields so that, when there are multiple output types, the final_result_ tool name can get an appropriate suffix, like you can see in the test.

Finally, I think we should not claim this provides validation, as we depend on completely on the model's understanding of the JSON schema and don't do any Pydantic validation or something like that -- we just pass the dict straight through.

What do you think?

Copy link
Contributor Author

@fswair fswair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better, yes, thank you!

@fswair
Copy link
Contributor Author

fswair commented Jul 9, 2025

i've updated docs with more correct description but its still --not enough-- description and removed prints that causes error.

@fswair
Copy link
Contributor Author

fswair commented Jul 9, 2025

@DouweM can u review this doc before merge: https://github.com/fswair/pydantic-ai/blob/structured_dict/docs/output.md line 390

@DouweM DouweM changed the title Add: StructuredDict for structured outputs Add StructuredDict for structured outputs with custom JSON schema Jul 10, 2025
@fswair
Copy link
Contributor Author

fswair commented Jul 10, 2025

yeah it looks nice

@DouweM DouweM enabled auto-merge (squash) July 10, 2025 15:42
@DouweM DouweM merged commit d5ff4a1 into pydantic:main Jul 10, 2025
17 checks passed
@fswair fswair deleted the structured_dict branch July 10, 2025 15:55
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.

2 participants