Pydantic Generator¶
Generate Pydantic models from JSON Schema.
Overview¶
API Reference¶
Input Functions¶
from_dict
¶
Convert a JSON schema dictionary to a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
The JSON schema as a dictionary (must contain "version" field) |
required |
model_name
|
str
|
Name for the generated Pydantic model class |
'DynamicModel'
|
Returns:
| Type | Description |
|---|---|
Type[BaseModel]
|
A Pydantic model class |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema does not have a "version" field |
Example
schema = { ... "type": "object", ... "version": "1.0.0", ... "properties": { ... "name": {"type": "string", "description": "Person's name"}, ... "age": {"type": "integer", "description": "Person's age"} ... }, ... "required": ["name"] ... } Person = from_dict(schema, "Person") person = Person(name="Alice", age=30) person.name 'Alice' person.age 30
Source code in src/pycharter/pydantic_generator/converter.py
from_file
¶
Load a JSON schema from a file and convert it to a Pydantic model.
Supports both JSON (.json) and YAML (.yaml, .yml) file formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the schema file (JSON or YAML, must contain "version" field) |
required |
model_name
|
str | None
|
Name for the generated Pydantic model class. If None, uses the file stem as the model name. |
None
|
Returns:
| Type | Description |
|---|---|
Type[BaseModel]
|
A Pydantic model class |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If schema does not have a "version" field or format is unsupported |
Example
JSON file¶
Person = from_file("schema.json", "Person")
YAML file¶
Person = from_file("schema.yaml", "Person")
Source code in src/pycharter/pydantic_generator/converter.py
from_json
¶
Convert a JSON schema string to a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str
|
The JSON schema as a string (must contain "version" field) |
required |
model_name
|
str
|
Name for the generated Pydantic model class |
'DynamicModel'
|
Returns:
| Type | Description |
|---|---|
Type[BaseModel]
|
A Pydantic model class |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema does not have a "version" field |
Example
schema_json = ''' ... { ... "type": "object", ... "version": "1.0.0", ... "properties": { ... "name": {"type": "string"} ... } ... } ... ''' Person = from_json(schema_json, "Person")
Source code in src/pycharter/pydantic_generator/converter.py
from_url
¶
Load a JSON schema from a URL and convert it to a Pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to the JSON schema (must contain "version" field) |
required |
model_name
|
str
|
Name for the generated Pydantic model class |
'DynamicModel'
|
Returns:
| Type | Description |
|---|---|
Type[BaseModel]
|
A Pydantic model class |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema does not have a "version" field |
Example
Load schema from a URL (must have version field)¶
Person = from_url("https://example.com/schema.json", "Person")
Source code in src/pycharter/pydantic_generator/converter.py
Examples¶
from pycharter import from_dict, from_file, from_json
# From dictionary
schema = {
"type": "object",
"version": "1.0.0",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
User = from_dict(schema, "User")
# From file
Product = from_file("schemas/product.json", "Product")
# From JSON string
json_str = '{"type": "object", "properties": {...}}'
Model = from_json(json_str, "Model")
# Use the model
user = User(name="Alice", age=30)
print(user.model_dump())