This commit is contained in:
Mateo Wang 2026-10-27 02:24:56 +00:00 • committed by GitHub
commit 9aa62cd315
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 122 additions and 1 deletions

View file

@ -342,6 +342,7 @@ DEFAULT_REASONING_EFFORT_MINIMAL_THINKING_BUDGET: Final = get_env_int(
# Provider-specific API base URLs
XAI_API_BASE: Final = "https://api.x.ai/v1"
PLACEHOLDER_API_KEY: Final = "fake-api-key"
PLACEHOLDER_API_KEYS: Final = frozenset({PLACEHOLDER_API_KEY, "dummy-key"})
OPEN_SANDBOX_API_BASE_ENV_VAR: Final = "OPEN_SANDBOX_API_BASE"
OPEN_SANDBOX_API_KEY_ENV_VAR: Final = "OPEN_SANDBOX_API_KEY"
OPEN_SANDBOX_DEFAULT_TEMPLATE: Final = "opensandbox/code-interpreter:v1.1.0"

View file

@ -66,6 +66,7 @@ from litellm.constants import (
DEFAULT_MOCK_RESPONSE_PROMPT_TOKEN_COUNT,
NADIR_DEFAULT_API_BASE,
OPENAI_AUDIO_TRANSCRIPTION_PROVIDERS,
PLACEHOLDER_API_KEYS,
)
from litellm.exceptions import LiteLLMUnknownProvider
from litellm.integrations.custom_logger import CustomLogger
@ -8629,6 +8630,7 @@ def speech(
model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(
model=model, custom_llm_provider=custom_llm_provider, api_base=api_base or base_url
)
provider_api_key: Final = None if dynamic_api_key in PLACEHOLDER_API_KEYS else dynamic_api_key
kwargs.pop("tags", [])
optional_params = {}
@ -8718,9 +8720,11 @@ def speech(
# set API KEY
api_key = (
api_key
or litellm.api_key # for deepinfra/perplexity/anyscale we check in get_llm_provider and pass in the api key from there
or provider_api_key # for deepinfra/perplexity/anyscale we check in get_llm_provider and pass in the api key from there
or litellm.api_key
or litellm.openai_key
or get_secret("OPENAI_API_KEY")
or dynamic_api_key
)
organization = (
@ -8803,6 +8807,7 @@ def speech(
api_key = (
api_key
or provider_api_key
or litellm.api_key
or litellm.azure_key
or get_secret("AZURE_OPENAI_API_KEY")

View file

@ -5129,6 +5129,121 @@ def test_speech_mistral_routes_to_configured_api_base(respx_mock: respx.MockRout
assert response.content == audio_bytes
TTS_AUDIO: Final = b"ID3\x04\x00\x00\x00fake-mp3-payload"
def _clear_global_tts_keys(monkeypatch: pytest.MonkeyPatch) -> None:
for attribute in ("api_key", "openai_key", "azure_key", "api_base"):
monkeypatch.setattr(litellm, attribute, None)
def _set_or_clear_env(monkeypatch: pytest.MonkeyPatch, values: dict[str, str | None]) -> None:
for name, value in values.items():
if value is None:
monkeypatch.delenv(name, raising=False)
else:
monkeypatch.setenv(name, value)
@pytest.mark.parametrize(
("provider_key", "openai_key", "sent_authorization"),
[
("hosted-vllm-scoped-key", None, "Bearer hosted-vllm-scoped-key"),
("hosted-vllm-scoped-key", "generic-openai-key", "Bearer hosted-vllm-scoped-key"),
(None, "generic-openai-key", "Bearer generic-openai-key"),
(None, None, "Bearer fake-api-key"),
],
ids=["provider-key-alone", "provider-key-beats-generic", "generic-key-beats-placeholder", "placeholder-when-unset"],
)
def test_speech_openai_compatible_sends_the_provider_scoped_key(
respx_mock: respx.MockRouter,
monkeypatch: pytest.MonkeyPatch,
provider_key: str | None,
openai_key: str | None,
sent_authorization: str,
):
_clear_global_tts_keys(monkeypatch)
_set_or_clear_env(monkeypatch, {"HOSTED_VLLM_API_KEY": provider_key, "OPENAI_API_KEY": openai_key})
route: Final = respx_mock.post("https://vllm.internal/v1/audio/speech").mock(
return_value=httpx.Response(200, content=TTS_AUDIO, headers={"content-type": "audio/mpeg"})
)
response: Final = litellm.speech(
model="hosted_vllm/tts-1", input="which key goes out", voice="alloy", api_base="https://vllm.internal/v1"
)
assert response.content == TTS_AUDIO
assert route.call_count == 1
assert route.calls.last.request.headers["authorization"] == sent_authorization
@pytest.mark.parametrize(
("generic_azure_key", "sent_api_key"),
[(None, "azure-ai-scoped-key"), ("generic-azure-key", "azure-ai-scoped-key")],
ids=["provider-key-alone", "provider-key-beats-generic"],
)
def test_speech_azure_sends_the_provider_scoped_key(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, generic_azure_key: str | None, sent_api_key: str
):
_clear_global_tts_keys(monkeypatch)
_set_or_clear_env(
monkeypatch,
{"AZURE_AI_API_KEY": "azure-ai-scoped-key", "AZURE_OPENAI_API_KEY": None, "AZURE_API_KEY": generic_azure_key},
)
route: Final = respx_mock.post(
url__regex=r"https://foundry\.internal/openai/deployments/tts-1/audio/speech\?api-version=.+"
).mock(return_value=httpx.Response(200, content=TTS_AUDIO, headers={"content-type": "audio/mpeg"}))
response: Final = litellm.speech(
model="azure_ai/tts-1",
input="which key goes out",
voice="alloy",
api_base="https://foundry.internal",
api_version="2024-05-01-preview",
)
assert response.content == TTS_AUDIO
assert route.call_count == 1
assert route.calls.last.request.headers["api-key"] == sent_api_key
def test_speech_docker_model_runner_placeholder_does_not_beat_a_real_key(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
):
_clear_global_tts_keys(monkeypatch)
_set_or_clear_env(monkeypatch, {"DOCKER_MODEL_RUNNER_API_KEY": None, "OPENAI_API_KEY": "generic-openai-key"})
route: Final = respx_mock.post("https://dmr.internal/v1/audio/speech").mock(
return_value=httpx.Response(200, content=TTS_AUDIO, headers={"content-type": "audio/mpeg"})
)
litellm.speech(
model="docker_model_runner/tts-1", input="which key goes out", voice="alloy", api_base="https://dmr.internal/v1"
)
assert route.calls.last.request.headers["authorization"] == "Bearer generic-openai-key"
def test_speech_keeps_an_explicitly_passed_api_key_over_the_provider_scoped_one(
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
):
_clear_global_tts_keys(monkeypatch)
monkeypatch.setenv("HOSTED_VLLM_API_KEY", "hosted-vllm-scoped-key")
route: Final = respx_mock.post("https://vllm.internal/v1/audio/speech").mock(
return_value=httpx.Response(200, content=TTS_AUDIO, headers={"content-type": "audio/mpeg"})
)
litellm.speech(
model="hosted_vllm/tts-1",
input="which key goes out",
voice="alloy",
api_base="https://vllm.internal/v1",
api_key="caller-supplied-key",
)
assert route.calls.last.request.headers["authorization"] == "Bearer caller-supplied-key"
FOUNDRY_HOST: Final = "https://my-project.services.ai.azure.com"