xtekky/gpt4free · error · ValueError
API key is required for Azure provider. Ask for API key in t
Error message
API key is required for Azure provider. Ask for API key in the {cls.login_url} Discord server. What it means
When AZURE_API_KEYS is configured, the provider resolves the key per model (falling back to the 'default' entry) and raises this ValueError when neither exists. It is the provider's way of saying: endpoint routing worked, but you supplied no credential for that model and Azure OpenAI requires one.
Source
Thrown at g4f/Provider/needs_auth/Azure.py:85
api_endpoint: str = None,
**kwargs,
) -> AsyncResult:
if not model:
model = os.environ.get("AZURE_DEFAULT_MODEL", cls.default_model)
if model in cls.model_aliases:
model = cls.model_aliases[model]
if not api_endpoint:
if not cls.routes:
cls.get_models()
api_endpoint = cls.routes.get(model)
if cls.routes and not api_endpoint:
raise ModelNotFoundError(f"No API endpoint found for model: {model}")
if not api_endpoint:
api_endpoint = os.environ.get("AZURE_API_ENDPOINT")
if cls.api_keys:
api_key = cls.api_keys.get(model, cls.api_keys.get("default"))
if not api_key:
raise ValueError(
f"API key is required for Azure provider. Ask for API key in the {cls.login_url} Discord server."
)
if api_endpoint and "/images/" in api_endpoint:
prompt = format_media_prompt(messages, kwargs.get("prompt"))
width, height = get_width_height(
kwargs.get("aspect_ratio", "1:1"),
kwargs.get("width"),
kwargs.get("height"),
)
output_format = kwargs.get("output_format", "png")
form = None
data = None
if media:
form = FormData()
form.add_field("prompt", prompt)
form.add_field("width", str(width))
form.add_field("height", str(height))
output_format = "png"View on GitHub (pinned to 973504e177)
Solutions
- Add a 'default' key entry: AZURE_API_KEYS='{"default": "sk-..."}' so any routed model has a credential.
- Or add a per-model entry whose key exactly matches the model name used in routing.
- Restart the process after changing the environment variable — cls.api_keys is cached on the class.
Example fix
# before
export AZURE_API_KEYS='{"gpt-4o": "sk-abc"}'
# request model="gpt-4o-mini" -> ValueError
# after
export AZURE_API_KEYS='{"default": "sk-abc", "gpt-4o": "sk-abc"}' Defensive patterns
Strategy: validation
Validate before calling
keys = Azure.api_keys or json.loads(os.environ.get("AZURE_API_KEYS", "{}"))
if keys and not (model in keys or "default" in keys):
raise ValueError(f"no API key configured for model {model!r}; add it or a 'default' entry") Type guard
def model_has_api_key(model: str) -> bool:
"""True when the model or a 'default' entry exists in AZURE_API_KEYS."""
return model in Azure.api_keys or "default" in Azure.api_keys Try / catch
try:
await Azure.create_async_generator(model=model, messages=messages)
except ValueError as e:
if "API key is required" in str(e):
# config gap: add key for the model or a 'default' entry, restart
raise Prevention
- Always include a 'default' entry in AZURE_API_KEYS as a safety net
- Validate at startup that every AZURE_ROUTES key has a matching key or a default
- Restart the process after editing key env vars — values are class-cached
When it happens
Trigger: AZURE_API_KEYS='{"gpt-4o": "sk-..."}' with no 'default' entry, and the caller requests a different model ('gpt-35-turbo'). api_key = cls.api_keys.get(model, cls.api_keys.get('default')) is None -> raise.
Common situations: New model added to AZURE_ROUTES but its key forgotten in AZURE_API_KEYS; 'default' fallback omitted; key JSON valid but model-name key mismatched (typos, case).
Related errors
- Invalid AZURE_API_KEYS environment variable
- Invalid AZURE_ROUTES environment variable format: {routes}
- No Yupp accounts configured. Set YUPP_API_KEY environment va
- Failed to obtain API key from Z.ai authentication endpoint
- No API endpoint found for model: {model}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/8c4a6be05e4657fc.
Report an issue: GitHub.