Skip to main content

Datamodel Converter

CI Coverage PyPI

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

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)

Source distribution for datamodel-converter 0.0.2
File Size Uploaded
datamodel_converter-0.0.2.tar.gz 64.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for datamodel-converter 0.0.2
File Interpreter ABI Platform
datamodel_converter-0.0.2-py3-none-any.whl Python 3 none any Details

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 log

Release 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 log

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page