YAML to Python
Paste YAML and instantly generate Python dataclass definitions with inferred field types for every nested object and list.
Example
Input YAML:
name: Ada
age: 36
address:
city: London
zip: null
Generated Python:
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional, Any
@dataclass
class Address:
city: str
zip: Optional[Any]
@dataclass
class Root:
name: str
age: int
address: Address
How it works
It parses your YAML with js-yaml, walks the resulting value to infer a type model, then emits Python @dataclass definitions (root named Root) with typing hints like List, Optional, Dict, and Any.
Good to know
YAML to Python takes a YAML document and turns it into ready-to-paste Python @dataclass definitions, guessing the type of each field from the actual values you provide. It is aimed at Python developers who already have a config file, an API response, or a fixture in YAML and want a typed model to load it into, rather than hand-writing classes and typing imports from scratch.
It is most useful when you are wiring up a config loader, sketching a schema for a new service, or producing typed stubs for data you only have as a sample. Because every nested mapping becomes its own dataclass (the outer one is always named Root) and list element types are inferred, you get a structured starting point in seconds instead of tracing the shape of the YAML by eye.
Read the output as a draft you confirm against your real data, not a guaranteed contract. The types reflect only the sample you pasted, so:
- A
null shows up as Optional[Any] and an empty list as List[Any] because the tool has nothing concrete to infer from. - A field that happens to be a whole number becomes
int even if it could legitimately be a float elsewhere, and a list with varied items becomes List[Union[...]].
A practical tip: feed it the richest, most complete sample you have, including optional and populated fields, so inference has real values to work with, then widen any Any or over-narrow types by hand. Note that the dataclasses are emitted in dependency order with from __future__ import annotations, so forward references resolve cleanly, but the field names are sanitized to valid Python identifiers and may differ from the original YAML keys.
Frequently asked questions
What Python output style does it generate?
It emits @dataclass definitions with typing hints (List, Optional, Dict, Union, Any). Nested mappings become their own dataclasses, and the top-level mapping is named Root.
How are mixed or empty lists handled?
A list with one element type becomes List[T]; mixed element types become List[Union[...]]. An empty list infers List[Any], and a null value infers Optional[Any].
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
What is a Python dataclass and why generate one from YAML?
A dataclass is a Python class decorated with @dataclass that automatically gets an __init__ and other boilerplate based on its typed fields. Generating one from YAML gives you a typed structure to deserialize the YAML into, with autocomplete and static type checking support.
Does this tool actually load the YAML into the generated dataclasses?
No. It only emits the class definitions as text; it does not produce parsing or deserialization code. You would still need a library such as PyYAML, dacite, or pydantic to populate the dataclasses from your YAML at runtime.
How does it name the generated classes?
The top-level mapping is always named Root, and each nested mapping is named from its key, converted to PascalCase. List item types are singularized where possible (for example a 'projects' list yields a 'Project' class), and duplicate names are de-duplicated with a numeric suffix.
Why did a number in my YAML become int instead of float?
The tool infers float only when the sampled value has a fractional part; a whole number like 36 is treated as int. If a field can hold decimals, you may need to change the type to float manually after generating.
Can it handle a YAML file whose top level is a list or a single value?
Yes. If the top-level YAML is a list it aliases Root to the inferred list type (for example Root = List[Item]), and if it is a single scalar it emits Root = the scalar type, such as Root = str.
Will it work with multi-document YAML files?
It parses a single document using js-yaml's load, so only the first or sole document is processed. Files with multiple documents separated by '---' should be split and converted one document at a time.
How is this different from using pydantic or dacite for YAML?
This tool only generates the static dataclass definitions from a sample; pydantic and dacite are runtime libraries that validate and construct objects from data. The generated dataclasses can serve as the target models that those libraries then populate.
Can dataclass field types include Optional or Union automatically?
Yes. A null value produces Optional[Any], and a list containing mixed element types produces List[Union[...]] listing each distinct inferred type. The required typing imports are added automatically based on which constructs appear.
Related tools