Format Code Now
/
ConvertYAML to Python Next: YAML to TypeScript ›

YAML to Python Class Generator

CV-93

Paste a YAML document and press Convert to get dataclasses, TypedDicts or Pydantic models. The YAML is read into JSON first, so the types come from its values, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

YAML

Python

How to use it

  1. Paste one YAML document, or open a .yaml or .yml file. Use a sample that shows every section you expect.
  2. Choose dataclass, TypedDict or Pydantic BaseModel (v2).
  3. Press Convert, then copy the result or download it as a .py file.
  4. Load the data with json.loads; Pydantic models check it with Root.model_validate(data), while dataclasses and TypedDicts describe its shape. The names follow the JSON form of your YAML, so add the tags your YAML library needs if you load YAML directly.

How the classes are built

By default each object becomes a dataclass; a TypedDict or a Pydantic v2 model can be chosen instead. Types are written as list[...] and Optional[...], numbers become int or float, and a key that is null in some items becomes Optional. In a dataclass, a key missing from some objects gets = None and fields without a default are placed first; mixed types and empty arrays become Any. Keys that are valid Python names are kept as they are, so plain dict access still works.

How the YAML becomes classes

true/false, null, integers and floats get their usual types, while dates stay strings. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the generated types. Comments are dropped, only one document is read, and keys that are not text become text. A value written as 1.0 is read as the number 1, and an integer beyond 9007199254740991 is rounded by the YAML reader. Keys that are not valid Python names, such as first-name, are renamed, and the original key is kept in a comment, a Pydantic alias or the functional TypedDict form.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay str; convert them with datetime, or let Pydantic parse them if you change the type. A YAML file with optional sections should be pasted with every section present, or the missing sections will not appear in the output.

Questions people ask

How do I convert YAML to a Python class?

Paste the YAML into the input box, choose your options and press Convert to get the classes. Load the data with json.loads; Pydantic models check it with Root.model_validate(data), while dataclasses and TypedDicts describe its shape. The classes follow the JSON form of the YAML. The conversion happens in your browser.

Why is a Python field Optional, and when does it get a None default?

A key that is null in some items becomes Optional[...]. A key missing from some objects also gets = None in a dataclass, and fields without defaults are placed first because Python requires that order. This applies to the JSON form of your YAML.

Do the Python dataclasses convert nested dictionaries into classes?

Not by themselves: Root(**data) leaves nested objects as plain dicts. Pydantic models validate and build the nested classes for you, so choose that style if you want nested objects converted when you load data. This applies to the JSON form of your YAML.

Dataclass, TypedDict or Pydantic: which should I pick for the Python output?

Use a dataclass for plain objects, a TypedDict to type dictionaries you already get from json.loads, and Pydantic when you want validation and conversion of nested data. All three describe the same fields. This applies to the JSON form of your YAML.

Are YAML dates and timestamps typed as dates?

No. The core schema is used, so 2026-01-05 stays a string and the generated field is a string type. Change it to a date type by hand if your language has one. The Python classes are written from that converted data.

Does it keep YAML comments, anchors and merge keys?

Comments are dropped because they are not data. Anchors, aliases and << merge keys are resolved, so the values they point to appear in the types. A document that contains an alias to itself is rejected with an error. The Python classes are written from that converted data.

About this tool

The YAML is read with the core schema (true/false, null, integers and floats), converted to JSON, and the code is generated from that. Dates stay text, comments are dropped, anchors, aliases and << merge keys are resolved, keys that are not text become text, and only one document is read. A value such as 1.0 is read as the number 1, so a field that holds whole-looking decimals can come out as an integer type; an integer beyond 9007199254740991 is rounded by the YAML reader before any code is written; .nan and .inf become null. Every object becomes a dataclass by default, or a TypedDict or Pydantic v2 model if you pick one. Keys that are valid Python names are used as they are; others are renamed, and the original key is kept by the TypedDict functional form, by a Pydantic alias, or by a comment in a dataclass. Types are written as list[...] and Optional[...], numbers become int or float, and a key that is null or missing in some items is Optional with a None default. Mixed types, always-null keys and empty arrays become Any. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.