YAML to Pydantic converter
Convert YAML to Pydantic v2 models from a sample YAML file, for typed config loading in Python: load the YAML, then validate it with Root.model_validate(data) (Root is the default name; change it in the options).
What you get
- A Pydantic model from YAML: one BaseModel per nested object, to load a YAML config to Pydantic with full typing.
- Run with the real pydantic v2 in your browser: your YAML data must pass the models.
- YAML read strictly first: duplicate keys are errors, and anchors and merge keys are expanded.
- Typed settings: use the models like Pydantic settings from YAML, with every key and type checked when the app starts.
- Private and free: everything runs in your browser, nothing is uploaded, and there is no sign-up.
How each result is checked
- Step 1: Your YAML is parsed strictly as YAML 1.2: duplicate keys and syntax errors are rejected with the line number.
- Step 1: The JSON output is parsed back and compared with the parsed YAML, value by value.
- Step 2: Your JSON is validated by the generated models with the real pydantic 2 (CPython in WebAssembly, in your browser).
- Step 2: Test values built from the inferred schema are validated by pydantic and by a reference validator (Ajv); they must agree, apart from the differences listed in the notes.
What can't convert exactly
- Anchors (&), aliases (*) and merge keys (<<) are expanded into full copies, because JSON has no references.
- Comments and custom tags such as !Ref are not data, so they are not carried over. You get a warning when this happens.
- Several documents separated by --- become one JSON array.
- Types come from one sample. A field that is null in the sample can only be typed None; add a sample with a real value for a better type.
- The first run downloads Python and pydantic (about 6 MB compressed), once.
Questions
How is a two-step conversion checked?
Your input is converted to JSON first, then to the target format. Each step has its own independent check, and both must pass. The checks of both steps are listed under every result.
Is my data uploaded?
No. Everything runs in your browser, in a background worker. Nothing is sent to a server.
How do I load a YAML config into Pydantic?
Read the file with a YAML library (for example yaml.safe_load), then pass the result to Root.model_validate(data). The models generated here type every key, so a missing or mistyped setting fails at start-up with a clear message.
How do I convert YAML to Pydantic online?
Paste your YAML or open a file on the left (or click Example). The Pydantic appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.
Can the Pydantic output be wrong?
Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.