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.0.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.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openapi_client_gen-0.2.0.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.0.tar.gz
Algorithm Hash digest
SHA256 8c66381d91e5205023e76c0ec0a887552b82baba82a6b99c91d20243ff35c670
MD5 b437a5142dba5515e80240ae78f07874
BLAKE2b-256 25d04e925bba5d2a5d3953206400c05dd7254f2127adb0809b5d0392b11748b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openapi_client_gen-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c06bae31bc2786d6c2ed53e8ecbb434397709a21d30e4c0b6d9f71bcc73820dc
MD5 f6058f56c623d094670ce434f1d28c3e
BLAKE2b-256 5d608989e0d5b558ed6176d69b3af7d821a4f6575347205bc6add17dd62c4bc9

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