From 4e7115bc34818b991c38a0f9301ba8faf3785b5d Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Tue, 1 Jul 2025 18:12:22 -0700 Subject: [PATCH] Bug Fix - responses api fix got multiple values for keyword argument 'litellm_trace_id' (#12225) * fix - handling trace id arg on responses api * test_async_response_api_handler_merges_trace_id_without_error * test_anthropic_with_responses_api --- litellm/proxy/proxy_config.yaml | 5 ++- .../handler.py | 11 ++++-- .../test_anthropic_responses_api.py | 35 ++++++++++++++++++- .../test_e2e_openai_responses_api.py | 9 +++++ 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yaml index 13724177c05..8a8fd6794e7 100644 --- a/litellm/proxy/proxy_config.yaml +++ b/litellm/proxy/proxy_config.yaml @@ -1,8 +1,7 @@ model_list: - - model_name: groq/* + - model_name: anthropic/* litellm_params: - model: groq/* - api_key: bad + model: anthropic/* - model_name: openai/* litellm_params: model: openai/* diff --git a/litellm/responses/litellm_completion_transformation/handler.py b/litellm/responses/litellm_completion_transformation/handler.py index f960092f2bf..7dc182747df 100644 --- a/litellm/responses/litellm_completion_transformation/handler.py +++ b/litellm/responses/litellm_completion_transformation/handler.py @@ -56,6 +56,10 @@ class LiteLLMCompletionTransformationHandler: responses_api_request=responses_api_request, **kwargs, ) + + completion_args = {} + completion_args.update(kwargs) + completion_args.update(litellm_completion_request) litellm_completion_response: Union[ ModelResponse, litellm.CustomStreamWrapper @@ -98,12 +102,15 @@ class LiteLLMCompletionTransformationHandler: previous_response_id=previous_response_id, litellm_completion_request=litellm_completion_request, ) + + acompletion_args = {} + acompletion_args.update(kwargs) + acompletion_args.update(litellm_completion_request) litellm_completion_response: Union[ ModelResponse, litellm.CustomStreamWrapper ] = await litellm.acompletion( - **litellm_completion_request, - **kwargs, + **acompletion_args, ) if isinstance(litellm_completion_response, ModelResponse): diff --git a/tests/llm_responses_api_testing/test_anthropic_responses_api.py b/tests/llm_responses_api_testing/test_anthropic_responses_api.py index 3eabc0c15bc..1e24457145f 100644 --- a/tests/llm_responses_api_testing/test_anthropic_responses_api.py +++ b/tests/llm_responses_api_testing/test_anthropic_responses_api.py @@ -4,6 +4,10 @@ import pytest import asyncio from typing import Optional from unittest.mock import patch, AsyncMock +from litellm.responses.litellm_completion_transformation.handler import LiteLLMCompletionTransformationHandler +from litellm.responses.litellm_completion_transformation.transformation import LiteLLMCompletionResponsesConfig +from litellm.types.utils import ModelResponse + sys.path.insert(0, os.path.abspath("../..")) import litellm @@ -103,4 +107,33 @@ def test_multiturn_tool_calls(): print("follow_up_response=", follow_up_response) - \ No newline at end of file + + +@pytest.mark.asyncio +async def test_async_response_api_handler_merges_trace_id_without_error(): + handler = LiteLLMCompletionTransformationHandler() + + async def fake_session_handler(previous_response_id, litellm_completion_request): + litellm_completion_request["litellm_trace_id"] = "session-trace" + return litellm_completion_request + + with patch.object( + LiteLLMCompletionResponsesConfig, + "async_responses_api_session_handler", + side_effect=fake_session_handler, + ): + with patch("litellm.acompletion", new_callable=AsyncMock) as mock_acompletion: + mock_acompletion.return_value = ModelResponse( + id="id", created=0, model="test", object="chat.completion", choices=[] + ) + await handler.async_response_api_handler( + litellm_completion_request={"model": "test"}, + request_input="hi", + responses_api_request={"previous_response_id": "123"}, + litellm_trace_id="original-trace", + ) + # ensure acompletion called once with merged trace_id + assert mock_acompletion.call_count == 1 + assert ( + mock_acompletion.call_args.kwargs["litellm_trace_id"] == "session-trace" + ) \ No newline at end of file diff --git a/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py b/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py index c2e17d3ef46..e637066d2f9 100644 --- a/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py +++ b/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py @@ -119,3 +119,12 @@ def test_bad_request_bad_param_error(): client.responses.create( model="gpt-4o", input="This should fail", temperature=2000 ) + +def test_anthropic_with_responses_api(): + client = get_test_client() + response = client.responses.create( + model="anthropic/claude-3-5-sonnet-20240620", + input="just respond with the word 'ping'", + previous_response_id="hi", + ) + print("anthropic response=", response)