🌐
Pydantic
docs.pydantic.dev › latest › integrations › datamodel_code_generator
datamodel-code-generator - Pydantic Validation
The datamodel-code-generator project is a library and command-line utility to generate pydantic models from just about any data source, including:
🌐
GitHub
github.com › koxudaxi › datamodel-code-generator
GitHub - koxudaxi/datamodel-code-generator: Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV). · GitHub
pip install 'datamodel-code-generator[http]' datamodel-codegen --url https://example.com/api/openapi.yaml --output model.py · [tool.datamodel-codegen] input = "schema.yaml" output = "src/models.py" output-model-type = "pydantic_v2.BaseModel"
Starred by 3.9K users
Forked by 431 users
Languages   Python
🌐
Pydantic
pydantic.dev › docs › validation › latest › integrations › dev-tools › datamodel_code_generator
datamodel-code-generator | Pydantic Docs
Whenever you find yourself with ... hierarchies on demand. ... In this case, datamodel-code-generator creates pydantic models from a JSON Schema file....
🌐
PyPI
pypi.org › project › datamodel-code-generator › 0.4.8
datamodel-code-generator · PyPI
This code generator creates pydantic model from an openapi file and others. ... usage: datamodel-codegen [-h] [--input INPUT] [--output OUTPUT] [--base-class BASE_CLASS] [--custom-template-dir CUSTOM_TEMPLATE_DIR] [--extra-template-data ...
      » pip install datamodel-code-generator
    
Published   Apr 18, 2020
Version   0.4.8
🌐
Pydantic
docs.pydantic.dev › 1.10 › datamodel_code_generator
Code Generation - Pydantic
The datamodel-code-generator project is a library and command-line utility to generate pydantic models from just about any data source, including:
🌐
GitHub
github.com › koxudaxi › datamodel-code-generator › releases
Releases · koxudaxi/datamodel-code-generator
Python data model generator (Pydantic, dataclasses, TypedDict, msgspec) from OpenAPI, JSON Schema, GraphQL, and raw data (JSON/YAML/CSV). - Releases · koxudaxi/datamodel-code-generator
Author   koxudaxi
🌐
GitHub
github.com › tomercagan › datamodel-code-generator
GitHub - tomercagan/datamodel-code-generator: Pydantic model generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
This code generator creates pydantic model from an openapi file and others. See documentation for more details. ... If you want to resolve $ref for remote files then you should specify http extra option.
Author   tomercagan
🌐
Ubuntu
manpages.ubuntu.com › manpages › resolute › man1 › datamodel-codegen.1.html
Ubuntu Manpage: datamodel-codegen - pydantic code generator from OpenAPI and more
usage: datamodel-codegen [options] Generate Python data models from schema definitions or structured data For detailed usage, see: https://koxudaxi.github.io/datamodel-code-generator
Find elsewhere
🌐
GitHub
github.com › pydantic › pydantic › blob › main › docs › integrations › datamodel_code_generator.md
pydantic/docs/integrations/datamodel_code_generator.md at main · pydantic/pydantic
November 20, 2024 - The datamodel-code-generator project is a library and command-line utility to generate pydantic models from just about any data source, including:
Author   pydantic
🌐
PyPI
pypi.org › project › improved-datamodel-codegen
improved-datamodel-codegen · PyPI
This code generator creates pydantic model from an openapi file and others. See documentation for more details. ... If you want to resolve $ref for remote files then you should specify http extra option.
      » pip install improved-datamodel-codegen
    
Published   Mar 07, 2023
Version   1.2.1
🌐
Koxudaxi
koxudaxi.github.io › datamodel-code-generator › openapi
Generate from OpenAPI - datamodel-code-generator
The code generator can create pydantic models from OpenAPI schema definitions, particularly using the data from the schema field. $ datamodel-codegen --input api.yaml --input-file-type openapi --output model.py ... openapi: "3.0.0" info: version: 1.0.0 title: Swagger Petstore license: name: ...
🌐
Koxudaxi
datamodel-code-generator.koxudaxi.dev
datamodel-code-generator - datamodel-code-generator
datamodel-codegen --input pet.json --input-file-type jsonschema --output-model-type pydantic_v2.BaseModel --output model.py ... # generated by datamodel-codegen: # filename: tutorial_pet.json from __future__ import annotations from enum import Enum from typing import Optional from pydantic import BaseModel, Field class Species(Enum): dog = 'dog' cat = 'cat' bird = 'bird' fish = 'fish' class Pet(BaseModel): name: str = Field(..., description="The pet's name") species: Species age: Optional[int] = Field(None, description='Age in years', ge=0) vaccinated: Optional[bool] = False
🌐
Docker Hub
hub.docker.com › r › koxudaxi › datamodel-code-generator
koxudaxi/datamodel-code-generator - Docker Image
This code generator creates pydantic v1 and v2⁠ model, dataclasses.dataclass⁠, typing.TypedDict⁠ and msgspec.Struct⁠ from an openapi file and others. See documentation⁠ for more details. To install datamodel-code-generator: $ pip install datamodel-code-generator Copy ·
🌐
Pydantic
pydantic.com.cn › en › integrations › datamodel_code_generator
datamodel-code-generator - Pydantic documentation (en)
The datamodel-code-generator project is a library and command-line utility to generate pydantic models from just about any data source, including:
🌐
Reddit
reddit.com › r/python › jsontopydantic - generate pydantic models from json in the browser
r/Python on Reddit: JSONtoPydantic - Generate Pydantic Models from JSON in the browser
December 4, 2020 -

https://jsontopydantic.com

Hi there! I built this over the weekend and figured someone other than myself might find it useful.

Like many who work with REST APIs in Python, I've recently fallen in love with Pydantic. If you haven't heard of Pydantic, it's a data validation and parsing library that makes working with JSON in Python quite pleasant.

I needed a quick way to generate a Pydantic model from any given sample of JSON, and hacked together this application to do so. You can paste in a valid JSON string, and you'll get a valid Pydantic model back.

This is helpful if you're working with poorly documented APIs, really big objects, or have lots of edge cases to catch.

Check it out and let me know what you think!

Code -> https://github.com/brokenloop/jsontopydantic

🌐
Koxudaxi
koxudaxi.github.io › datamodel-code-generator › what_is_the_difference_between_v1_and_v2
Output Model Types - datamodel-code-generator
pip install 'datamodel-code-generator[msgspec]' datamodel-codegen --input schema.json --output-model-type msgspec.Struct --output model.py · from msgspec import Struct, field from typing import Union, UnsetType from msgspec import UNSET class Pet(Struct): id: int name: str tag: Union[str, UnsetType] = UNSET ... graph TD A[Need runtime validation?] -->|Yes| B[Need best performance?] A -->|No| C[Need type hints for dicts?] B -->|Yes| D[msgspec.Struct] B -->|No| E[Pydantic v1 dependency?] E -->|Yes| F[pydantic.BaseModel] E -->|No| G[pydantic_v2.BaseModel] C -->|Yes| H[typing.TypedDict] C -->|No| I[dataclasses.dataclass]
Top answer
1 of 5
11

This has been discussed some time ago and Samuel Colvin said he didn't want to pursue this as a feature for Pydantic.

If you are fine with code generation instead of actual runtime creation of models, you can use the datamodel-code-generator.

To be honest, I struggle to see the use case for generating complex models at runtime, seeing as their main purpose is validation, implying that you think about correct schema before running your program. But that is just my view.

For simple models I guess you can throw together your own logic for this fairly quickly.

If do you need something more sophisticated, the aforementioned library does offer some extensibility. You should be able to import and inherit from some of their classes like the JsonSchemaParser. Maybe that will get you somewhere.

Ultimately I think this becomes non-trivial very quickly, which is why Pydantic's maintainer didn't want to deal with it and why there is a whole separate project for this.

2 of 5
7

Updated @Alon's answer to handle nested modals:

from typing import Any, Type, Optional
from enum import Enum

from pydantic import BaseModel, Field, create_model


def json_schema_to_base_model(schema: dict[str, Any]) -> Type[BaseModel]:
    type_mapping: dict[str, type] = {
        "string": str,
        "integer": int,
        "number": float,
        "boolean": bool,
        "array": list,
        "object": dict,
    }

    properties = schema.get("properties", {})
    required_fields = schema.get("required", [])
    model_fields = {}

    def process_field(field_name: str, field_props: dict[str, Any]) -> tuple:
        """Recursively processes a field and returns its type and Field instance."""
        json_type = field_props.get("type", "string")
        enum_values = field_props.get("enum")

        # Handle Enums
        if enum_values:
            enum_name: str = f"{field_name.capitalize()}Enum"
            field_type = Enum(enum_name, {v: v for v in enum_values})
        # Handle Nested Objects
        elif json_type == "object" and "properties" in field_props:
            field_type = json_schema_to_base_model(
                field_props
            )  # Recursively create submodel
        # Handle Arrays with Nested Objects
        elif json_type == "array" and "items" in field_props:
            item_props = field_props["items"]
            if item_props.get("type") == "object":
                item_type: type[BaseModel] = json_schema_to_base_model(item_props)
            else:
                item_type: type = type_mapping.get(item_props.get("type"), Any)
            field_type = list[item_type]
        else:
            field_type = type_mapping.get(json_type, Any)

        # Handle default values and optionality
        default_value = field_props.get("default", ...)
        nullable = field_props.get("nullable", False)
        description = field_props.get("title", "")

        if nullable:
            field_type = Optional[field_type]

        if field_name not in required_fields:
            default_value = field_props.get("default", None)

        return field_type, Field(default_value, description=description)

    # Process each field
    for field_name, field_props in properties.items():
        model_fields[field_name] = process_field(field_name, field_props)

    return create_model(schema.get("title", "DynamicModel"), **model_fields)

Example Schema

schema = {
    "title": "User",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
        "is_active": {"type": "boolean"},
        "address": {
            "type": "object",
            "properties": {
                "street": {"type": "string"},
                "city": {"type": "string"},
                "zipcode": {"type": "integer"},
            },
        },
        "roles": {
            "type": "array",
            "items": {
                "type": "string",
                "enum": ["admin", "user", "guest"]
            }
        }
    },
    "required": ["name", "age"]
}

Generate the Pydantic model

DynamicModel = json_schema_to_base_model(schema)

Example usage

print(DynamicModel.schema_json(indent=2))