Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(together_ai): let an explicit api_key beat the Together env key on api_base match

mateo-berri5e6b6c6281f8
diff --git a/litellm/litellm_core_utils/get_llm_provider_logic.py b/litellm/litellm_core_utils/get_llm_provider_logic.py
index d2d82064c47..005e94ebe82 100644
--- a/litellm/litellm_core_utils/get_llm_provider_logic.py
+++ b/litellm/litellm_core_utils/get_llm_provider_logic.py
@@ -274,7 +274,7 @@ def get_llm_provider(
dynamic_api_key = get_secret_str("DEEPSEEK_API_KEY")
elif endpoint == "api.together.ai/v1" or endpoint == "api.together.xyz/v1":
custom_llm_provider = "together_ai"
- dynamic_api_key = (
+ dynamic_api_key = api_key or (
get_secret_str("TOGETHER_API_KEY")
or get_secret_str("TOGETHER_AI_API_KEY")
or get_secret_str("TOGETHERAI_API_KEY")
diff --git a/litellm/llms/together_ai/rerank/handler.py b/litellm/llms/together_ai/rerank/handler.py
index 8407018b898..b8079e52c97 100644
--- a/litellm/llms/together_ai/rerank/handler.py
+++ b/litellm/llms/together_ai/rerank/handler.py
@@ -51,7 +51,7 @@ class TogetherAIRerank(BaseLLM):
raise ValueError("TogetherAI does not support max_chunks_per_doc")
if _is_async:
- return self.async_rerank(request_data_dict, api_key, api_base) # Call async method
+ return self.async_rerank(request_data_dict, api_key, api_base)
response: Final = client.post(
_rerank_url(api_base),
diff --git a/tests/test_litellm/litellm_core_utils/test_get_llm_provider_endpoint_match.py b/tests/test_litellm/litellm_core_utils/test_get_llm_provider_endpoint_match.py
index 5c20284282a..6cacd119030 100644
--- a/tests/test_litellm/litellm_core_utils/test_get_llm_provider_endpoint_match.py
+++ b/tests/test_litellm/litellm_core_utils/test_get_llm_provider_endpoint_match.py
@@ -165,6 +165,18 @@ class TestTogetherApiBaseResolvesProvider:
assert returned_api_base == api_base
assert model == "some-model"
+ def test_explicit_api_key_beats_together_env_key(self, monkeypatch):
+ monkeypatch.setenv("TOGETHER_API_KEY", "together-key-from-env")
+
+ _, provider, dynamic_api_key, _ = get_llm_provider(
+ model="some-model",
+ api_base="https://api.together.ai/v1",
+ api_key="explicit-caller-key",
+ )
+
+ assert provider == "together_ai"
+ assert dynamic_api_key == "explicit-caller-key"
+
def test_together_default_api_base_is_together_ai(self, monkeypatch):
monkeypatch.delenv("TOGETHER_AI_API_BASE", raising=False)