Skip to main content

model_json_schema

Utility function to generate a JSON Schema for a model.

def model_json_schema(
cls: type[BaseModel] | type[PydanticDataclass],
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
union_format: Literal['any_of', 'primitive_type_array'] = 'any_of',
schema_generator: type[GenerateJsonSchema] = GenerateJsonSchema,
mode: JsonSchemaMode = 'validation'
) - > dict[str, Any]

Utility function to generate a JSON Schema for a model.

Parameters

NameTypeDescription
cls`type[BaseModel]type[PydanticDataclass]`
by_aliasbool = TrueIf True (the default), fields will be serialized according to their alias. If False, fields will be serialized according to their attribute name.
ref_templatestr = DEFAULT_REF_TEMPLATEThe template to use for generating JSON Schema references.
union_formatLiteral['any_of', 'primitive_type_array'] = 'any_of'The format to use when combining schemas from unions together; either 'any_of' for standard keyword combination or 'primitive_type_array' for a type array fallback.
schema_generatortype[GenerateJsonSchema] = GenerateJsonSchemaThe class to use for generating the JSON Schema.
modeJsonSchemaMode = 'validation'The mode to use for generating the JSON Schema, determining if the schema is tailored for validation or serialization.

Returns

TypeDescription
dict[str, Any]The generated JSON Schema.