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: fixed openai conversion from responses to completions (#16864)
Nigel Kukardc5c563c30240
diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.pyindex 54d32fe3466..588fdd66350 100644--- a/litellm/completion_extras/litellm_responses_transformation/transformation.py+++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py@@ -30,6 +30,7 @@ from litellm.types.llms.openai import (ChatCompletionToolParamFunctionChunk,Reasoning,ResponsesAPIOptionalRequestParams,+ ResponsesAPIStreamEvents,)if TYPE_CHECKING:@@ -652,6 +653,8 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator):# Handle different event types from responses APIevent_type = parsed_chunk.get("type")+ if isinstance(event_type, ResponsesAPIStreamEvents):+ event_type = event_type.valueverbose_logger.debug(f"Chat provider: Processing event type: {event_type}")if event_type == "response.created":@@ -671,7 +674,7 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator):index=0,type="function",function=ChatCompletionToolCallFunctionChunk(- name=parsed_chunk.get("name", None),+ name=output_item.get("name", None),arguments=parsed_chunk.get("arguments", ""),),),@@ -717,7 +720,7 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator):index=0,type="function",function=ChatCompletionToolCallFunctionChunk(- name=parsed_chunk.get("name", None),+ name=output_item.get("name", None),arguments="", # responses API sends everything again, we don't),),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 1b3bc5a92ef..e9c71ef43c4 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@@ -86,6 +86,73 @@ def test_openai_responses_chunk_parser_reasoning_summary():assert delta.function_call is None+def test_chunk_parser_string_output_text_delta_produces_text():+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )++ iterator = OpenAiResponsesToChatCompletionStreamIterator(+ streaming_response=None, sync_stream=True+ )++ chunk = {"type": "response.output_text.delta", "delta": "literal text"}++ result = iterator.chunk_parser(chunk)++ assert result["text"] == "literal text"+ assert result.get("tool_use") is None+ assert result.get("finish_reason") == ""+ assert not result.get("is_finished")+++def test_chunk_parser_enum_output_text_delta_produces_text():+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )+ from litellm.types.llms.openai import ResponsesAPIStreamEvents++ iterator = OpenAiResponsesToChatCompletionStreamIterator(+ streaming_response=None, sync_stream=True+ )++ chunk = {"type": ResponsesAPIStreamEvents.OUTPUT_TEXT_DELTA, "delta": "enum text"}++ result = iterator.chunk_parser(chunk)++ assert result["text"] == "enum text"+ assert result.get("tool_use") is None+ assert result.get("finish_reason") == ""+ assert not result.get("is_finished")+++def test_chunk_parser_function_call_added_produces_tool_use():+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )+ from litellm.types.llms.openai import ResponsesAPIStreamEvents++ iterator = OpenAiResponsesToChatCompletionStreamIterator(+ streaming_response=None, sync_stream=True+ )++ chunk = {+ "type": ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED,+ "arguments": '{"key": "value"}',+ "item": {"type": "function_call", "name": "fn", "call_id": "call-42"},+ }++ result = iterator.chunk_parser(chunk)++ tool_use = result["tool_use"]+ assert tool_use is not None+ assert tool_use["id"] == "call-42"+ assert tool_use["type"] == "function"+ assert tool_use["function"]["name"] == "fn"+ assert tool_use["function"]["arguments"] == '{"key": "value"}'+ assert result.get("finish_reason") == ""+ assert not result.get("is_finished")++def test_transform_response_with_reasoning_and_output():"""Test transform_response handles ResponsesAPIResponse with reasoning items and output messages."""from unittest.mock import Mock