pydantic.json_schema module contains classes and functions to allow the way JSON Schema is generated to be customized.
In general you shouldn’t need to use this module directly; instead, you can use BaseModel.model_json_schema and TypeAdapter.json_schema.
GenerateJsonSchema
Whether to use field aliases in the generated schemas.
The format string to use when generating reference names.
The format to use when combining schemas from unions together:
'any_of': Use theanyOfkeyword to combine schemas (the default)'primitive_type_array': Use thetypekeyword as an array of strings, containing each type of the combination. If any of the schemas is not a primitive type (string,boolean,null,integerornumber) or contains constraints/metadata, falls back toany_of.
Attributes
The JSON schema dialect used to generate the schema. Default:
'https://json-schema.org/draft/2020-12/schema'Warnings to ignore when generating the schema. Default:
{'skipped-choice'}Whether to use field aliases when generating the schema.
The format string used when generating reference names.
Methods
generate
A Pydantic core schema.
The mode in which to generate the schema. Either
'validation' or 'serialization'.A JSON schema representing the specified schema.
generate_definitions
A sequence of tuples, where:
- The first element is a JSON schema key type
- The second element is the JSON mode: either ‘validation’ or ‘serialization’
- The third element is a core schema
A tuple where:
- The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and whose values are the JSON schema corresponding to that pair of inputs
- The second element is a dictionary whose keys are definition references for the JSON schemas from the first returned element, and whose values are the actual JSON schema definitions
model_json_schema
The model class to generate a JSON Schema for.
If
True (the default), fields will be serialized according to their alias. If False, fields will be serialized according to their attribute name.The template to use for generating JSON Schema references.
The format to use when combining schemas from unions together.
The class to use for generating the JSON Schema.
The mode to use for generating the JSON Schema. Either
'validation' or 'serialization'.The generated JSON Schema.
Example
models_json_schema
A sequence of tuples of the form (model, mode).
Whether field aliases should be used as keys in the generated JSON Schema.
The title of the generated JSON Schema.
The description of the generated JSON Schema.
The reference template to use for generating JSON Schema references.
The format to use when combining schemas from unions together.
The schema generator to use for generating the JSON Schema.
A tuple where:
- The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and whose values are the JSON schema corresponding to that pair of inputs
- The second element is a JSON schema containing all definitions referenced in the first returned element, along with the optional title and description keys
WithJsonSchema
Callable, or types that have an is-instance core schema, without needing to go so far as creating a custom subclass of pydantic.json_schema.GenerateJsonSchema.
The JSON schema to use for this field, or
None to omit the field from the schema.If set, this will only apply to that schema generation mode.