Instructor and Pydantic: extract invoice data from PDFs
Learn to extract PDF invoice fields with Instructor, Pydantic and OpenAI, including datetime conversion and a vendor ambiguity that needs checking.
Player not loading? Watch on YouTube
This tutorial, uploaded on August 18, 2025, demonstrates PDF invoice extraction with Instructor and Pydantic in Python. The source does not specify package versions. The example uses the OpenAI API and requires an API key in the environment; it does not demonstrate a local LLM or offline inference.
The walkthrough starts with an existing uv environment and uses pypdf to read an invoice. A helper function collects text from each page into one string. The presenter then defines an InvoiceData model with a vendor, invoice number, invoice date, due date and total due. The dates use Python datetime objects, while the total uses a float.
Instructor wraps an OpenAI client, and the request supplies the invoice text alongside the Pydantic response model. The presenter uses Python DevTools to inspect the returned object and shows how applications can access individual fields directly.
In the demonstrated result, the dates, invoice number and total match the PDF according to the presenter. The vendor field returns "test business" rather than the expected "demo sliced invoices", exposing an ambiguity that type validation alone does not resolve. The presenter recommends testing extracted values before production use and describes Pydantic field validators and Instructor retries as ways to handle validation failures. Multi-provider support and nested models receive a brief explanation, but the worked example uses OpenAI throughout.