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(responses/): filter metadata to be openai-compatible

Krrish Dholakiaeb272f07840f
diff --git a/litellm/completion_extras/litellm_responses_transformation/handler.py b/litellm/completion_extras/litellm_responses_transformation/handler.py
index ae73f153f22..a799bca437b 100644
--- a/litellm/completion_extras/litellm_responses_transformation/handler.py
+++ b/litellm/completion_extras/litellm_responses_transformation/handler.py
@@ -1,6 +1,7 @@
"""
Handler for transforming /chat/completions api requests to litellm.responses requests
"""
+
from typing import TYPE_CHECKING, Any, Coroutine, TypedDict, Union
if TYPE_CHECKING:
@@ -163,6 +164,7 @@ class ResponsesToCompletionBridgeHandler:
headers=headers,
litellm_logging_obj=logging_obj,
)
+
result = await aresponses(
**request_data,
aresponses=True,
diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py
index 0d8b1e61508..83510ffea86 100644
--- a/litellm/responses/utils.py
+++ b/litellm/responses/utils.py
@@ -87,6 +87,13 @@ class ResponsesAPIRequestUtils:
)
filtered_params["previous_response_id"] = decoded_previous_response_id
+ if "metadata" in filtered_params:
+ from litellm.utils import add_openai_metadata
+
+ filtered_params["metadata"] = add_openai_metadata(
+ filtered_params["metadata"]
+ )
+
return cast(ResponsesAPIOptionalRequestParams, filtered_params)
@staticmethod
diff --git a/litellm/utils.py b/litellm/utils.py
index bb500181d25..e8eade6871e 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -7147,6 +7147,16 @@ def add_openai_metadata(metadata: dict) -> dict:
if k != "hidden_params" and isinstance(v, (str))
}
+ # max 16 keys allowed by openai - trim down to 16
+ if len(visible_metadata) > 16:
+ filtered_metadata = {}
+ idx = 0
+ for k, v in visible_metadata.items():
+ if idx < 16:
+ filtered_metadata[k] = v
+ idx += 1
+ visible_metadata = filtered_metadata
+
return visible_metadata.copy()