Skip to main content

Generate Python async clients from OpenAPI specs with ExternalService pattern

Project description

openapi-client-gen

Generate Python async API clients from OpenAPI 3.x specifications with Pydantic v2 support.

Features

  • Generates Pydantic v2 models from OpenAPI schemas
  • Creates async service classes extending ExternalService
  • Handles query parameters, path parameters, and request bodies
  • Supports custom Jinja2 templates for service generation
  • Automatic enum value normalization (lowercase)
  • Handles Python reserved words in parameter names
  • Deduplicates operation names automatically

Installation

pip install openapi-client-gen

For full schema generation (recommended):

pip install openapi-client-gen[schemas]

Quick Start

CLI Usage

openapi-client-gen -i openapi.json -o ./client

openapi-client-gen -i openapi.json -o ./client --no-lowercase-enums

openapi-client-gen -i openapi.json -o ./client --template my_service.jinja2

Programmatic Usage

from pathlib import Path
from openapi_client_gen import parse_openapi, generate_schemas, generate_service

spec = parse_openapi(Path("openapi.json"))

generate_schemas(
    spec_path=Path("openapi.json"),
    output_path=Path("client/schemas.py"),
    lowercase_enums=True,
)

generate_service(
    spec=spec,
    output_path=Path("client/service.py"),
)

Generated Code Structure

client/
  __init__.py
  schemas.py
  service.py

Requirements

  • Python 3.10+
  • external_service library (for generated clients)
  • jinja2 (for template rendering)
  • pydantic>=2.0 (for schema validation)

Optional

  • datamodel-code-generator (for better schema generation)

API Reference

parse_openapi(spec_path: Path) -> ParsedSpec

Parse an OpenAPI JSON file and return a structured representation.

generate_schemas(spec_path: Path, output_path: Path, lowercase_enums: bool = True)

Generate Pydantic models from OpenAPI schemas.

generate_service(spec: ParsedSpec, output_path: Path, template_path: Path | None = None)

Generate an async service class from parsed specification.

Custom Templates

You can provide custom Jinja2 templates for service generation. The template receives:

  • operations: List of Operation objects
  • schema_imports: List of schema names to import
  • title: API title
  • version: API version

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openapi_client_gen-0.3.0.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_client_gen-0.3.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file openapi_client_gen-0.3.0.tar.gz.

File metadata

  • Download URL: openapi_client_gen-0.3.0.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for openapi_client_gen-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7696f274a1586c375755b69fdbdde1da656938659882a72c908dbdaca8f0a3ea
MD5 e41b4a8d2001df4ac5341b07e8809f5b
BLAKE2b-256 7d212c876d59cba524340a68d6c9241b05fdf304f71647fb65c75aaf06aa13d0

See more details on using hashes here.

File details

Details for the file openapi_client_gen-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_client_gen-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d28c8bd5b36868502318e29c6386cef82d88d55cde944152b7a36c2b4c9294a
MD5 cc6ffe406d558f48119224513e55e6d2
BLAKE2b-256 bbd62a6f0709689e6a97e9f29f05fb67f7b9b38b14beb3b8def1e63c58f75fcb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page