diff --git a/litellm/utils.py b/litellm/utils.py index 3795262a6c03..d93c88e05a09 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -4789,7 +4789,7 @@ def get_api_key(llm_provider: str, dynamic_api_key: str | None): api_key = api_key or litellm.anthropic_key or get_secret("ANTHROPIC_API_KEY") # ai21 elif llm_provider == "ai21": - api_key = api_key or litellm.ai21_key or get_secret("AI211_API_KEY") + api_key = api_key or litellm.ai21_key or get_secret("AI21_API_KEY") # aleph_alpha elif llm_provider == "aleph_alpha": api_key = api_key or litellm.aleph_alpha_key or get_secret("ALEPH_ALPHA_API_KEY") diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py index f1f863b99a0f..a8eb8b119741 100644 --- a/tests/test_litellm/test_utils.py +++ b/tests/test_litellm/test_utils.py @@ -26,6 +26,7 @@ TextCompletionStreamWrapper, _check_provider_match, _is_streaming_request, + get_api_key, get_llm_provider, get_optional_params_image_gen, get_prompt_cache_min_tokens, @@ -5108,3 +5109,14 @@ def test_reapply_runtime_registrations_drops_request_scoped_registrations(monkey finally: litellm.model_cost = saved_model_cost _invalidate_model_cost_lowercase_map() + + +def test_ai21_api_key_is_resolved_from_the_documented_env_var(monkeypatch: pytest.MonkeyPatch) -> None: + """The ai21 branch resolved a misspelled env var, so the name every other ai21 code path + reads, and the only name documented, was ignored.""" + monkeypatch.setattr(litellm, "api_key", None) + monkeypatch.setattr(litellm, "ai21_key", None) + monkeypatch.delenv("AI211_API_KEY", raising=False) + monkeypatch.setenv("AI21_API_KEY", "sk-ai21-resolved-from-env") + + assert get_api_key(llm_provider="ai21", dynamic_api_key=None) == "sk-ai21-resolved-from-env"