
Instructor Has a Free API: Get Structured Data From LLMs Every Single Time
LLMs return text. Your code needs JSON. Instructor bridges the gap with validated, typed outputs every time. What Is Instructor? Instructor patches OpenAI/Anthropic/Google clients to return Pydantic models instead of raw text. Define a schema, get structured data. No parsing. No regex. No "please respond in JSON." import instructor from openai import OpenAI from pydantic import BaseModel client = instructor . from_openai ( OpenAI ()) class User ( BaseModel ): name : str age : int email : str user = client . chat . completions . create ( model = " gpt-4o " , response_model = User , messages = [{ " role " : " user " , " content " : " John is 30 years old, email john@example.com " }] ) print ( user ) # User(name='John', age=30, email='john@example.com') print ( user . name ) # 'John' — fully typed, IDE autocomplete works Validation + Retry from pydantic import BaseModel , field_validator class UserProfile ( BaseModel ): name : str age : int @field_validator ( ' age ' ) @classmethod def ag
Continue reading on Dev.to Python
Opens in a new tab


