Convert Pydantic models to OpenAI output schema, OpenAI tool schema, and more.
Project description
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).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datamodel_converter-0.0.2.tar.gz.
File metadata
- Download URL: datamodel_converter-0.0.2.tar.gz
- Upload date:
- Size: 64.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11603ff5b92ac3d7bb961cc4711921bf290079074d147fbd48695cda69c247b7
|
|
| MD5 |
90db099290b8899a33d8d49c59210e1b
|
|
| BLAKE2b-256 |
12b839d2a2f7d423acbc32e1b852b78ba67d8f3d1cd62558159e77c8f7285b8e
|
Provenance
The following attestation bundles were made for datamodel_converter-0.0.2.tar.gz:
Publisher:
ci.yml on ShaojieJiang/datamodel-converter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datamodel_converter-0.0.2.tar.gz -
Subject digest:
11603ff5b92ac3d7bb961cc4711921bf290079074d147fbd48695cda69c247b7 - Sigstore transparency entry: 200054163
- Sigstore integration time:
-
Permalink:
ShaojieJiang/datamodel-converter@e3d3db7f59decde4a4bd9e6915cdb4e4f30320eb -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/ShaojieJiang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@e3d3db7f59decde4a4bd9e6915cdb4e4f30320eb -
Trigger Event:
push
-
Statement type:
File details
Details for the file datamodel_converter-0.0.2-py3-none-any.whl.
File metadata
- Download URL: datamodel_converter-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f5d23ecc90894109466eb5b1038dd1a4148fc84cb38a526566791260abba247
|
|
| MD5 |
f93b0873f30f5a7c8ae41af50d6f0dc7
|
|
| BLAKE2b-256 |
4cf1e04e01bdb37164f3036602bdbdaa2dd91c142865ce65a66b65ca302c1d93
|
Provenance
The following attestation bundles were made for datamodel_converter-0.0.2-py3-none-any.whl:
Publisher:
ci.yml on ShaojieJiang/datamodel-converter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datamodel_converter-0.0.2-py3-none-any.whl -
Subject digest:
1f5d23ecc90894109466eb5b1038dd1a4148fc84cb38a526566791260abba247 - Sigstore transparency entry: 200054164
- Sigstore integration time:
-
Permalink:
ShaojieJiang/datamodel-converter@e3d3db7f59decde4a4bd9e6915cdb4e4f30320eb -
Branch / Tag:
refs/tags/v0.0.2 - Owner: https://github.com/ShaojieJiang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@e3d3db7f59decde4a4bd9e6915cdb4e4f30320eb -
Trigger Event:
push
-
Statement type: