Loading OpenAPI to Python Converter...
Please wait a moment

How to Convert OpenAPI to Python Pydantic Models - Step by Step Guide

Generate type-safe Pydantic v2 models from any OpenAPI 3.0 or Swagger 2.0 specification

Step 1

Paste Your OpenAPI Specification

Paste your OpenAPI 3.0 or Swagger 2.0 spec in JSON or YAML. The converter reads all schemas from components/schemas and generates a Pydantic model class for each one:

Paste directly: Copy your spec from Swagger Editor, your API gateway, or spec files in your project
Upload a file: Load an openapi.json, swagger.yaml, or .yml file directly from disk
Try the sample: Click "Sample" to load an E-Commerce API spec with Products, Orders, and enums

Example: OpenAPI Schema with Enum and Array

components:
  schemas:
    Product:
      type: object
      required: [id, name, price]
      properties:
        id: { type: integer }
        name: { type: string }
        currency:
          type: string
          enum: [USD, EUR, GBP]
Step 2

Auto-Generated Pydantic v2 Models

Each OpenAPI schema becomes a Pydantic v2 BaseModel class with proper Python types. Optional fields default to None, enums become Literal types:

Example: Generated Python Output

from typing import Optional, List, Literal
from pydantic import BaseModel

class Product(BaseModel):
    id: int
    name: str
    price: float
    currency: Optional[Literal['USD', 'EUR', 'GBP']] = None
    tags: Optional[List[str]] = None
    category: Optional[Category] = None
Step 3

Download and Use in Your Python Project

Download as a models.py file and import it in your project. The models work with any Python web framework:

FastAPI: Use as request/response body types — @app.post("/products", response_model=Product)
Validation: Product.model_validate(data) raises clear errors on invalid input
For full client generation: Use openapi-python-client for typed async API clients

What is OpenAPI to Python Conversion?

OpenAPI to Python conversion takes the schema definitions in an OpenAPI 3.0 or Swagger 2.0 spec and generates native Pydantic v2 model classes. This ensures your Python code stays in sync with the API contract — when the spec changes, regenerating the models catches type mismatches at runtime.

Each OpenAPI object schema becomes a Pydantic BaseModel subclass. Fields listed in the required array are non-optional; all others are typed as Optional[T] = None. OpenAPI enum arrays become Literal['a', 'b'] types, array schemas become List[T], and $ref schemas reference other generated model classes.

For production workflows, openapi-python-client generates fully typed async clients as part of your build pipeline. This converter is ideal for quick model generation and exploration. Validate your spec first with our OpenAPI Validator.

Frequently Asked Questions

Which Pydantic version does this generate?

The generated code targets Pydantic v2. It uses BaseModel, from __future__ import annotations for forward references, and model_rebuild() to resolve circular model references — all v2 patterns.

How are enums handled?

OpenAPI enum arrays are converted to Python Literal types. For example, enum: [USD, EUR, GBP] becomes Literal['USD', 'EUR', 'GBP'] — giving you validation and IDE autocomplete on all allowed values.

How are $ref references resolved?

$ref: '#/components/schemas/User' becomes the Python class name User. Since all models are generated in the same file with from __future__ import annotations, forward references work automatically. model_rebuild() calls at the bottom resolve any remaining circular references.

Does it support Swagger 2.0?

Yes. For Swagger 2.0 specs, schemas are read from the definitions object instead of components/schemas. The conversion logic is identical. Use our Swagger to OpenAPI converter first if you want to upgrade your spec.

Can I use these models with FastAPI?

Yes. FastAPI is built on Pydantic and uses these models natively as request body types, response models, and query parameters. Simply import the generated models and annotate your endpoint functions.

Is this converter free?

Yes, completely free with no limits on spec size or usage. No registration required. All conversion runs in your browser — your spec never leaves your device.