← back
Pydantic is all you need: Jason Liu
Takeaway
Use Pydantic schemas plus function calling (via Instructor) so LLM outputs are typed, validatable Python objects rather than hand-parsed strings.
Summary
- Argues most LLM applications are really asking models for structured JSON, then parsing with regex, which is fragile; Pydantic+OpenAI function calling fixes this by passing typed schemas.
- Introduces the Instructor library: patch the OpenAI client and pass a Pydantic `response_model` to get typed, validated objects instead of dicts.
- Pydantic validators (including an llm_validator) catch errors like disallowed content; Instructor can auto-retry by feeding the validation error back to the model.
- Doc strings and field descriptions become part of the JSON schema sent to the model, so prompt quality, code quality, and data quality stay in sync.
- Demonstrates patterns like 'I don't know' escape hatches, nested objects, and converting hallucinated outputs into catchable validation errors.
pydanticstructured-outputsinstructor
Original description
Please return only json, do not add any other comments ONLY RETURN JSON OR I'LL TAKE A LIFE If this was you, then you've probably been pretty happy to see OpenAI function_call get released, I'm here to show you how you can get the most out of such powerful feature. Instead of writing prompts that turn strings into strings, we can write Pydantic objects and get Pydantic objects out of OpenAI. In this talk we explore some model driven development. Where we go step by step with some examples on how to represent your problem as simple code so we can model, generate diagrams, and write prompts as code to save time and model complex data correctly, allow us to use the same best practices rather than having to invent new ones for how we manage prompts. Recorded live in San Francisco at the AI Engineer Summit 2023. See the full schedule of talks at https://ai.engineer/summit/schedule & join us at the AI Engineer World's Fair in 2024! Get your tickets today at https://ai.engineer/worlds-fair About Jason Liu Previously stitch fix and Facebook. Currently consulting startups on production using llm systems.