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.2.1.tar.gz (28.3 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.2.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openapi_client_gen-0.2.1.tar.gz
  • Upload date:
  • Size: 28.3 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.2.1.tar.gz
Algorithm Hash digest
SHA256 66e2be8259796fc5a42f4d06247ad68a1536e5d62b1cf5a3fd0361e97342a4c3
MD5 d49e0e50e4b478182729e0134ace47c2
BLAKE2b-256 82997ff8fd77e2a5efbb59fd4cdfe286cde641c800c6f6a133fc7fa8db8b414c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openapi_client_gen-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c183bb112a8f319fdfbd9fb0072bfc39889ca699b7945af2b1192d6223de906
MD5 b5660b3f74426ce9b3c039c36951ccb0
BLAKE2b-256 6c46675c6bb38105ddf27a5abb42505e7733cc6d88eeb6edec9d1d44a9d3cdf9

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