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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix responses bridge metadata isolation (#20484)
Zero Clover704451240784
diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.pyindex 753a94295b3..b2a5e2fa59d 100644--- a/litellm/completion_extras/litellm_responses_transformation/transformation.py+++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py@@ -271,8 +271,6 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):responses_api_request["text"] = text_format # type: ignoreelif key in ResponsesAPIOptionalRequestParams.__annotations__.keys():responses_api_request[key] = value # type: ignore- elif key == "metadata":- responses_api_request["metadata"] = valueelif key == "previous_response_id":responses_api_request["previous_response_id"] = valueelif key == "reasoning_effort":@@ -304,11 +302,32 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):setattr(litellm_logging_obj, "call_type", CallTypes.responses.value)+ responses_optional_param_keys = set(+ ResponsesAPIOptionalRequestParams.__annotations__.keys()+ )+ sanitized_litellm_params: Dict[str, Any] = {+ key: value+ for key, value in litellm_params.items()+ if key not in responses_optional_param_keys+ }++ legacy_metadata = litellm_params.get("metadata")+ existing_litellm_metadata = litellm_params.get("litellm_metadata")+ merged_litellm_metadata: Dict[str, Any] = {}+ if isinstance(legacy_metadata, dict):+ merged_litellm_metadata.update(legacy_metadata)+ if isinstance(existing_litellm_metadata, dict):+ merged_litellm_metadata.update(existing_litellm_metadata)+ if merged_litellm_metadata:+ sanitized_litellm_params["litellm_metadata"] = merged_litellm_metadata+ else:+ sanitized_litellm_params.pop("litellm_metadata", None)+request_data = {"model": api_model,"input": input_items,"litellm_logging_obj": litellm_logging_obj,- **litellm_params,+ **sanitized_litellm_params,"client": client,}diff --git a/tests/test_litellm/completion_extras/test_litellm_responses_transformation_transformation.py b/tests/test_litellm/completion_extras/test_litellm_responses_transformation_transformation.pyindex 57352eafaf1..5458b466f68 100644--- a/tests/test_litellm/completion_extras/test_litellm_responses_transformation_transformation.py+++ b/tests/test_litellm/completion_extras/test_litellm_responses_transformation_transformation.py@@ -156,3 +156,83 @@ def test_transform_request_includes_extra_headers():litellm_logging_obj=MockLoggingObj(),)assert result.get("extra_headers") == headers+++def test_transform_request_strips_internal_metadata_to_litellm_metadata():+ handler = LiteLLMResponsesTransformationHandler()+ messages = [{"role": "user", "content": "Hello"}]+ optional_params = {}+ litellm_params = {+ "metadata": {"user_api_key_auth": {"id": "abc"}},+ "litellm_metadata": {"trace_id": "trace-1"},+ "api_key": "sk-test",+ }++ class MockLoggingObj:+ pass++ result = handler.transform_request(+ model="gpt-5-pro",+ messages=messages,+ optional_params=optional_params,+ litellm_params=litellm_params,+ headers={},+ litellm_logging_obj=MockLoggingObj(),+ )++ assert "metadata" not in result+ assert result["litellm_metadata"]["user_api_key_auth"]["id"] == "abc"+ assert result["litellm_metadata"]["trace_id"] == "trace-1"+++def test_transform_request_preserves_user_metadata():+ handler = LiteLLMResponsesTransformationHandler()+ messages = [{"role": "user", "content": "Hello"}]+ optional_params = {"metadata": {"customer_id": "cust-123"}}+ litellm_params = {"metadata": {"internal_key": "secret"}}++ class MockLoggingObj:+ pass++ result = handler.transform_request(+ model="gpt-5-pro",+ messages=messages,+ optional_params=optional_params,+ litellm_params=litellm_params,+ headers={},+ litellm_logging_obj=MockLoggingObj(),+ )++ assert result["metadata"] == {"customer_id": "cust-123"}+ assert "internal_key" not in result["metadata"]+ assert result["litellm_metadata"]["internal_key"] == "secret"+++def test_transform_request_drops_user_metadata_with_additional_drop_params():+ from litellm.utils import get_optional_params++ handler = LiteLLMResponsesTransformationHandler()+ messages = [{"role": "user", "content": "Hello"}]+ optional_params = get_optional_params(+ model="gpt-4o",+ messages=messages,+ metadata={"customer_id": "cust-123"},+ additional_drop_params=["metadata"],+ custom_llm_provider="openai",+ )+ litellm_params = {"metadata": {"internal_key": "secret"}}++ class MockLoggingObj:+ pass++ result = handler.transform_request(+ model="gpt-4o",+ messages=messages,+ optional_params=optional_params,+ litellm_params=litellm_params,+ headers={},+ litellm_logging_obj=MockLoggingObj(),+ )++ assert "metadata" not in result+ assert result["litellm_metadata"]["internal_key"] == "secret"