pythonintermediate

Structured AI Extraction with Instructor

Use the Instructor library to extract validated Pydantic models from LLM responses reliably.

python
import instructor
from openai import OpenAI
from pydantic import BaseModel, Field
from typing import Optional

client = instructor.from_openai(OpenAI())

class Person(BaseModel):
    name:        str
    age:         Optional[int] = None
    occupation:  str
    skills:      list[str]     = Field(default_factory=list)

class ExtractedPeople(BaseModel):
    people: list[Person]

text = 'Alice (30) is a data engineer who knows Python, SQL and Spark. Bob is a senior ML engineer skilled in PyTorch and transformers.'

result = client.chat.completions.create(
    model='gpt-4o-mini',
    response_model=ExtractedPeople,
    messages=[{'role':'user','content':f'Extract people from: {text}'}],
)
for p in result.people:
    print(p.model_dump())

Use Cases

  • information extraction
  • entity recognition
  • structured NLP

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.