Skip to content

Add example: agent with structured output validation (Pydantic) #10

@rajkumar42

Description

@rajkumar42

Summary

Create an example that demonstrates using Pydantic models to validate and enforce structured output from an OpenSymbolicAI agent. Shows how to guarantee the agent returns data in a specific schema.

What needs to happen

  • Create structured_output/ directory
  • Define a Pydantic model for the expected output (e.g., ExtractedContact with name, email, phone)
  • Create an agent that extracts structured data from unstructured text
  • Validate the agent's output against the Pydantic model
  • Handle validation errors gracefully (retry or report)
  • Add README explaining the pattern

Example

from pydantic import BaseModel
from opensymbolicai import Agent

class Contact(BaseModel):
    name: str
    email: str
    phone: str | None

agent = Agent(
    goal="Extract contact info from this text",
    output_schema=Contact
)
result = agent.run(input="Call John at john@example.com or 555-1234")
print(result)  # Contact(name='John', email='john@example.com', phone='555-1234')

Acceptance criteria

  • Output is validated against a Pydantic model
  • Invalid outputs are caught and handled
  • README explains when and why to use structured output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions