WhoBuilt

Pydantic is all you need

Nov 1, 2023· 18 min· 231K views·Watch on YouTube

Speaker

Jason Liu, creator of Instructor, explains how to use Pydantic for structured prompting and language model applications at an AI Engineer event.

Why JSON prompts fail

Liu says many production applications ask language models for structured output that existing software can consume. Asking for JSON as plain text can produce extra commentary, inconsistent keys, or missing values, leaving developers to parse and validate fragile strings. OpenAI function calling helps specify a JSON Schema, but developers still need to check the parsed data.

Use Pydantic models as prompts

Pydantic uses Python type hints to validate and parse data, and can generate the JSON Schema used by OpenAI function calling. Liu demonstrates a model with a datetime and tuple fields, which can parse input values while giving developers IDE support such as autocomplete. He presents structured prompting as defining the desired output through reviewable code and data models.

Add validation and recovery

With Instructor, developers can set a Pydantic model as the response model for an OpenAI completion and receive a typed object. Liu also describes Marvin as an option for working with more language models. Validators can enforce rules, including rejecting objectionable text or checking a name against a database, while retries can send validation errors back to the model for another attempt.

Build reusable structures and plans

Liu shows how optional fields can represent cases where information is unknown, and how reusable nested models can represent time ranges, properties, or user networks. He applies the approach to retrieval augmented generation by having a model produce typed searches with fields such as query, source type, and date filter. A dependency graph can represent a query plan whose independent steps run in parallel.

Ground outputs in source data

Liu describes generating knowledge graphs by modeling data to match a graph visualization API. For question answering, he proposes representing answers as facts with substring quotes from the source text, then validating that the quotes exist and discarding unsupported facts. He also points to potential structured interfaces for evaluation and image outputs, such as bounding boxes paired with product search queries.

Takeaways

  • Define language model outputs as typed models so application code can work with validated objects instead of loosely parsed dictionaries.
  • Put field descriptions and instructions in the model so the schema and prompt are easier to review together.
  • Use validators and retries to enforce application rules and recover from invalid model outputs.
  • Represent retrieval searches and dependencies as data structures to make query execution easier to manage.
  • Require source text evidence for extracted facts when answers need traceable support.

Mentioned in this talk

Search inside this talk

What was claimed 5

  • built
    “That's why I built a library called Instructor”
    YouTube
    Verified2 sources· Talk, Web· archived
  • metric
    Pydantic AI: 70 million downloads a month
    “It has 70 million downloads a month”
    YouTube
    Unverified1 source· Talk
  • feature
    Pydantic AI: Python data model validation powered by type hints; supports model and field validation and outputs JSON Schema
    “It's powered by type hints and has great model and field validation.”
    YouTube
    Unverified1 source· Talk
  • feature
    Marvin: Provides access to more language models and capabilities for Pydantic work
    “It gives you access to more language models and more capabilities.”
    YouTube
    Unverified1 source· Talk
  • feature
    Instructor: Supports validation retries by sending validation errors back to the language model
    “append the message you had before, capture all the validations in one shot, send them back to the language model, and try again”
    YouTube
    Unverified1 source· Talk

Related talks