From eb272f07840f7dc961311eb9f30e1a8be1ac1d21 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 13 Jun 2025 21:56:28 -0700 Subject: [PATCH] fix(responses/): filter metadata to be openai-compatible prevents optional params for responses api from including invalid args / litellm internal information Closes https://github.com/BerriAI/litellm/pull/11632#issuecomment-2971076684 --- .../litellm_responses_transformation/handler.py | 2 ++ litellm/responses/utils.py | 7 +++++++ litellm/utils.py | 10 ++++++++++ 3 files changed, 19 insertions(+) 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()