openai/openai-python · critical · ValueError
Must provide one of the `base_url` or `azure_endpoint` argum
Error message
Must provide one of the `base_url` or `azure_endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable
What it means
The synchronous AzureOpenAI client requires a service endpoint to build request URLs. None was supplied via the base_url argument, the azure_endpoint argument, or the AZURE_OPENAI_ENDPOINT environment variable, so the client cannot determine where to send requests and raises ValueError during construction.
Source
Thrown at src/openai/lib/azure.py:324
if api_version is None:
api_version = os.environ.get("OPENAI_API_VERSION")
if api_version is None:
raise ValueError(
"Must provide either the `api_version` argument or the `OPENAI_API_VERSION` environment variable"
)
if default_query is None:
default_query = {"api-version": api_version}
else:
default_query = {**default_query, "api-version": api_version}
if base_url is None:
if azure_endpoint is None:
azure_endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT")
if azure_endpoint is None:
raise ValueError(
"Must provide one of the `base_url` or `azure_endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable"
)
if azure_deployment is not None:
base_url = f"{azure_endpoint.rstrip('/')}/openai/deployments/{azure_deployment}"
else:
base_url = f"{azure_endpoint.rstrip('/')}/openai"
else:
if azure_endpoint is not None:
raise ValueError("base_url and azure_endpoint are mutually exclusive")
if api_key is None:
# define a sentinel value to avoid any typing issues
api_key = API_KEY_SENTINEL
super().__init__(
api_key=api_key,
admin_api_key=admin_api_key,View on GitHub (pinned to 9917c6e28e)
Solutions
- Set AZURE_OPENAI_ENDPOINT (e.g. https://myresource.openai.azure.com) in the environment
- Pass azure_endpoint="https://myresource.openai.azure.com" to AzureOpenAI(...)
- Alternatively pass base_url directly if you use a full custom URL
Example fix
// before client = AzureOpenAI(api_version="2024-02-01") // after client = AzureOpenAI(azure_endpoint="https://myresource.openai.azure.com", api_version="2024-02-01")
Defensive patterns
Strategy: validation
Validate before calling
import os
if not (os.environ.get("AZURE_OPENAI_ENDPOINT") or MY_AZURE_ENDPOINT):
raise RuntimeError("AZURE_OPENAI_ENDPOINT or azure_endpoint is required") Try / catch
try:
client = AzureOpenAI(api_version=V)
except ValueError as e:
if "azure_endpoint" in str(e):
raise ConfigError(str(e)) from e
raise Prevention
- Centralize Azure config in one settings module
- Fail fast at startup on missing env vars
- Document required env vars in README/deployment config
When it happens
Trigger: Constructing AzureOpenAI() with no azure_endpoint/base_url and no AZURE_OPENAI_ENDPOINT env var set; CI shells or stripped env where the variable was expected but absent.
Common situations: Env var not exported in the active shell/venv, .env file not loaded, deploying to a new environment without copying Azure config, or confusing OpenAI's OPENAI_BASE_URL with AZURE_OPENAI_ENDPOINT.
Understand the failure class
Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.
Related errors
- base_url and azure_endpoint are mutually exclusive
- The webhook secret must either be set using the env var, OPE
- `provider` cannot be combined with top-level {formatted}. Mo
- The `api_key` and `workload_identity` arguments are mutually
- Missing credentials. Please pass an `api_key`, `workload_ide
AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28).
Data as JSON: /api/errors/98d2d06029a991ce.
Report an issue: GitHub.