new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Pydantic · all subjects

json_schema generation

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

BaseModel method name changes: json_schema to model_json_schema

In Pydantic V1, the method `json_schema()` is used to generate JSON schema. In Pydantic V2, this has been renamed to `model_json_schema()`.

Custom type __modify_schema__ replaced with __get_pydantic_json_schema__

In Pydantic V2, the `__modify_schema__` method for custom types is replaced with `__get_pydantic_json_schema__` for JSON schema customization.

JSON schema Optional indicates null allowed

In Pydantic V2, the JSON schema for `Optional` fields now indicates that the value `null` is allowed.

Decimal exposed as string in JSON schema

In Pydantic V2, the Decimal type is now exposed in JSON schema (and serialized) as a string.

JSON schema no longer preserves namedtuples

In Pydantic V2, the JSON schema no longer preserves namedtuples as namedtuples.

JSON schema targets draft 2020-12 by default

In Pydantic V2, the JSON schema generated by default targets draft 2020-12 (with some OpenAPI extensions).

JSON schema can represent inputs or outputs

In Pydantic V2, when validation inputs and serialization outputs differ, you can specify if you want the JSON schema representing the inputs to validation or the outputs from serialization.

GenerateJsonSchema class for customizing JSON schema

In Pydantic V2, the GenerateJsonSchema class implements the translation of a type's pydantic-core schema into JSON schema. It breaks the process into smaller methods that can be easily overridden in subclasses. Pass a custom subclass to `schema_generator` parameter on methods like `BaseModel.model_json_schema()` or `TypeAdapter.json_schema()`.

Give your agent this brain