Datamodel Converter
Every time I need to specify output schema for LLMs, I need to write a converter from Pydantic models to the schema.
Pydantic V2's model_json_schema is not supported by some platforms like OpenAI or n8n.
This package provides a converter for this purpose.
Installation
pip install datamodel-converter
Example
import json
from pydantic import BaseModel
from datamodel_converter.pydantic_converter import pydantic_converter
class Address(BaseModel):
"""Address model."""
street: str
city: str
state: str
zip: str
class Person(BaseModel, use_attribute_docstrings=True):
"""Person model."""
name: str
age: int
addresses: list[Address]
"""Person might have multiple addresses."""
print("Pydantic schema:")
print(json.dumps(Person.model_json_schema(), indent=2))
print()
print("OpenAI output schema:")
print(json.dumps(pydantic_converter(Person, flavor="openai_output_schema"), indent=2))
print()
Output:
Pydantic schema:
{
"$defs": {
"Address": {
"description": "Address model.",
"properties": {
"street": {
"title": "Street",
"type": "string"
},
"city": {
"title": "City",
"type": "string"
},
"state": {
"title": "State",
"type": "string"
},
"zip": {
"title": "Zip",
"type": "string"
}
},
"required": [
"street",
"city",
"state",
"zip"
],
"title": "Address",
"type": "object"
}
},
"description": "Person model.",
"properties": {
"name": {
"title": "Name",
"type": "string"
},
"age": {
"title": "Age",
"type": "integer"
},
"addresses": {
"description": "Person might have multiple addresses.",
"items": {
"$ref": "#/$defs/Address"
},
"title": "Addresses",
"type": "array"
}
},
"required": [
"name",
"age",
"addresses"
],
"title": "Person",
"type": "object"
}
OpenAI output schema:
{
"description": "Person model.",
"name": "Person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"addresses": {
"description": "Person might have multiple addresses.",
"items": {
"description": "Address model.",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"zip": {
"type": "string"
}
},
"required": [
"street",
"city",
"state",
"zip"
],
"type": "object",
"additionalProperties": false
},
"type": "array"
}
},
"additionalProperties": false,
"required": [
"name",
"age",
"addresses"
]
}
}
Related works
- datamodel-code-generator: Generate Pydantic models from JSON Schema (opposite direction of this package).
Metadata
Release files for datamodel-converter 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| datamodel_converter-0.0.2.tar.gz | 64.4 kB | With provenance | Details for datamodel_converter-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| datamodel_converter-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for datamodel_converter-0.0.2-py3-none-any.whl |
Total release size: 69.6 kB
Release files / datamodel_converter-0.0.2.tar.gz
| Download URL | datamodel_converter-0.0.2.tar.gz |
|---|---|
| Size | 64.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
11603ff5b92ac3d7bb961cc4711921bf290079074d147fbd48695cda69c247b7
|
|
BLAKE2b-256 checksum How to use checksums |
12b839d2a2f7d423acbc32e1b852b78ba67d8f3d1cd62558159e77c8f7285b8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2025.
Transparency logRelease files / datamodel_converter-0.0.2-py3-none-any.whl
| Download URL | datamodel_converter-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f5d23ecc90894109466eb5b1038dd1a4148fc84cb38a526566791260abba247
|
|
BLAKE2b-256 checksum How to use checksums |
4cf1e04e01bdb37164f3036602bdbdaa2dd91c142865ce65a66b65ca302c1d93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2025.
Transparency logSecurity for datamodel-converter 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for datamodel_converter-0.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 21, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.