OpenAPI Python Client Generator is a Python library and CLI tool that generates modern, type-hinted Python clients from OpenAPI specifications. It supports both OpenAPI v2 (Swagger) and v3 specifications, generating Pydantic-based models and httpx-based clients. It's actively maintained with frequent releases, currently at version 0.28.3.
pip install openapi-python-clientVerified import paths — ran on the pinned version, not inferred.
Generates a Python client for the Petstore API from its OpenAPI specification. The primary interaction with openapi-python-client is via its command-line interface. The generated client is then imported and used like a standard Python library.
The `name` argument was renamed to `path` in `create_new_client`. The output directory structure also changed from `{output_path}` to `{output_path}/{project_name}/`. Adjust programmatic calls and expect a new directory layout.`BaseModel` was replaced by `PydanticBaseModel` and `TypingBaseModel`. The `from_dict` method was removed from models, and `_get_kwargs` was renamed to `_prepped_request` on `_Endpoint` classes. Update any custom code interacting directly with these internal components.
Prior to 0.23.0, `httpx` was optional and `mypy` generation could be disabled. Since 0.23.0, `httpx` is a direct dependency and `mypy` type hints are always generated. Ensure your environment allows for these dependencies and integrate mypy checks into your workflow if not already.
Clients generated with `openapi-python-client` versions 0.21.0 and above require Pydantic V2. If you have existing code using Pydantic V1 and wish to regenerate your client, you might need to manage Pydantic versions carefully or update your application to Pydantic V2.
While `openapi-python-client` attempts to handle complex types, especially those involving `oneOf`/`anyOf`/`allOf` or discriminator patterns, the resulting Pydantic models can sometimes be difficult to work with or might not perfectly reflect the intended schema logic. Always review the generated client's models and their types, and test thoroughly. Custom templates can be used for advanced control.