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
Add extra_body support for response api params from chat completion (#16765)
Sameer Kankute9e93d65ee2bd
diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.pyindex 8c3ebd51036..54d32fe3466 100644--- a/litellm/completion_extras/litellm_responses_transformation/transformation.py+++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py@@ -26,7 +26,11 @@ from litellm.llms.base_llm.base_model_iterator import BaseModelResponseIteratorfrom litellm.llms.base_llm.bridges.completion_transformation import (CompletionTransformationBridge,)-from litellm.types.llms.openai import ChatCompletionToolParamFunctionChunk, Reasoning+from litellm.types.llms.openai import (+ ChatCompletionToolParamFunctionChunk,+ Reasoning,+ ResponsesAPIOptionalRequestParams,+)if TYPE_CHECKING:from openai.types.responses import ResponseInputImageParam@@ -165,13 +169,13 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):litellm_logging_obj: "LiteLLMLoggingObj",client: Optional[Any] = None,) -> dict:- from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams-(input_items,instructions,) = self.convert_chat_completion_messages_to_responses_api(messages)+ optional_params = self._extract_extra_body_params(optional_params)+# Build responses API request using the reverse transformation logicresponses_api_request = ResponsesAPIOptionalRequestParams()@@ -538,6 +542,35 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):return cast(List["ALL_RESPONSES_API_TOOL_PARAMS"], responses_tools)+ def _extract_extra_body_params(self, optional_params: dict):+ """+ Extract extra_body from optional_params and separate supported Responses API params+ from unsupported ones. Supported params are moved to top-level optional_params,+ unsupported params remain in extra_body.+ """+ # Extract extra_body and separate supported params from unsupported ones+ extra_body = optional_params.pop("extra_body", None) or {}+ if not extra_body:+ return optional_params++ supported_responses_api_params = set(+ ResponsesAPIOptionalRequestParams.__annotations__.keys()+ )+ # Also include params we handle specially+ supported_responses_api_params.update({+ "previous_response_id",+ "reasoning_effort", # We map this to "reasoning"+ })++ # Extract supported params from extra_body and merge into optional_params+ extra_body_copy = extra_body.copy()+ for key, value in extra_body_copy.items():+ if key in supported_responses_api_params:+ # Prefer extra_body value if it exists (may have more complete info like summary in reasoning_effort)+ optional_params[key] = extra_body.pop(key)++ return optional_params+def _map_reasoning_effort(self, reasoning_effort: Union[str, Dict[str, Any]]) -> Optional[Reasoning]:# If dict is passed, convert it directly to Reasoning objectif isinstance(reasoning_effort, dict):diff --git a/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.pyindex 40a6bbd8e0f..1b3bc5a92ef 100644--- a/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py+++ b/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py@@ -273,3 +273,39 @@ def test_convert_tools_to_responses_format():result = handler._convert_tools_to_responses_format(tools)assert result[0]["name"] == "test"+++def test_extract_extra_body_params_reasoning_effort_override():+ """Test that reasoning_effort from extra_body overrides top-level reasoning_effort"""+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ LiteLLMResponsesTransformationHandler,+ )++ handler = LiteLLMResponsesTransformationHandler()++ # Test case: reasoning_effort in extra_body (with summary) should override top-level string+ optional_params = {+ "reasoning_effort": "high", # Top-level string+ "extra_body": {+ "reasoning_effort": {+ "effort": "high",+ "summary": {"type": "summary_text"},+ }, # More complete dict in extra_body+ "previous_response_id": "resp_123", # Supported param+ "custom_param": "will_stay_in_extra_body", # Unsupported param+ },+ }++ result = handler._extract_extra_body_params(optional_params)++ # reasoning_effort from extra_body should override top-level+ assert result["reasoning_effort"] == {+ "effort": "high",+ "summary": {"type": "summary_text"},+ }++ # previous_response_id should be extracted to top-level+ assert result["previous_response_id"] == "resp_123"++ # extra_body should no longer be in optional_params (it was popped)+ assert "extra_body" not in result