liveBook · Manning

Structured data

As explained in Build an AI Agent (From Scratch)

Data organized according to a defined schema with predictable fields, types, or relationships.

Structured data has predictable fields and types. When inputs and outputs already follow a clear schema, ordinary programming is usually faster, cheaper, and more reliable than an LLM. An agent becomes useful when it must interpret varied, unstructured input—but its answer may still need to become structured data before another system can use it. section 1.4.1 section 2.1.3

A schema turns a natural-language request into a data contract. In Python, a Pydantic BaseModel declares field names and types and validates values against them. Pass the model as response_format to request JSON matching that structure. A required name and email can be paired with an optional phone number that defaults to None. section 2.2.5

Listing 2.7 Structured output with Pydantic
from pydantic import BaseModel
from litellm import completion

class ExtractedInfo(BaseModel):
    name: str
    email: str
    phone: str | None = None

response = completion(
    model="gpt-5-mini",
    messages=[{
        "role": "user", 
        "content": "My name is John Smith, my email is john@example.com, and my phone is 555-1234."
    }],
    response_format=ExtractedInfo
)

result = response.choices[0].message.content

The extraction example shows the complete path: declare a Pydantic model, request a matching response, then obtain the result from the response message. section 2.2.5

Structured data serves different points in an agent workflow:

  • Tool calls encode a selected function and its arguments rather than free-form prose. section 3.2.2
  • Final answers expose typed fields to downstream code, avoiding parsing that depends on the model's wording. section 4.7
  • Evaluation responses separate whether a problem is solvable, the final answer, and any reason it cannot be solved. section 2.4.2
  • Memory records preserve a problem, approach, result, and possible error analysis so past attempts can be retrieved and used. section 6.4.2

A tool-calling agent can produce a typed final answer without parsing its final prose. Give the agent an output_type, create a final-answer tool whose input is that Pydantic model, and require tool use when structured output is needed. Treat the answer as complete only after that tool succeeds; then return its validated result. An unsuccessful call must not be mistaken for a finished answer. section 4.7.1 section 4.7.2

Customer-feedback result

Define sentiment as positive, negative, or neutral; confidence as a float; and key_phrases as a list of strings. After the agent analyzes a review, its result.output is a Pydantic object whose fields the calling code can access directly. section 4.7.3

How Build an AI Agent (From Scratch) covers it

How deep it goes
Implements Pydantic response schemas and extends a tool-calling agent through output-tool creation, required tool use, completion checks, and result extraction.
Examples
Uses contact extraction, customer-feedback sentiment fields, GAIA answer records, and structured problem-solving memories.
What it assumes
Assumes basic Python classes and agent tool calls while introducing Pydantic schemas through a small extraction example.
What you can do after
Build it from scratch as a typed agent output path that returns a validated Pydantic object instead of final-answer prose.
How it connects
Connects Pydantic validation, tool calling, agent evaluation, and memory retrieval through schema-based records and responses.

Also explained in

Compare how 24 books explain structured data →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.