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 · all subjects

json_schema

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

JSON Schema generation from Pydantic models

Pydantic can generate a JSON Schema for any Pydantic schema, allowing self-documenting APIs and integration with tools that support JSON Schema format.

Pydantic JSON Schema compliance

Pydantic is compliant with the latest version of JSON Schema specification (2020-12), which is compatible with OpenAPI 3.1.

JSON Schema generation example

Example showing JSON Schema generation from a Pydantic model: from datetime import datetime from pydantic import BaseModel class Address(BaseModel): street: str city: str zipcode: str class Meeting(BaseModel): when: datetime where: Address why: str = 'No idea' print(Meeting.model_json_schema()) """ { '$defs': { 'Address': { 'properties': { 'street': {'title': 'Street', 'type': 'string'}, 'city': {'title': 'City', 'type': 'string'}, 'zipcode': {'title': 'Zipcode', 'type': 'string'}, }, 'required': ['street', 'city', 'zipcode'], 'title': 'Address', 'type': 'object', } }, 'properties': { 'when': {'format': 'date-time', 'title': 'When', 'type': 'string'}, 'where': {'$ref': '#/$defs/Address'}, 'why': {'default': 'No idea', 'title': 'Why', 'type': 'string'}, }, 'required': ['when', 'where'], 'title': 'Meeting', 'type': 'object', } """

json_schema_input_type argument for before/plain/wrap validators

When using before, plain, or wrap field validators, the accepted input type may differ from the field annotation. The json_schema_input_type argument can be provided to @field_validator to specify the correct input type in the JSON schema. If not provided, Pydantic uses the field type by default, except for plain validators where it defaults to Any since the field type is completely discarded.

json_schema_input_type example for before validator

Example of using json_schema_input_type: from typing import Any from pydantic import BaseModel, field_validator class Model(BaseModel): value: str @field_validator('value', mode='before', json_schema_input_type=int | str) @classmethod def cast_ints(cls, value: Any) -> Any: if isinstance(value, int): return str(value) else: return value Model.model_json_schema()['properties']['value'] produces {'anyOf': [{'type': 'integer'}, {'type': 'string'}], 'title': 'Value'}

Give your agent this brain