liveBook · Manning

BaseModel

As explained in Build a Multi-Agent System (from Scratch)

Pydantic BaseModel is a base class for defining typed data models that validate and serialize structured data.

Pydantic BaseModel is a base class for declaring structured data models. Subclass it to define fields, then use the model for validated tool parameters or as a target type for structured LLM output. section 2.1 section 2.3 section 3.1.2

In tool and LLM interfaces, BaseModel supports two related operations:

  • Generate a JSON Schema from a parameter model with model_json_schema(), and validate inputs with model_validate(). section 2.3
  • Specify a subclass as the desired structured-output type; structured_output() returns an instance of that type. section 3.1.2

A function parameter model

Define MyFuncParams as a BaseModel subclass with an integer x field, then accept it as the parameter to my_func and wrap that function in PydanticFunctionTool. section 2.3

# Included in chapter-notebooks/ch02/ch02.ipynb  #A
from pydantic import BaseModel
 
class MyFuncParams(BaseModel):  #B
    x: int
 
 
def my_func(params: MyFuncParams) -> int:  #C
    print(params.x)  #D

See the parameter model and function signature used in the tool example. section 2.3

How Build a Multi-Agent System (from Scratch) covers it

How deep it goes
Explains subclassing, schema generation, and validation calls; defers the full PydanticFunctionTool implementation.
Examples
Uses MyFuncParams with integer x and a Joke model with a constrained subject and joke text.
What you can do after
Apply BaseModel subclasses as parameter schemas for wrapped functions and as requested structured-output types.
How it connects
Connects typed models to JSON Schema, validation, tool calls, and structured output.

Also explained in

Compare how 3 books explain BaseModel →

Related concepts

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