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_servicelibrary (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 ofOperationobjectsschema_imports: List of schema names to importtitle: API titleversion: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openapi_client_gen-0.1.0.tar.gz.
File metadata
- Download URL: openapi_client_gen-0.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba29b33a822bbe20295c89875e83dd160586f49117286688116d91ed307564d2
|
|
| MD5 |
3a696fa302e709bd8487f09d1f26467f
|
|
| BLAKE2b-256 |
dccf2fc83fc074b2d1a777493bb7896922785da5c9b7aa38058b14626b25328e
|
File details
Details for the file openapi_client_gen-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openapi_client_gen-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae7116b59c6432f7d4291da46a6bf6fb799dcb8c9e5281230f8f43d4a9cd9f28
|
|
| MD5 |
25e6c99dcda9eb24ea20bca7ad70fc77
|
|
| BLAKE2b-256 |
c1d405af24b399f4c75f3bc07c124708cba9bab3e920a9443cd4962d3eb8bcfa
|