liveBook · Manning

Dataclass

As explained in Learn AI Data Engineering

A data class is a class whose primary purpose is representing structured data, typically with declarative fields and automatically supplied data-oriented behavior.

A data class defines the structured result an AI workflow expects. In these pipelines, a Pydantic BaseModel acts as a schema: it names fields, declares their data types, and validates whether returned data fits that shape. The class becomes a contract between a prompt and the model response, replacing fragile free-form text parsing with a parsed Python object. section 6.2.1 section 6.2.3

Describe a cleaning result

A cleaning task can request two explicit results: row indexes identified as duplicates and column names to remove. Defining those fields up front makes the requested output programmable rather than asking the model to explain its recommendations in prose. from pydantic import BaseModel from typing import List class CleanedData(BaseModel): duplicates: List[int] drop_columns: List[str] The resulting object can drive DataFrame cleanup while avoiding manual string parsing. It also makes the expected shape clear when the prompt, data, or cleaning rules change. section 6.2.1 section 6.2.3

from pydantic import BaseModel
from typing import List
 
class CleanedData(BaseModel):
    duplicates: List[int]
    drop_columns: List[str]

This minimal model shows a data class as two typed fields: duplicate row indexes and columns selected for removal. section 6.2.1

Use the class alongside structured response parsing. A system message can state the task and expected output, while a user message supplies the data. With the BaseModel supplied through the API response format, the response is captured as a fully parsed Python object. This separation keeps instructions distinct from input data and supports reusable cleaning workflows. section 6.2.3

Data classes are especially useful when input structures are nested or likely to evolve. For a library JSON payload, a LibraryBook model specifies flattened fields such as library name, location, title, author, genres, and publication year. Shared attributes are attached to each payload; the model maps nested values to the target structure. When the target schema changes, update the class definition rather than rewriting custom extraction logic. section 7.2

Flatten nested library records

A flattened record model can make the desired output explicit: class LibraryBook(BaseModel): library_name: str location: str title: str author: str genres: str published_year: int A prompt can include this schema and ask for matching data. Compared with hand-coded traversal, concatenation, and array conversion, the schema-centered approach concentrates the output design in one definition. section 7.2

Treat a data class as an operational boundary in an AI-assisted pipeline:

  • Define fields that represent the exact decisions or records downstream code needs, such as duplicate indexes, columns to drop, or flattened customer attributes. section 6.2.1 section 7.6
  • Pair the class with structured response parsing so returned values are parsed and type-checked instead of manually extracted from text. section 6.2.1 section 6.2.3
  • Revise the class and prompt together when new fields or data-quality signals are required, then verify that the returned structure still matches the new fields. section 6.2.3 section 7.2
  • Keep human control over the resulting operations: structured output makes automation more reliable, but cleaning and transformation choices still need to be checked against the data. section 6.2 section 6.2.3

The pattern extends beyond cleanup. Typed models can represent parsed address and subscription fields, entity-resolution confidence and reasoning, transformed transaction attributes, or account-analysis records. Across these uses, the main benefit is adaptability: changes in data formats or business rules can be expressed by updating prompts and data classes instead of rebuilding complex parsing and transformation code. section 7.6

How Learn AI Data Engineering covers it

How deep it goes
Defines Pydantic BaseModel fields and response parsing, contrasting them with string parsing and hand-coded extraction without covering language-level dataclass internals.
Examples
Uses CleanedData for duplicate rows and null-heavy columns, then LibraryBook for flattening nested library JSON.
What it assumes
Introduces Pydantic from zero while assuming practical Python, DataFrame, JSON, and API-based data-workflow context.
What you can do after
Apply it via Pydantic and structured response parsing to obtain typed, reusable AI results for Python data workflows.
How it connects
Connects Pydantic, BaseModel, structured outputs, Chat Completions API, response_format, DataFrames, JSON, and data validation.

Also explained in

Compare how 8 books explain dataclass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI Data Engineering. The section links point to the parts of the book it draws on.