Attach examples or descriptions to generated schemas
Documenting API contracts
Pydantic · Pipe
json_schema_extra adds custom properties to a Pydantic model's generated JSON Schema. Configure it with a dictionary or callable; its default is None.
Set json_schema_extra in a model's configuration to add custom properties to the JSON Schema generated for that model. The value can be a dictionary of schema data or a callable that modifies the schema.
model_config = ConfigDict(json_schema_extra={...})| Name | Type | Description |
|---|---|---|
json_schema_extra | JsonDict | JsonSchemaExtraCallable | None | Extra JSON Schema properties supplied as a dictionary, a callable, or None. |
from pydantic import BaseModel, ConfigDict
class Product(BaseModel):
model_config = ConfigDict(
json_schema_extra={"examples": [{"name": "Keyboard"}]}
)
name: str
print(Product.model_json_schema())Adds an example object to the generated JSON Schema.
Documenting API contracts
Schema customization
Dynamic metadata
json_schema_extra is a model configuration option for adding extra JSON Schema properties. It accepts either a dictionary or a callable.
Follow-up: What happens when the option is omitted? It defaults to None, so this customization is not applied.
Use ConfigDict(json_schema_extra={...}) to attach metadata such as examples or descriptions to the generated schema. The extra data belongs to schema generation rather than runtime field validation.
Follow-up: How could dynamic metadata be supplied? Use the callable form so the schema customization can be computed or applied programmatically.
The documented type is JsonDict | JsonSchemaExtraCallable | None. This means a dictionary, a supported callable, or no value can be used.
Follow-up: What should be checked when choosing the callable form? Verify the callable matches Pydantic's documented JsonSchemaExtraCallable type and modifies schema data as intended.
ConfigDictBaseModel.model_json_schemaJsonDictJsonSchemaExtraCallable