Autogenerated and fully typed OpenAPI Python clients with a developer-friendly, ergonomic interface.
Project description
openapi-python
openapi-python generates typed Python API clients from OpenAPI specs, with a developer-friendly and ergonomic string-literal-based interface strongly inspired by openapi-typescript.
Installation
uv add openapi-python # If you want to define your own HTTP transport (requests, asyncio, ...)
uv add openapi-python[httpx] # Ships with an `httpx` transport
Client generation
Generate a client from an OpenAPI spec in openapi.json:
# Types + HTTP transport
uv run openapi-python generate --spec ./openapi.json --out ./generated
# Types + custom transport protocol
uv run openapi-python generate --spec ./openapi.json --out ./generated --protocol-only
... or programatically:
from pathlib import Path
from openapi_python import GenerationRequest, generate_client
result = generate_client(
GenerationRequest(
spec_source="./openapi.json",
output_dir=Path("./generated"),
package_name="my_client",
overwrite=True,
)
)
Using generated clients
Generated clients expose route-specific callables with typed params, query, headers, body, and return values.
With the built-in httpx transport:
from generated.my_client import Client
client = Client(base_url="https://api.example.com")
book = client.get("/books/{book_id}")(params={"book_id": 1})
For async APIs:
from generated.my_client import AsyncClient
async_client = AsyncClient(base_url="https://api.example.com")
book = await async_client.get("/books/{book_id}")(params={"book_id": 1})
For protocol-only clients, provide your own transport:
from generated.my_client import Client
client = Client(base_url="https://api.example.com", transport=my_transport)
book = client.get("/books/{book_id}")(params={"book_id": 1})
Extensibility
GeneratorExtensions exposes two safe hooks:
normalize_hooks: transform the normalized model before rendering.render_context_hooks: transform rendered file content map before writing.
Transport Decoupling
Generated clients expose a transport protocol. You can plug in your own transport while keeping route-level typing guarantees.
Use --protocol-only to generate clients that require a supplied transport and do not emit the built-in httpx transport classes. By default, generated clients include DefaultTransport and DefaultAsyncTransport, which require the httpx extra when instantiated.
Protocol typing can be relaxed independently with --no-routes, --no-requests, and --no-responses. Those flags replace the corresponding route literals, request payload types, or response types with broad catch-all types.
Built-in httpx transport
Install the httpx extra and generate with the default transport mode:
uv add "openapi-python[httpx]"
uv run openapi-python generate --spec ./openapi.json --out ./generated --package my_client
You can supply preconfigured httpx clients:
import httpx
from generated.my_client import AsyncClient, Client, DefaultAsyncTransport, DefaultTransport
sync_http = httpx.Client(headers={"authorization": "Bearer token"})
async_http = httpx.AsyncClient(headers={"authorization": "Bearer token"})
client = Client(
base_url="https://api.example.com",
transport=DefaultTransport(client=sync_http),
)
async_client = AsyncClient(
base_url="https://api.example.com",
transport=DefaultAsyncTransport(client=async_http),
)
Custom transport
Install openapi-python without extras and generate protocol-only code:
uv add openapi-python requests
uv run openapi-python generate \
--spec ./openapi.json \
--out ./generated \
--package my_client \
--protocol-only
Then provide an object that satisfies the generated Transport protocol:
from collections.abc import Mapping
import requests
from generated.my_client import Client
class RequestsTransport:
def request(
self,
*,
method: str,
route: str,
base_url: str,
params: Mapping[str, object] | None,
query: Mapping[str, object] | None,
headers: Mapping[str, object] | None,
body: object | None,
) -> object:
response = requests.request(
method=method.upper(),
url=f"{base_url.rstrip('/')}{route.format(**(params or {}))}",
params={key: str(value) for key, value in (query or {}).items()} or None,
headers={key: str(value) for key, value in (headers or {}).items()} or None,
json=body,
)
response.raise_for_status()
if response.content:
return response.json()
return None
client = Client(
base_url="https://api.example.com",
transport=RequestsTransport(),
)
book = client.get("/books/{book_id}")(params={"book_id": 1})
Project details
Release history Release notifications | RSS feed
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_python-0.0.11.tar.gz.
File metadata
- Download URL: openapi_python-0.0.11.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
115ccfdaa0a29a37b77511c102b302e10a5b96f507bd98d9976372011f0e01fc
|
|
| MD5 |
032c7d4db06671240eb2355fa2e32127
|
|
| BLAKE2b-256 |
93615dc47fca6e490ca4ffbf6236c118aedafdbb014c77aa6f00dd731cfeec9b
|
Provenance
The following attestation bundles were made for openapi_python-0.0.11.tar.gz:
Publisher:
release.yml on Minibrams/openapi-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_python-0.0.11.tar.gz -
Subject digest:
115ccfdaa0a29a37b77511c102b302e10a5b96f507bd98d9976372011f0e01fc - Sigstore transparency entry: 1555347726
- Sigstore integration time:
-
Permalink:
Minibrams/openapi-python@206fa18a0446075b8af730bc8c7a030a8c5a1837 -
Branch / Tag:
refs/heads/releases - Owner: https://github.com/Minibrams
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@206fa18a0446075b8af730bc8c7a030a8c5a1837 -
Trigger Event:
push
-
Statement type:
File details
Details for the file openapi_python-0.0.11-py3-none-any.whl.
File metadata
- Download URL: openapi_python-0.0.11-py3-none-any.whl
- Upload date:
- Size: 27.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d6bc0d5e78093cbda7364c78f76eb987507d5083ae1a7c3b2c402fd38d10e1a
|
|
| MD5 |
f42c230e883f79410f5de05bb0608a27
|
|
| BLAKE2b-256 |
b4697fe3c437f22770c3dd1fd6dc4d7a457f61b96b60d44c076fbed1612f96f6
|
Provenance
The following attestation bundles were made for openapi_python-0.0.11-py3-none-any.whl:
Publisher:
release.yml on Minibrams/openapi-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
openapi_python-0.0.11-py3-none-any.whl -
Subject digest:
5d6bc0d5e78093cbda7364c78f76eb987507d5083ae1a7c3b2c402fd38d10e1a - Sigstore transparency entry: 1555347728
- Sigstore integration time:
-
Permalink:
Minibrams/openapi-python@206fa18a0446075b8af730bc8c7a030a8c5a1837 -
Branch / Tag:
refs/heads/releases - Owner: https://github.com/Minibrams
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@206fa18a0446075b8af730bc8c7a030a8c5a1837 -
Trigger Event:
push
-
Statement type: