Generate modern Python clients from OpenAPI 3.0 and 3.1 documents
—
Comprehensive configuration system supporting file-based configuration, class overrides, project customization, and post-generation hooks. The configuration system allows fine-grained control over code generation behavior and output structure.
Load configuration from YAML or JSON files for repeatable builds and complex setups.
class ConfigFile(BaseModel):
class_overrides: Optional[dict[str, ClassOverride]] = None
content_type_overrides: Optional[dict[str, str]] = None
project_name_override: Optional[str] = None
package_name_override: Optional[str] = None
package_version_override: Optional[str] = None
use_path_prefixes_for_title_model_names: bool = True
post_hooks: Optional[list[str]] = None
docstrings_on_attributes: bool = False
field_prefix: str = "field_"
generate_all_tags: bool = False
http_timeout: int = 5
literal_enums: bool = False
@staticmethod
def load_from_path(path: Path) -> "ConfigFile":
"""
Load configuration from JSON or YAML file.
Parameters:
- path: Path to configuration file
Returns:
ConfigFile object with loaded settings
"""Control the names and organization of generated model classes.
class ClassOverride(BaseModel):
class_name: Optional[str] = None
module_name: Optional[str] = None
"""
Configuration for overriding generated class names.
Attributes:
- class_name: Override the generated class name
- module_name: Override the generated module name
"""Control the type of project metadata generated (pyproject.toml, setup.py, etc.).
class MetaType(str, Enum):
NONE = "none" # No project metadata files
POETRY = "poetry" # Poetry-compatible pyproject.toml
SETUP = "setup" # setup.py file
PDM = "pdm" # PDM-compatible pyproject.toml
UV = "uv" # uv-compatible pyproject.tomlThe main configuration object used during generation, combining file-based and runtime settings.
class Config:
meta_type: MetaType
class_overrides: dict[str, ClassOverride]
project_name_override: Optional[str]
package_name_override: Optional[str]
package_version_override: Optional[str]
use_path_prefixes_for_title_model_names: bool
post_hooks: list[str]
docstrings_on_attributes: bool
field_prefix: str
generate_all_tags: bool
http_timeout: int
literal_enums: bool
document_source: Union[Path, str]
file_encoding: str
content_type_overrides: dict[str, str]
overwrite: bool
output_path: Optional[Path]
@staticmethod
def from_sources(
config_file: ConfigFile,
meta_type: MetaType,
document_source: Union[Path, str],
file_encoding: str,
overwrite: bool,
output_path: Optional[Path],
) -> "Config":
"""
Create a Config from various sources.
Parameters:
- config_file: File-based configuration settings
- meta_type: Project metadata type
- document_source: OpenAPI document URL or file path
- file_encoding: Text encoding for generated files
- overwrite: Whether to overwrite existing output
- output_path: Custom output directory
Returns:
Combined configuration object
"""Control the names and structure of generated projects:
# Project and package names
project_name_override: "my-awesome-api-client"
package_name_override: "my_awesome_api_client"
package_version_override: "1.0.0"
# Use path prefixes in model names for disambiguation
use_path_prefixes_for_title_model_names: trueRename generated classes and control their module organization:
class_overrides:
VeryLongGeneratedModelName:
class_name: ShortName
module_name: short_name
AnotherLongName:
class_name: Better
# module_name defaults to snake_case of class_nameControl aspects of the generated code:
# Add docstrings to model attributes
docstrings_on_attributes: true
# Prefix for fields that conflict with Python keywords
field_prefix: "field_"
# Generate all tags even if they have no operations
generate_all_tags: false
# Use literal types instead of enums for string enums
literal_enums: trueOverride content type detection for specific MIME types:
content_type_overrides:
"application/vnd.api+json": "application/json"
"text/csv": "text/plain"Run commands after code generation for formatting and validation:
post_hooks:
- "ruff check --fix ."
- "ruff format ."
- "mypy ."
- "pytest tests/"Control HTTP behavior when fetching OpenAPI documents:
# Timeout in seconds for HTTP requests
http_timeout: 30# config.yaml
project_name_override: "weather-api-client"
package_name_override: "weather_api_client"
class_overrides:
WeatherDataResponseModel:
class_name: WeatherData
module_name: weather_data
post_hooks:
- "ruff check --fix ."
- "ruff format ."
field_prefix: "attr_"
docstrings_on_attributes: true
http_timeout: 10{
"project_name_override": "weather-api-client",
"package_name_override": "weather_api_client",
"class_overrides": {
"WeatherDataResponseModel": {
"class_name": "WeatherData",
"module_name": "weather_data"
}
},
"post_hooks": [
"ruff check --fix .",
"ruff format ."
],
"field_prefix": "attr_",
"docstrings_on_attributes": true,
"http_timeout": 10
}from pathlib import Path
from openapi_python_client.config import ConfigFile, MetaType, ClassOverride
# Load from file
config_file = ConfigFile.load_from_path(Path("config.yaml"))
# Or create programmatically
config_file = ConfigFile(
project_name_override="my-client",
class_overrides={
"LongModelName": ClassOverride(
class_name="ShortName",
module_name="short_name"
)
},
post_hooks=["ruff check --fix .", "ruff format ."],
docstrings_on_attributes=True,
field_prefix="field_",
http_timeout=30
)from openapi_python_client.config import MetaType
# Poetry (default) - generates pyproject.toml with Poetry metadata
meta_type = MetaType.POETRY
# PDM - generates pyproject.toml with PDM metadata
meta_type = MetaType.PDM
# Setup.py - generates traditional setup.py file
meta_type = MetaType.SETUP
# UV - generates pyproject.toml with uv metadata
meta_type = MetaType.UV
# None - generates only Python code, no project metadata
meta_type = MetaType.NONE# Advanced post-hooks for different environments
post_hooks:
# Code formatting
- "ruff check --fix ."
- "ruff format ."
# Type checking
- "mypy ."
# Testing
- "pytest --cov=. --cov-report=html"
# Documentation
- "sphinx-build -b html docs docs/_build"
# Security scanning
- "bandit -r ."When no configuration file is provided, the system uses these defaults:
MetaType.POETRY"field_"5 seconds"utf-8"truefalsefalsefalseInstall with Tessl CLI
npx tessl i tessl/pypi-openapi-python-client