How to Get Structured Output from Any LLM in 5 Min
You asked an LLM to extract contact info from an email. It returned a wall of text instead of clean data. Now you're writing regex to parse a response that changes format every time. There's a bett...

Source: DEV Community
You asked an LLM to extract contact info from an email. It returned a wall of text instead of clean data. Now you're writing regex to parse a response that changes format every time. There's a better way. PydanticAI's output_type parameter forces any LLM to return typed, validated data -- no parsing required. The Code import asyncio from pydantic import BaseModel, Field from pydantic_ai import Agent class ContactInfo(BaseModel): """Structured contact details extracted from text.""" name: str = Field(description="Full name of the person") email: str = Field(description="Email address") company: str = Field(description="Company or organization") role: str = Field(description="Job title or role") agent = Agent( 'openai:gpt-4o', output_type=ContactInfo, instructions='Extract contact information from the provided text.', ) raw_text = """ Hey, just met Sarah Chen at the DevTools Summit. She's the VP of Engineering at Acme Corp. Her email is [email protected] -- said she's interested in