Converters¶
Turn a Schema into a Pydantic model.
Convert a JSON Schema Schema into a Pydantic model (to_model / SchemaConverter).
SchemaConverter
¶
SchemaConverter(
*,
default_model_name: str = _DEFAULT_MODEL_NAME,
refs: dict[Ref, type[BaseModel]] | None = None,
formats: dict[FormatName, FormatType] | None = None
)
Stateful converter from JSON Schema to Pydantic models.
Initialize converter with optional pre-built refs and format types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_model_name
|
str
|
Fallback name for models without |
_DEFAULT_MODEL_NAME
|
refs
|
dict[Ref, type[BaseModel]] | None
|
Pre-built Pydantic models for |
None
|
formats
|
dict[FormatName, FormatType] | None
|
Format types (a |
None
|
convert_schema
¶
convert_schema(
schema: Schema, /, *, model_name: str | None = None
) -> type[BaseModel]
Convert JSON Schema (root schema) to Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
Schema to convert. |
required |
model_name
|
str | None
|
Name for the generated model. |
None
|
Returns:
| Type | Description |
|---|---|
type[BaseModel]
|
Pydantic model class. |
Raises:
| Type | Description |
|---|---|
SchemaConversionError
|
If schema cannot be converted. |
to_model
¶
to_model(
schema: Schema,
/,
*,
model_name: str | None = None,
refs: dict[Ref, type[BaseModel]] | None = None,
formats: dict[FormatName, FormatType] | None = None,
) -> type[BaseModel]
Convert schema to Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
Schema to convert. |
required |
refs
|
dict[Ref, type[BaseModel]] | None
|
Pre-built reference models. |
None
|
formats
|
dict[FormatName, FormatType] | None
|
Format types (a |
None
|
model_name
|
str | None
|
Name for the generated model. |
None
|
Returns:
| Type | Description |
|---|---|
type[BaseModel]
|
Pydantic model class. |