huggingface/smolagents · error · ValueError
Received both `token` and `api_key` arguments. Please provid
Error message
Received both `token` and `api_key` arguments. Please provide only one of them. `api_key` is an alias for `token` to make the API compatible with OpenAI's client. It has the exact same behavior as `token`.
What it means
InferenceClientModel.__init__ accepts both `token` and `api_key` (an OpenAI-compatibility alias for token), but passing both is ambiguous so it raises ValueError immediately. Only one credential argument may be supplied; they are otherwise identical in behavior.
Source
Thrown at src/smolagents/models.py:1528
"Quantum mechanics is the branch of physics that studies..."
```
"""
def __init__(
self,
model_id: str = "Qwen/Qwen3-Next-80B-A3B-Thinking",
provider: str | None = None,
token: str | None = None,
timeout: int = 120,
client_kwargs: dict[str, Any] | None = None,
custom_role_conversions: dict[str, str] | None = None,
api_key: str | None = None,
bill_to: str | None = None,
base_url: str | None = None,
**kwargs,
):
if token is not None and api_key is not None:
raise ValueError(
"Received both `token` and `api_key` arguments. Please provide only one of them."
" `api_key` is an alias for `token` to make the API compatible with OpenAI's client."
" It has the exact same behavior as `token`."
)
token = token if token is not None else api_key
if token is None:
token = os.getenv("HF_TOKEN")
self.client_kwargs = {
**(client_kwargs or {}),
"model": model_id,
"provider": provider,
"token": token,
"timeout": timeout,
"bill_to": bill_to,
"base_url": base_url,
}
super().__init__(model_id=model_id, custom_role_conversions=custom_role_conversions, **kwargs)
View on GitHub (pinned to 30bb116109)
Solutions
- Pass only one of the two: keep `api_key=` for OpenAI-style code, or `token=` — never both.
- If a wrapper injects token, remove it from your explicit call (or filter kwargs) so only one credential reaches the constructor.
Example fix
# before model = InferenceClientModel(model_id="...", token=os.environ["HF_TOKEN"], api_key=os.environ["HF_TOKEN"]) # after model = InferenceClientModel(model_id="...", api_key=os.environ["HF_TOKEN"])
Defensive patterns
Strategy: validation
Validate before calling
creds = {k: v for k, v in {"token": token, "api_key": api_key}.items() if v is not None}
assert len(creds) <= 1, "pass only one of token/api_key" Type guard
null
Try / catch
try:
model = InferenceClientModel(model_id=mid, api_key=key)
except ValueError as e:
if "both `token` and `api_key`" in str(e):
model = InferenceClientModel(model_id=mid, api_key=key) # retry with single credential
else:
raise Prevention
- Standardize on api_key in OpenAI-style codebases; never forward unfiltered **kwargs that may contain token.
- Centralize credential passing in one config helper.
When it happens
Trigger: Constructing InferenceClientModel(token=..., api_key=...) in the same call — commonly when code sets api_key explicitly while token is also filled from a config/env wrapper or kwargs merging.
Common situations: Migrating OpenAI-style code to smolagents and passing api_key while a framework (e.g. an agent scaffold) injects token; copying examples that use token and adding api_key on top; forwarding **kwargs that happen to contain token.
Related errors
AI-assisted analysis of huggingface/smolagents@30bb116109 (2026-08-28).
Data as JSON: /api/errors/6746480e44ebd98d.
Report an issue: GitHub.