Generate modern Python clients from OpenAPI 3.0 and 3.1 documents
—
Command-line interface for generating Python clients from OpenAPI specifications. The CLI provides a user-friendly way to generate client libraries with various customization options.
The primary command for generating OpenAPI Python clients from specifications.
def generate(
url: Optional[str] = None,
path: Optional[Path] = None,
custom_template_path: Optional[Path] = None,
meta: MetaType = MetaType.POETRY,
file_encoding: str = "utf-8",
config_path: Optional[Path] = None,
fail_on_warning: bool = False,
overwrite: bool = False,
output_path: Optional[Path] = None,
) -> None:
"""
Generate a new OpenAPI Client library.
Parameters:
- url: A URL to read the OpenAPI document from
- path: A path to the OpenAPI document
- custom_template_path: A path to a directory containing custom template(s)
- meta: The type of metadata to generate (POETRY, SETUP, PDM, UV, NONE)
- file_encoding: Encoding used when writing generated files
- config_path: Path to the config file to use
- fail_on_warning: Exit with error code on warnings
- overwrite: Overwrite the existing client if it exists
- output_path: Path to write the generated code to
"""Display version information and exit.
openapi-python-client --versionThe CLI provides comprehensive error handling and reporting.
def handle_errors(errors: Sequence[GeneratorError], fail_on_warning: bool = False) -> None:
"""
Turn custom errors into formatted error messages.
Parameters:
- errors: List of GeneratorError objects to handle
- fail_on_warning: Whether to exit with error code on warnings
"""Generate a client from a URL:
openapi-python-client generate --url https://api.example.com/openapi.jsonGenerate a client from a local file:
openapi-python-client generate --path ./openapi.yamlGenerate with custom configuration:
openapi-python-client generate \
--url https://api.example.com/openapi.json \
--config config.yaml \
--meta poetry \
--overwrite \
--output-path ./my-clientGenerate with custom templates:
openapi-python-client generate \
--url https://api.example.com/openapi.json \
--custom-template-path ./my-templatesGenerate with specific encoding:
openapi-python-client generate \
--path ./openapi.yaml \
--file-encoding utf-16Use a YAML configuration file for complex setups:
# config.yaml
class_overrides:
VeryLongModelName:
class_name: ShortName
module_name: short_name
project_name_override: "my-api-client"
package_name_override: "my_api_client"
post_hooks:
- "ruff check --fix ."
- "ruff format ."
- "mypy ."
field_prefix: "attr_"
http_timeout: 10Then use it:
openapi-python-client generate \
--url https://api.example.com/openapi.json \
--config config.yamlThe CLI generates a complete Python package structure:
my-api-client/ # Project directory
├── pyproject.toml # Project metadata
├── README.md # Generated documentation
├── .gitignore # Git ignore rules
└── my_api_client/ # Python package
├── __init__.py # Package initialization
├── client.py # HTTP client classes
├── errors.py # Error definitions
├── types.py # Type definitions
├── models/ # Data models
│ ├── __init__.py
│ └── *.py # Individual model files
└── api/ # API endpoints
├── __init__.py
└── */ # Tag-based API modules
├── __init__.py
└── *.py # Endpoint functionsInstall with Tessl CLI
npx tessl i tessl/pypi-openapi-python-client