Skip to main content

Add your description here

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.1.tar.gz (63.5 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.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file datamodel_converter-0.0.1.tar.gz.

File metadata

  • Download URL: datamodel_converter-0.0.1.tar.gz
  • Upload date:
  • Size: 63.5 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.1.tar.gz
Algorithm Hash digest
SHA256 e558bc56b27fe16c49b389c380cba331d8972e60cc030dec25244416907c0159
MD5 b44db4797c12b5df5c2b2cd9c5c69a7d
BLAKE2b-256 95213d1a823e3a9d0287b8bab4e3b8895711198c1e3d76f6864ccc226ecc0b33

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_converter-0.0.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for datamodel_converter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 822d56a2f97ce54d799232bac664f1bae1f9b9c888de91301e276ad60ef50075
MD5 069c04a16e383feb1296dfd8545c47ff
BLAKE2b-256 abe009c001bfc36146e5f41306b6156049206d89c74587b50012bd4479cc9fe9

See more details on using hashes here.

Provenance

The following attestation bundles were made for datamodel_converter-0.0.1-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