A class for generating JSON schemas.
This class generates JSON schemas based on configured parameters. The default schema dialect
is https://json-schema.org/draft/2020-12/schema.
The class uses by_alias to configure how fields with
multiple names are handled and ref_template to format reference names.
Attributes
| Attribute | Type | Description |
|---|
| schema_dialect | string = https://json-schema.org/draft/2020-12/schema | The JSON schema dialect used to generate the schema. |
| ignored_warning_kinds | set[JsonSchemaWarningKind] = {'skipped-choice'} | Warnings to ignore when generating the schema, allowing subclasses to control which warnings are emitted. |
Constructor
Signature
def GenerateJsonSchema(
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
union_format: Literal['any_of', 'primitive_type_array'] = 'any_of'
)
Parameters
| Name | Type | Description |
|---|
| by_alias | bool = True | Whether to use field aliases in the generated schemas. |
| ref_template | str = DEFAULT_REF_TEMPLATE | The format string to use when generating reference names. |
| union_format | Literal['any_of', 'primitive_type_array'] = 'any_of' | The format to use when combining schemas from unions together. |
Methods
mode()
@classmethod
def mode() - > JsonSchemaMode
Determines the current JSON schema mode, prioritizing configuration overrides over the internal state.
Returns
| Type | Description |
|---|
JsonSchemaMode | The active mode, either 'validation' or 'serialization' |
build_schema_type_to_method()
@classmethod
def build_schema_type_to_method() - > dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]]
Builds a dictionary mapping fields to methods for generating JSON schemas.
Returns
| Type | Description |
|---|
dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]] | A dictionary containing the mapping of CoreSchemaOrFieldType to a handler method. |
generate_definitions()
@classmethod
def generate_definitions(
inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]]
) - > tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]
Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a mapping that links the input keys to the definition references.
Parameters
| Name | Type | Description |
|---|
| inputs | Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]] | A sequence of tuples containing the key, mode, and core schema to process |
Returns
| Type | Description |
|---|
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]] | A tuple containing the input-to-schema mapping and the final dictionary of definitions. |
generate()
@classmethod
def generate(
schema: CoreSchema,
mode: JsonSchemaMode = validation
) - > JsonSchemaValue
Generates a JSON schema for a specified schema in a specified mode.
Parameters
| Name | Type | Description |
|---|
| schema | CoreSchema | The Pydantic core schema to convert |
| mode | JsonSchemaMode = validation | The mode in which to generate the schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A JSON schema representing the specified schema. |
generate_inner()
@classmethod
def generate_inner(
schema: CoreSchemaOrField
) - > JsonSchemaValue
Generates a JSON schema for a given core schema.
Parameters
| Name | Type | Description |
|---|
| schema | CoreSchemaOrField | The given core schema or field to process |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
sort()
@classmethod
def sort(
value: JsonSchemaValue,
parent_key: str | None = null
) - > JsonSchemaValue
Override this method to customize the sorting of the JSON schema (e.g., don't sort at all, sort all keys unconditionally, etc.)
Parameters
| Name | Type | Description |
|---|
| value | JsonSchemaValue | The JSON schema dictionary to sort |
| parent_key | `str | None` = null |
Returns
| Type | Description |
|---|
JsonSchemaValue | The sorted JSON schema |
invalid_schema()
@classmethod
def invalid_schema(
schema: core_schema.InvalidSchema
) - > JsonSchemaValue
Placeholder - should never be called.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.InvalidSchema | The invalid core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | None; raises RuntimeError |
any_schema()
@classmethod
def any_schema(
schema: core_schema.AnySchema
) - > JsonSchemaValue
Generates a JSON schema that matches any value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.AnySchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | An empty dictionary representing an 'any' schema |
none_schema()
@classmethod
def none_schema(
schema: core_schema.NoneSchema
) - > JsonSchemaValue
Generates a JSON schema that matches None.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.NoneSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type set to 'null' |
bool_schema()
@classmethod
def bool_schema(
schema: core_schema.BoolSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a bool value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.BoolSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type set to 'boolean' |
int_schema()
@classmethod
def int_schema(
schema: core_schema.IntSchema
) - > JsonSchemaValue
Generates a JSON schema that matches an int value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.IntSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'integer' and applicable numeric validations |
float_schema()
@classmethod
def float_schema(
schema: core_schema.FloatSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a float value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.FloatSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'number' and applicable numeric validations |
decimal_schema()
@classmethod
def decimal_schema(
schema: core_schema.DecimalSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a decimal value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DecimalSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema using regex patterns and numeric constraints to represent decimals |
str_schema()
@classmethod
def str_schema(
schema: core_schema.StringSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a string value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.StringSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'string' and applicable string validations |
bytes_schema()
@classmethod
def bytes_schema(
schema: core_schema.BytesSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a bytes value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.BytesSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'string' and format 'base64url' or 'binary' |
date_schema()
@classmethod
def date_schema(
schema: core_schema.DateSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a date value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DateSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'string' and format 'date' |
time_schema()
@classmethod
def time_schema(
schema: core_schema.TimeSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a time value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TimeSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'string' and format 'time' |
datetime_schema()
@classmethod
def datetime_schema(
schema: core_schema.DatetimeSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a datetime value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DatetimeSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'string' and format 'date-time' |
timedelta_schema()
@classmethod
def timedelta_schema(
schema: core_schema.TimedeltaSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a timedelta value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TimedeltaSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'number' or 'string' depending on configuration |
literal_schema()
@classmethod
def literal_schema(
schema: core_schema.LiteralSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a literal value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.LiteralSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema using 'const' or 'enum' to represent literal values |
missing_sentinel_schema()
@classmethod
def missing_sentinel_schema(
schema: core_schema.MissingSentinelSchema
) - > JsonSchemaValue
Generates a JSON schema that matches the MISSING sentinel value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.MissingSentinelSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | None; raises PydanticOmit |
enum_schema()
@classmethod
def enum_schema(
schema: core_schema.EnumSchema
) - > JsonSchemaValue
Generates a JSON schema that matches an Enum value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.EnumSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with 'enum' values and metadata derived from the Enum class |
is_instance_schema()
@classmethod
def is_instance_schema(
schema: core_schema.IsInstanceSchema
) - > JsonSchemaValue
Handles JSON schema generation for a core schema that checks if a value is an instance of a class.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.IsInstanceSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema or an error |
is_subclass_schema()
@classmethod
def is_subclass_schema(
schema: core_schema.IsSubclassSchema
) - > JsonSchemaValue
Handles JSON schema generation for a core schema that checks if a value is a subclass of a class.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.IsSubclassSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | An empty dictionary for V1 compatibility |
callable_schema()
@classmethod
def callable_schema(
schema: core_schema.CallableSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a callable value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.CallableSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema or an error |
list_schema()
@classmethod
def list_schema(
schema: core_schema.ListSchema
) - > JsonSchemaValue
Returns a schema that matches a list schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ListSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'array' and defined items |
tuple_positional_schema()
@classmethod
def tuple_positional_schema(
schema: core_schema.TupleSchema
) - > JsonSchemaValue
Replaced by tuple_schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TupleSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated tuple schema |
tuple_variable_schema()
@classmethod
def tuple_variable_schema(
schema: core_schema.TupleSchema
) - > JsonSchemaValue
Replaced by tuple_schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TupleSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated tuple schema |
tuple_schema()
@classmethod
def tuple_schema(
schema: core_schema.TupleSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a tuple schema e.g. tuple[int, str, bool] or tuple[int, ...].
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TupleSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'array' using prefixItems or items |
set_schema()
@classmethod
def set_schema(
schema: core_schema.SetSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a set schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.SetSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'array' and uniqueItems set to true |
frozenset_schema()
@classmethod
def frozenset_schema(
schema: core_schema.FrozenSetSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a frozenset schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.FrozenSetSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'array' and uniqueItems set to true |
generator_schema()
@classmethod
def generator_schema(
schema: core_schema.GeneratorSchema
) - > JsonSchemaValue
Returns a JSON schema that represents the provided GeneratorSchema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.GeneratorSchema | The schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'array' representing the generator's output |
dict_schema()
@classmethod
def dict_schema(
schema: core_schema.DictSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a dict schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DictSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'object' using additionalProperties or patternProperties |
function_before_schema()
@classmethod
def function_before_schema(
schema: core_schema.BeforeValidatorFunctionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a function-before schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.BeforeValidatorFunctionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for the input or the inner schema |
function_after_schema()
@classmethod
def function_after_schema(
schema: core_schema.AfterValidatorFunctionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a function-after schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.AfterValidatorFunctionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for the inner schema |
function_plain_schema()
@classmethod
def function_plain_schema(
schema: core_schema.PlainValidatorFunctionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a function-plain schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.PlainValidatorFunctionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for the input or an error |
function_wrap_schema()
@classmethod
def function_wrap_schema(
schema: core_schema.WrapValidatorFunctionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a function-wrap schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.WrapValidatorFunctionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for the input or the inner schema |
default_schema()
@classmethod
def default_schema(
schema: core_schema.WithDefaultSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema with a default value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.WithDefaultSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The inner schema updated with a 'default' key |
get_default_value()
@classmethod
def get_default_value(
schema: core_schema.WithDefaultSchema
) - > Any
Get the default value to be used when generating a JSON Schema for a core schema with a default.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.WithDefaultSchema | The 'with-default' core schema. |
Returns
| Type | Description |
|---|
Any | The default value to use, or NoDefault if no default value is available. |
nullable_schema()
@classmethod
def nullable_schema(
schema: core_schema.NullableSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that allows null values.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.NullableSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A union schema including the inner schema and a null schema |
union_schema()
@classmethod
def union_schema(
schema: core_schema.UnionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that allows values matching any of the given schemas.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.UnionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A union of the generated schemas for each choice |
get_union_of_schemas()
@classmethod
def get_union_of_schemas(
schemas: list[JsonSchemaValue]
) - > JsonSchemaValue
Returns the JSON Schema representation for the union of the provided JSON Schemas.
Parameters
| Name | Type | Description |
|---|
| schemas | list[JsonSchemaValue] | The list of JSON Schemas to be included in the union. |
Returns
| Type | Description |
|---|
JsonSchemaValue | The JSON Schema representing the union of schemas. |
tagged_union_schema()
@classmethod
def tagged_union_schema(
schema: core_schema.TaggedUnionSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that allows values matching any of the given schemas, where the schemas are tagged with a discriminator field that indicates which schema should be used to validate the value.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TaggedUnionSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema using 'oneOf' and an optional 'discriminator' object |
chain_schema()
@classmethod
def chain_schema(
schema: core_schema.ChainSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a core_schema.ChainSchema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ChainSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for the first step (validation) or last step (serialization) |
lax_or_strict_schema()
@classmethod
def lax_or_strict_schema(
schema: core_schema.LaxOrStrictSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that allows values matching either the lax schema or the strict schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.LaxOrStrictSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema for either the strict or lax branch based on configuration |
json_or_python_schema()
@classmethod
def json_or_python_schema(
schema: core_schema.JsonOrPythonSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that allows values matching either the JSON schema or the Python schema.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.JsonOrPythonSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
typed_dict_schema()
@classmethod
def typed_dict_schema(
schema: core_schema.TypedDictSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a typed dict.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TypedDictSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema with type 'object' and defined properties for the typed dict |
typed_dict_field_schema()
@classmethod
def typed_dict_field_schema(
schema: core_schema.TypedDictField
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a typed dict field.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.TypedDictField | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
dataclass_field_schema()
@classmethod
def dataclass_field_schema(
schema: core_schema.DataclassField
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass field.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DataclassField | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
model_field_schema()
@classmethod
def model_field_schema(
schema: core_schema.ModelField
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model field.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ModelField | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
computed_field_schema()
@classmethod
def computed_field_schema(
schema: core_schema.ComputedField
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a computed field.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ComputedField | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
model_schema()
@classmethod
def model_schema(
schema: core_schema.ModelSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ModelSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
resolve_ref_schema()
@classmethod
def resolve_ref_schema(
json_schema: JsonSchemaValue
) - > JsonSchemaValue
Resolve a JsonSchemaValue to the non-ref schema if it is a $ref schema.
Parameters
| Name | Type | Description |
|---|
| json_schema | JsonSchemaValue | The schema to resolve. |
Returns
| Type | Description |
|---|
JsonSchemaValue | The resolved schema. |
model_fields_schema()
@classmethod
def model_fields_schema(
schema: core_schema.ModelFieldsSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a model's fields.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ModelFieldsSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
field_is_present()
@classmethod
def field_is_present(
field: CoreSchemaField
) - > bool
Whether the field should be included in the generated JSON schema.
Parameters
| Name | Type | Description |
|---|
| field | CoreSchemaField | The schema for the field itself. |
Returns
| Type | Description |
|---|
bool | True if the field should be included in the generated JSON schema, False otherwise. |
field_is_required()
@classmethod
def field_is_required(
field: core_schema.ModelField | core_schema.DataclassField | core_schema.TypedDictField,
total: bool
) - > bool
Whether the field should be marked as required in the generated JSON schema.
Parameters
| Name | Type | Description |
|---|
| field | `core_schema.ModelField | core_schema.DataclassField |
| total | bool | Indicates if the TypedDict this field belongs to is total |
Returns
| Type | Description |
|---|
bool | True if the field should be marked as required in the generated JSON schema, False otherwise. |
dataclass_args_schema()
@classmethod
def dataclass_args_schema(
schema: core_schema.DataclassArgsSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass's constructor arguments.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DataclassArgsSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
dataclass_schema()
@classmethod
def dataclass_schema(
schema: core_schema.DataclassSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a dataclass.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DataclassSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
arguments_schema()
@classmethod
def arguments_schema(
schema: core_schema.ArgumentsSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's arguments.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ArgumentsSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
kw_arguments_schema()
@classmethod
def kw_arguments_schema(
arguments: list[core_schema.ArgumentsParameter],
var_kwargs_schema: CoreSchema | None
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's keyword arguments.
Parameters
| Name | Type | Description |
|---|
| arguments | list[core_schema.ArgumentsParameter] | The core schema parameters |
| var_kwargs_schema | `CoreSchema | None` |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
p_arguments_schema()
@classmethod
def p_arguments_schema(
arguments: list[core_schema.ArgumentsParameter],
var_args_schema: CoreSchema | None
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's positional arguments.
Parameters
| Name | Type | Description |
|---|
| arguments | list[core_schema.ArgumentsParameter] | The core schema parameters |
| var_args_schema | `CoreSchema | None` |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
get_argument_name()
@classmethod
def get_argument_name(
argument: core_schema.ArgumentsParameter | core_schema.ArgumentsV3Parameter
) - > str
Retrieves the name of an argument.
Parameters
| Name | Type | Description |
|---|
| argument | `core_schema.ArgumentsParameter | core_schema.ArgumentsV3Parameter` |
Returns
| Type | Description |
|---|
str | The name of the argument. |
arguments_v3_schema()
@classmethod
def arguments_v3_schema(
schema: core_schema.ArgumentsV3Schema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function's arguments.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ArgumentsV3Schema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
call_schema()
@classmethod
def call_schema(
schema: core_schema.CallSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a function call.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.CallSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
custom_error_schema()
@classmethod
def custom_error_schema(
schema: core_schema.CustomErrorSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a custom error.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.CustomErrorSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
json_schema()
@classmethod
def json_schema(
schema: core_schema.JsonSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a JSON object.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.JsonSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
url_schema()
@classmethod
def url_schema(
schema: core_schema.UrlSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a URL.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.UrlSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
multi_host_url_schema()
@classmethod
def multi_host_url_schema(
schema: core_schema.MultiHostUrlSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a URL that can be used with multiple hosts.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.MultiHostUrlSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
uuid_schema()
@classmethod
def uuid_schema(
schema: core_schema.UuidSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a UUID.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.UuidSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
definitions_schema()
@classmethod
def definitions_schema(
schema: core_schema.DefinitionsSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that defines a JSON object with definitions.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DefinitionsSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
definition_ref_schema()
@classmethod
def definition_ref_schema(
schema: core_schema.DefinitionReferenceSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a schema that references a definition.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.DefinitionReferenceSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
ser_schema()
@classmethod
def ser_schema(
schema: core_schema.SerSchema | core_schema.IncExSeqSerSchema | core_schema.IncExDictSerSchema
) - > JsonSchemaValue | None
Generates a JSON schema that matches a schema that defines a serialized object.
Parameters
| Name | Type | Description |
|---|
| schema | `core_schema.SerSchema | core_schema.IncExSeqSerSchema |
Returns
| Type | Description |
|---|
| `JsonSchemaValue | None` |
complex_schema()
@classmethod
def complex_schema(
schema: core_schema.ComplexSchema
) - > JsonSchemaValue
Generates a JSON schema that matches a complex number.
Parameters
| Name | Type | Description |
|---|
| schema | core_schema.ComplexSchema | The core schema |
Returns
| Type | Description |
|---|
JsonSchemaValue | The generated JSON schema. |
get_title_from_name()
@classmethod
def get_title_from_name(
name: str
) - > str
Retrieves a title from a name.
Parameters
| Name | Type | Description |
|---|
| name | str | The name to retrieve a title from. |
Returns
| Type | Description |
|---|
str | The title. |
field_title_should_be_set()
@classmethod
def field_title_should_be_set(
schema: CoreSchemaOrField
) - > bool
Returns true if a field with the given schema should have a title set based on the field name.
Parameters
| Name | Type | Description |
|---|
| schema | CoreSchemaOrField | The schema to check. |
Returns
| Type | Description |
|---|
bool | True if the field should have a title set, False otherwise. |
normalize_name()
@classmethod
def normalize_name(
name: str
) - > str
Normalizes a name to be used as a key in a dictionary.
Parameters
| Name | Type | Description |
|---|
| name | str | The name to normalize. |
Returns
| Type | Description |
|---|
str | The normalized name. |
get_defs_ref()
@classmethod
def get_defs_ref(
core_mode_ref: CoreModeRef
) - > DefsRef
Override this method to change the way that definitions keys are generated from a core reference.
Parameters
| Name | Type | Description |
|---|
| core_mode_ref | CoreModeRef | The core reference. |
Returns
| Type | Description |
|---|
DefsRef | The definitions key. |
get_cache_defs_ref_schema()
@classmethod
def get_cache_defs_ref_schema(
core_ref: CoreRef
) - > tuple[DefsRef, JsonSchemaValue]
This method wraps the get_defs_ref method with some cache-lookup/population logic, and returns both the produced defs_ref and the JSON schema that will refer to the right definition.
Parameters
| Name | Type | Description |
|---|
| core_ref | CoreRef | The core reference to get the definitions reference for. |
Returns
| Type | Description |
|---|
tuple[DefsRef, JsonSchemaValue] | A tuple of the definitions reference and the JSON schema that will refer to it. |
handle_ref_overrides()
@classmethod
def handle_ref_overrides(
json_schema: JsonSchemaValue
) - > JsonSchemaValue
Remove any sibling keys that are redundant with the referenced schema.
Parameters
| Name | Type | Description |
|---|
| json_schema | JsonSchemaValue | The schema to remove redundant sibling keys from. |
Returns
| Type | Description |
|---|
JsonSchemaValue | The schema with redundant sibling keys removed. |
get_schema_from_definitions()
@classmethod
def get_schema_from_definitions(
json_ref: JsonRef
) - > JsonSchemaValue | None
Retrieves the JSON schema associated with a specific reference from the internal definitions dictionary.
Parameters
| Name | Type | Description |
|---|
| json_ref | JsonRef | The JSON reference key to look up |
Returns
| Type | Description |
|---|
| `JsonSchemaValue | None` |
encode_default()
@classmethod
def encode_default(
dft: Any
) - > Any
Encode a default value to a JSON-serializable value.
Parameters
| Name | Type | Description |
|---|
| dft | Any | The default value to encode. |
Returns
| Type | Description |
|---|
Any | The encoded default value. |
update_with_validations()
@classmethod
def update_with_validations(
json_schema: JsonSchemaValue,
core_schema: CoreSchema,
mapping: dict[str, str]
) - > null
Update the json_schema with the corresponding validations specified in the core_schema, using the provided mapping to translate keys in core_schema to the appropriate keys for a JSON schema.
Parameters
| Name | Type | Description |
|---|
| json_schema | JsonSchemaValue | The JSON schema to update. |
| core_schema | CoreSchema | The core schema to get the validations from. |
| mapping | dict[str, str] | A mapping from core_schema attribute names to the corresponding JSON schema attribute names. |
Returns
| Type | Description |
|---|
null | None; the schema is updated in place |
get_flattened_anyof()
@classmethod
def get_flattened_anyof(
schemas: list[JsonSchemaValue]
) - > JsonSchemaValue
Flattens nested 'anyOf' schemas into a single list and deduplicates them.
Parameters
| Name | Type | Description |
|---|
| schemas | list[JsonSchemaValue] | The list of schemas to flatten |
Returns
| Type | Description |
|---|
JsonSchemaValue | A schema containing the flattened and deduplicated anyOf list |
get_json_ref_counts()
@classmethod
def get_json_ref_counts(
json_schema: JsonSchemaValue
) - > dict[JsonRef, int]
Get all values corresponding to the key '$ref' anywhere in the json_schema.
Parameters
| Name | Type | Description |
|---|
| json_schema | JsonSchemaValue | The schema to analyze |
Returns
| Type | Description |
|---|
dict[JsonRef, int] | A dictionary mapping each JSON reference to its occurrence count |
handle_invalid_for_json_schema()
@classmethod
def handle_invalid_for_json_schema(
schema: CoreSchemaOrField,
error_info: str
) - > JsonSchemaValue
Raises a PydanticInvalidForJsonSchema error with details about the unsupported schema type.
Parameters
| Name | Type | Description |
|---|
| schema | CoreSchemaOrField | The schema that cannot be converted |
| error_info | str | A string describing the error context |
Returns
| Type | Description |
|---|
JsonSchemaValue | None; always raises an exception |
emit_warning()
@classmethod
def emit_warning(
kind: JsonSchemaWarningKind,
detail: str
) - > null
This method simply emits PydanticJsonSchemaWarnings based on handling in the warning_message method.
Parameters
| Name | Type | Description |
|---|
| kind | JsonSchemaWarningKind | The kind of warning to emit |
| detail | str | Additional details about the warning |
Returns
render_warning_message()
@classmethod
def render_warning_message(
kind: JsonSchemaWarningKind,
detail: str
) - > str | None
This method is responsible for ignoring warnings as desired, and for formatting the warning messages.
Parameters
| Name | Type | Description |
|---|
| kind | JsonSchemaWarningKind | The kind of warning to render. |
| detail | str | A string with additional details about the warning. |
Returns