liveBook · Manning

BaseModel

As explained in Build Python Web Apps with Streamlit

BaseModel is pydantic's base class for declaring validated, schema-driven data models.

BaseModel is pydantic’s foundation for declaring typed, validated, schema-driven data models. Subclass it to describe the structured data your application expects, then reuse built-in validation, type checking, and serialization. section 9.3.1

Model an answer evaluation

A quiz application can define two required fields: `is_correct: bool` and `correct_answer: str`. An `AnswerEvaluation` subclass of BaseModel gives the LLM a precise response schema and lets the application read `evaluation.is_correct` and `evaluation.correct_answer` directly. section 9.3.1

Listing 9.5 The AnswerEvaluation class in answer_evaluation.py
from pydantic import BaseModel

class AnswerEvaluation(BaseModel):
  is_correct: bool
  correct_answer: str

Inspect the minimal AnswerEvaluation model, including its BaseModel inheritance and typed fields. section 9.3.1

BaseModel resembles a dataclass but provides more extensive validation and related functionality. Its internal implementation can remain out of sight while the model serves as a contract between generated data and application code. section 9.3.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains inheritance, typed fields, validation, and serialization, but defers BaseModel internals and detailed inheritance theory.
Examples
Uses an AnswerEvaluation model with boolean correctness and string answer fields for quiz-response parsing.
What it assumes
Assumes basic Python classes and type annotations while briefly introducing inheritance through the subclass-superclass relationship.
What you can do after
Build it from scratch by declaring a typed Pydantic subclass for structured, validated application data.
How it connects
Connects BaseModel with pydantic, type hints, data validation, serialization, dataclasses, inheritance, and Structured Outputs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.