YAML to Python
Generate Python dataclasses, Pydantic models or TypedDicts from a YAML sample.
YAML to Python – Typed configuration models
The YAML to Python converter turns a YAML file into Python classes. It is the fastest way to give a configuration file, a CI definition, a Kubernetes-style manifest or a data fixture a proper typed model, with output as standard dataclasses, Pydantic models or TypedDicts.
How YAML maps to types
The document is parsed with the YAML 1.2 core schema: mappings become classes, sequences become lists, and plain scalars are typed as integers, floats, booleans, null or strings. Anchors and aliases are resolved before inference, comments are ignored, and quoted values such as "0042" stay strings. When a sequence contains several mappings, their keys are merged, so a key present in only some entries becomes optional.
Loading the file
Read the YAML with PyYAML or ruamel.yaml and hand the result to the generated model:
- Pydantic:
Config.model_validate(yaml.safe_load(f))validates types and gives clear error messages for a broken config — ideal for application settings. - dataclasses:
Config.from_dict(yaml.safe_load(f))uses the generated helper and needs no third-party package. - TypedDict: keep the plain dict but let mypy check every key you access.
Keys with dashes or reserved words (max-retries, class) become valid snake_case names, and Pydantic models keep the original key as an alias.
Why type your config?
Untyped config["database"]["port"] lookups fail at runtime and are hard to refactor. A model documents every option, provides autocompletion, and catches typos the moment the file is loaded instead of halfway through a deployment.
Tips
Include every optional key in the sample, even with a placeholder value. Quote values like "08:30" or "0755" in YAML if they must stay strings. For lists of objects, add several entries so missing keys are detected as optional.
Related tools
Check the input with the YAML Validator or convert it to JSON with YAML to JSON.
Frequently Asked Questions
Is my YAML uploaded anywhere?
No. The YAML parser and the code generator both run locally in your browser. You can disconnect from the network after the page loads and the converter keeps working.
Does it understand anchors and aliases?
Yes. Anchors (&name) and aliases (*name) are resolved by the parser before types are inferred, so reused blocks produce the same class.
Which YAML version is used?
YAML 1.2 core schema. Words like yes, no, on and off stay strings (unlike YAML 1.1 parsers such as PyYAML), so quote or replace them with true/false if you rely on PyYAML booleans.
Can I load multi-document files?
The converter reads the first document. Generate a model per document type and load them with yaml.safe_load_all in Python.