new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Pydantic · Concepts · all subjects

models

63 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

Use generics for polymorphic models

To handle polymorphic models, use generics: class Main[BaseT: Base](BaseModel): model: BaseT. This allows m = Main[Sub1](model={'base_field': 1, 'sub1_field': 'test'}) to work correctly.

Basic Pydantic model example

Example showing basic Pydantic model usage with Field for metadata: from datetime import date from pydantic import BaseModel, Field class Person(BaseModel): name: str age: int = Field(description='The age of the person') birthdate: date | None = None p = Person(name='John', age=20, birthdate='1970-01-01') This creates a Person model with name (required string), age (required int with description), and optional birthdate field that coerces the string to a date.

Discriminated union example

Example using discriminated unions for polymorphic models: from typing import Annotated, Literal from pydantic import BaseModel, Field class Base(BaseModel): base_field: int class Sub1(Base): type: Literal['sub1'] sub1_field: str class Sub2(Base): type: Literal['sub2'] sub2_field: bool Subs = Annotated[Sub1 | Sub2, Field(discriminator='type')] class Main(BaseModel): model: Subs m = Main(model={'type': 'sub1', 'base_field': 1, 'sub1_field': 'test'}) This ensures correct validation and serialization based on the discriminator field.

Give your agent this brain

models (2/2) — Pydantic · Concepts