YAML → Pydantic Model Generator

Paste a YAML data structure and get a Pydantic model class with inferred Python types. Supports nested objects, lists, optional fields, and both v1/v2 syntax.

v2: Uses model_config = ConfigDict(...) for settings.

from pydantic import BaseModel, ConfigDict

class Tags(BaseModel):
    model_config = ConfigDict(extra="forbid")

class Address(BaseModel):
    model_config = ConfigDict(extra="forbid")

    street: str
    city: str
    zip: int

class MyModel(BaseModel):
    model_config = ConfigDict(extra="forbid")

    name: str
    age: int
    email: str
    is_active: bool
    score: float
    tags: Tags
    address: Address

Type Inference Rules

YAML stringstr
YAML integerint
YAML floatfloat
YAML booleanbool
YAML listList[T]
YAML nested objectNested class

Free online YAML to Pydantic model generator with nested object support and v1/v2 syntax

Manually writing Pydantic model classes from a YAML schema or API response sample is tedious and error-prone, especially for deeply nested structures. The YAML to Pydantic Generator on AlteredIdea automates this process: paste any YAML, and get a complete, import-ready Pydantic model in seconds. The generator infers Python types from YAML values, handles nested objects by creating separate model classes, maps arrays to correctly typed List[T] fields, and outputs either Pydantic v1 or v2 syntax based on your selection.

This tool is particularly useful for FastAPI developers who need to quickly scaffold request and response models from API documentation samples, data engineers building data pipeline schemas, and anyone working with LLM structured output validation where Pydantic is the standard choice. All conversion runs in your browser: your YAML schema is never sent to a server.

Step-by-step guide

  1. 1
    Paste your YAML
    Copy any YAML data structure: a config file, API response, data schema, or sample record: and paste it into the YAML Input panel. Click 'Load sample' to see an example.
  2. 2
    Set the class name
    Enter a name for the root Pydantic model class in the Class Name field. Use PascalCase (e.g. UserProfile, OrderItem). Nested objects automatically get their own class names derived from the YAML key.
  3. 3
    Choose Pydantic version
    Select v1 or v2. v1 generates a class Config inner class; v2 uses model_config = ConfigDict(...). Choose based on the Pydantic version installed in your project.
  4. 4
    Toggle optional fields
    Enable the Optional fields checkbox to wrap all field types in Optional[...] with a default of None. Useful when your YAML represents a partial schema or when fields may be absent at runtime.
  5. 5
    Copy the generated model
    The model updates in real time. Click Copy to save the generated Pydantic model code to your clipboard, ready to paste into your Python project.

Related Tools

Frequently Asked Questions

What is Pydantic?
Pydantic is a Python data validation library that uses Python type annotations to define data models. It is widely used for validating API request/response schemas, configuration management, and structured output parsing from LLMs. FastAPI uses Pydantic models for all request and response validation.
What is the difference between Pydantic v1 and v2?
Pydantic v2 (released 2023) is a complete rewrite with significantly better performance. Key syntax differences: v1 uses an inner class Config for settings, while v2 uses model_config = ConfigDict(...). v2 also introduces stricter validation by default. This tool generates correct syntax for both versions.
How are Python types inferred from YAML?
The tool maps YAML types to Python types as follows: YAML string becomes str, integer becomes int, float becomes float, boolean becomes bool, null becomes None (or Optional), list becomes List[T] where T is inferred from the first element, and nested YAML objects become a separate nested BaseModel class.
What does the extra=forbid setting do?
The model_config = ConfigDict(extra='forbid') setting in v2 (and class Config: extra = 'forbid' in v1) causes Pydantic to raise a validation error if any extra fields are present in the input data that are not defined in the model. This is useful for strict API validation.
Can I generate models for deeply nested YAML?
Yes. The tool recursively generates nested Pydantic model classes for each nested YAML object. Each nested object gets its own named class (derived from the YAML key name), and all nested classes are output before the root class in the generated code.
What import statements are generated?
The tool automatically generates the correct imports based on the types used. It always imports BaseModel from pydantic. If v2 is selected, it also imports ConfigDict. Optional and List are imported from typing when needed, and Any is imported when empty lists are present.
Does this work with YAML arrays?
Yes. YAML arrays are mapped to List[T] where T is inferred from the first element in the array. Empty arrays map to List[Any]. If the array contains objects, the items are typed as a nested model class.
Can I use this for FastAPI request/response models?
Yes. Pasting a sample request or response body in YAML format and converting it to a Pydantic model gives you a ready-to-use schema for FastAPI route definitions. The generated model includes field type annotations that FastAPI uses for automatic OpenAPI documentation.
What YAML features are not supported?
The parser handles the most common YAML patterns: key-value pairs, nested objects, and simple lists. It does not support YAML anchors (&), aliases (*), multi-document YAML (---), or complex merge keys. For advanced YAML schemas, use a full YAML parser like PyYAML in your local environment.

AlteredIdea vs alternatives

vs ChatGPT / LLM APIs: LLMs can generate Pydantic models but may hallucinate field names or types. AlteredIdea deterministically infers types from your actual YAML values with no hallucination risk.

vs browser extensions: No install, no permissions, works on any device.

vs paid tools: Completely free, no account required.