About the YAML to Python Converter
Loading YAML with yaml.safe_load gives you nested dictionaries, and from that point every attribute access is a string key that no type checker can help with. Generating classes from the file fixes that. This tool parses your YAML, groups mappings that share the same keys, and writes one class per shape with annotations taken from the values it actually saw.
Three output styles are available. dataclass emits plain @dataclass classes with no runtime dependency. Pydantic BaseModel emits classes that validate and coerce on construction, which suits a settings object read at startup. TypedDict emits type declarations for code that keeps working with dictionaries and only wants mypy or pyright to check the keys.
Field names are converted to snake_case, so requestsPerMinute becomes requests_per_minute and retry-after becomes retry_after. Integers, floats, booleans and strings are annotated directly, sequences become List[T], dates resolved by the loader become datetime, and an empty value becomes Optional[Any]. Classes are emitted in dependency order so a nested type is defined before the class that refers to it, with the needed imports at the top. If your data starts as JSON instead, JSON to Python does the same job.
How to use
- Paste the YAML file you want to model.
- Choose dataclass, Pydantic or TypedDict depending on what your code needs.
- Rename the root class if
Rootis not descriptive enough. - Copy the classes or download
model.py.
Common questions
- Which style should I pick?
- Dataclasses for plain internal structures, Pydantic when you want validation and coercion on load, TypedDict when the code stays dictionary based and you only want static checks.
- Are the fields optional?
- No. Every key seen in the YAML is emitted as a required annotation. Add defaults by hand for keys that may be absent.
- Why is a field typed Optional[Any]?
- The value was empty in the source, so there was no type to infer. Replace it once you know what belongs there.
- Does it keep the original key names?
- The class fields are snake_case. If your loader needs the original spelling, add an alias in Pydantic or map the keys yourself.