Skip to main content

Convert Pydantic models to OpenAI output schema, OpenAI tool schema, and more.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datamodel_converter-0.0.2.tar.gz (64.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datamodel_converter-0.0.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

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

Hashes for datamodel_converter-0.0.2.tar.gz
Algorithm Hash digest
SHA256 11603ff5b92ac3d7bb961cc4711921bf290079074d147fbd48695cda69c247b7
MD5 90db099290b8899a33d8d49c59210e1b
BLAKE2b-256 12b839d2a2f7d423acbc32e1b852b78ba67d8f3d1cd62558159e77c8f7285b8e

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_converter-0.0.2.tar.gz:

Publisher: ci.yml on ShaojieJiang/datamodel-converter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file datamodel_converter-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for datamodel_converter-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f5d23ecc90894109466eb5b1038dd1a4148fc84cb38a526566791260abba247
MD5 f93b0873f30f5a7c8ae41af50d6f0dc7
BLAKE2b-256 4cf1e04e01bdb37164f3036602bdbdaa2dd91c142865ce65a66b65ca302c1d93

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_converter-0.0.2-py3-none-any.whl:

Publisher: ci.yml on ShaojieJiang/datamodel-converter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page