fix: forward metadata into speech() litellm_params_dict so Gemini TTS spend is tracked

## TLDR

Signed-off-by: Ishaan <ishaangupta0408@gmail.com>
This commit is contained in:
Ishaan 2026-08-15 09:22:51 +00:00
parent 7a5b98e6a9
commit f5593c38cd
2 changed files with 40 additions and 1 deletions

View file

@ -7945,7 +7945,7 @@ def speech(
if max_retries is None:
max_retries = litellm.num_retries or openai.DEFAULT_MAX_RETRIES
litellm_params_dict: Final = get_litellm_params(**kwargs)
litellm_params_dict: Final = get_litellm_params(metadata=metadata, **kwargs)
# Get provider-specific text-to-speech config and map parameters
text_to_speech_provider_config = ProviderConfigManager.get_provider_text_to_speech_config(

View file

@ -420,5 +420,44 @@ class TestGeminiTTSSpeechConfigInRequestBody:
assert "AUDIO" in generation_config["responseModalities"]
class TestSpeechToCompletionBridgeMetadata:
"""Regression for #37015: metadata must be forwarded into the inner completion call."""
def test_metadata_forwarded_to_bridge(self):
from unittest.mock import MagicMock, patch
from litellm.endpoints.speech.speech_to_completion_bridge.transformation import (
SpeechToCompletionBridgeTransformationHandler,
)
from litellm.litellm_core_utils.get_litellm_params import get_litellm_params
metadata = {"user_api_key": "test_key_hash", "user_api_key_user_id": "user123"}
litellm_params_dict = get_litellm_params(metadata=metadata)
assert litellm_params_dict["metadata"] == metadata, (
"metadata must be present in litellm_params_dict; without this the inner "
"completion call overwrites it with None and spend tracking is lost"
)
handler = SpeechToCompletionBridgeTransformationHandler()
logging_obj = MagicMock()
request_kwargs = handler.transform_request(
model="gemini-2.5-flash-preview-tts",
input="hello",
voice="Kore",
optional_params={},
litellm_params=litellm_params_dict,
headers={},
litellm_logging_obj=logging_obj,
custom_llm_provider="gemini",
)
assert request_kwargs["metadata"] == metadata, (
"metadata must survive transform_request so the inner completion call "
"does not overwrite the logging object's metadata with None"
)
if __name__ == "__main__":
pytest.main([__file__])