Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk5 min

Pydantic and LLMs: Turn Free-Form Text Into Validated Data

Use Pydantic to define the record, a compatible LLM interface to request structured output, and application checks to validate both the shape and meaning of extracted data.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To turn unstructured text into application data, define the target record as a Pydantic model, ask a compatible LLM API for a schema-constrained response, then validate the result and check its meaning in your own application. Structured output can enforce a response’s shape; it cannot prove that the extracted values are true or faithfully reflect the source.

How the conversion pipeline works

A reliable extraction workflow has four parts: describe the data your application needs, express that contract as a schema, obtain a structured response, and verify the result before using it. Treat the LLM as an extractor whose output still needs application-side checks—not as the authority on what the source document says.

As an Amazon Associate I earn from qualifying purchases.

  1. Define the target record. Choose fields, types, optional values, allowed choices, and constraints according to the downstream task.
  2. Generate or adapt a schema. Pydantic can produce JSON Schema, but the provider may support only a subset of its features.
  3. Request structured output. Use the selected provider’s native schema-constrained interface when it supports the required schema and model.
  4. Validate and assess meaning. Parse the response into the Pydantic model, then apply domain rules and compare important values with the source.

Define the record in Pydantic

Make the model reflect the record your software needs, rather than trying to capture every detail in the source. Use explicit types and optionality: a missing invoice number should be representable as missing, not guessed. Field descriptions can clarify what counts as a value and where it should come from.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from pydantic import BaseModel, Field

class InvoiceFields(BaseModel):
    supplier: str = Field(description="Supplier named on the invoice")
    invoice_number: str | None = None
    total: float | None = None

schema = InvoiceFields.model_json_schema()

This illustrates a Pydantic model and schema generation, not a complete provider request. Pydantic documents JSON Schema generation, including validation and serialization schema modes. Those modes can differ when a type’s accepted input and serialized representation are not the same, so choose the schema that matches what the model is expected to return.

Use constraints and enums where they represent real application rules. A field description can explain extraction intent, but it is not a substitute for a machine-checkable constraint or a later domain check.

Choose the output method that fits the API

“JSON output” can mean different things. JSON mode aims to produce syntactically valid JSON; native structured output constrains responses to a supported schema. Function calling is generally for asking a model to invoke an application function or tool, whereas a response-format schema is suited to returning a schema-shaped object to the caller. These interfaces are not interchangeable, and exact availability depends on provider, model, and API surface.

Approach What it provides What still needs attention
Native structured output Schema adherence for supported schemas and supported model/API combinations. Provider-specific schema limits, refusals or interrupted responses, application validation, and factual accuracy.
JSON mode Valid JSON, according to OpenAI’s distinction between JSON mode and Structured Outputs. It does not by itself ensure the object follows your particular schema; validate and handle errors.
Prompt-only formatting Instructions asking the model to return a desired structure. Adherence relies on the model following the prompt; parse, validate, and handle deviations.

OpenAI’s Structured Outputs documentation describes Pydantic models as a way to define schemas through its Python library and distinguishes structured output from JSON mode. Pydantic AI describes native and prompted output, noting that prompted output is generally less reliable but may be necessary when native support is unavailable or appropriate for a particular quality need. Check the current documentation for the provider and model you plan to use; support and schema subsets vary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make the schema compatible with the provider

A schema emitted by Pydantic is not automatically accepted unchanged by every constrained-output API. Inspect the generated JSON Schema, compare its keywords and structure with the provider’s supported subset, and simplify or adapt it if needed. Keep the Pydantic model as the application’s validation contract even if the schema sent to the API must be a compatible adaptation.

  • Confirm the feature is available for the exact model and API endpoint.
  • Check which JSON Schema constructs are supported rather than assuming full compatibility.
  • Choose validation or serialization schema mode based on the expected returned representation.
  • Validate the actual response with Pydantic even when the provider constrains generation.

For OpenAI, consult the current Structured Outputs guide for implementation details and supported behavior. Pydantic’s version 2.12 JSON Schema reference explains its schema-generation options; neither source implies that every Pydantic feature is supported by every provider.

Validate structure, then verify the extracted facts

After receiving a response, first establish that the request completed successfully and did not produce a refusal or an incomplete result. Only then parse the returned object into the expected Pydantic model. A refusal or a response cut off by a token limit or other stopping condition may not conform to the requested schema; route these cases separately instead of treating them as ordinary extraction failures.

Successful parsing establishes that values fit the model’s types and constraints. It does not establish that the supplier, date, amount, or interpretation is correct. Add checks for domain meaning, cross-field relationships, required evidence, and ambiguous or absent source values. When auditability matters, consider recording source evidence—such as a supporting text span—alongside a value, then verify that evidence against the original document.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

OpenAI’s documentation warns that schema-constrained output can still contain mistakes in the values. See its Structured Outputs guide and refusal handling guidance. Pydantic’s validation checks are useful for enforcing the application contract, but semantic correctness requires separate checks.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Test extraction quality, not just parse success

Build a representative set of inputs and expected records before relying on the extraction in production. Include clean examples as well as incomplete, ambiguous, and difficult cases. Compare the extracted fields with the source and track semantic errors separately from schema or parsing failures. A response that parses successfully can still be wrong.

  • Test missing values and make sure the model does not invent them.
  • Check dates, amounts, names, and other high-impact fields against their source evidence.
  • Exercise refusal, interrupted-generation, and validation-error paths.
  • Measure field-level extraction quality as well as the rate of structurally valid responses.

Pydantic AI’s evaluation documentation describes evals and testing for agent behavior. The same principle applies to extraction: evaluate outputs against expected results, not only against the schema.

What published schema-following results do—and do not—show

In a 2024 announcement, OpenAI reported that gpt-4o-2024-08-06 achieved 100% on its evaluation of complex JSON Schema following, while gpt-4-0613 scored below 40% on that same evaluation. OpenAI also said the newer model scored 93% on the benchmark before deterministic constrained decoding was added. These are historical, vendor-reported schema-following results—not independent measures of general extraction accuracy or production success. The announcement is available at OpenAI’s 2024 Structured Outputs announcement.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.