Implementation:Openai Openai python CLI Entry Point
| Knowledge Sources | |
|---|---|
| Domains | CLI |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for the command-line interface entry point provided by the openai-python SDK.
Description
The _cli module implements the openai command-line tool. The main() function serves as the entry point, catching APIError, CLIError, and pydantic.ValidationError and displaying them via display_error(). The _build_parser() function constructs an argparse.ArgumentParser with global options for --verbose, --api-base, --api-key, --proxy, --organization, --api-type (openai/azure), --api-version, --azure-endpoint, --azure-ad-token, and --version. It registers two subparser groups: api (via register_commands) for direct API calls and tools (via _tools.register_commands) for client-side utilities. The Arguments class is a Pydantic BaseModel that captures all parsed CLI arguments with Pydantic v1/v2 compatible configuration. The _main() function configures an httpx.Client with optional proxy support and HTTP/2, sets module-level OpenAI configuration, and dispatches to the appropriate subcommand handler.
Usage
Use this module by running openai from the command line, or by calling main() programmatically.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/cli/_cli.py
- Lines: 1-233
Signature
class Arguments(BaseModel):
verbosity: int
version: Optional[str] = None
api_key: Optional[str]
api_base: Optional[str]
organization: Optional[str]
proxy: Optional[List[str]]
api_type: Optional[_ApiType] = None
api_version: Optional[str] = None
azure_endpoint: Optional[str] = None
azure_ad_token: Optional[str] = None
args_model: Optional[Type[BaseModel]] = None
unknown_args: List[str] = []
allow_unknown_args: bool = False
def _build_parser() -> argparse.ArgumentParser: ...
def main() -> int: ...
Import
from openai.cli._cli import main
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sys.argv | list[str] | Yes | Command-line arguments passed to the openai CLI |
| --api-key / -k | str | No | API key to use for authentication |
| --api-base / -b | str | No | Base URL for API calls |
| --organization / -o | str | No | Organization ID |
| --proxy / -p | list[str] | No | HTTP/HTTPS proxy URLs |
| --api-type / -t | "openai" or "azure" | No | Backend API type |
| --azure-endpoint | str | No | Azure endpoint URL |
Outputs
| Name | Type | Description |
|---|---|---|
| main() return | int | Exit code: 0 for success, 1 for error |
| stdout/stderr | text | Command output or error messages |
Usage Examples
Basic Usage
# From the command line:
# openai api chat.completions.create -m gpt-4o -g user "Hello"
# Programmatic usage:
import sys
from openai.cli._cli import main
sys.argv = ["openai", "--version"]
exit_code = main()