OpenAPI to Pydantic converter
Convert OpenAPI to Pydantic v2 models from a 3.0/3.1 or Swagger 2.0 spec (YAML or JSON): one model per component schema, with allOf as inheritance and nullable fields as Optional.
What you get
- Pydantic v2 models for every schema in an OpenAPI or Swagger spec: ready-made FastAPI models from OpenAPI, or models for an httpx client.
- Run with the real pydantic and compared with a reference validator.
- Swagger to Pydantic online: Swagger 2.0 specs work the same as OpenAPI 3.0 and 3.1, in YAML or JSON.
- Private and free: everything runs in your browser, nothing is uploaded, and there is no sign-up.
How each result is checked
- The models are run with the real pydantic 2 in your browser (CPython in WebAssembly).
- The examples in your spec, plus valid and broken test values for every 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
- OpenAPI 3.0 nullable: true is read as "null is also allowed". In OpenAPI 3.0, keywords next to a $ref are ignored, as the specification says.
- The first run downloads Python and pydantic (about 6 MB compressed), once.
Questions
Does it generate an API client?
No, only the data models. They work with FastAPI, httpx, or any code that reads or writes the API's JSON.
Does Swagger to Pydantic work too?
Yes. Swagger 2.0 documents are read the same way as OpenAPI 3.0 and 3.1, in YAML or JSON.
How do I use the models in FastAPI?
Save the code as models.py and use the classes as request bodies and response_model types in your FastAPI routes. The same models validate responses in an httpx or requests client.
How do I convert OpenAPI to Pydantic online?
Paste your OpenAPI 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.