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
Merge branch 'main' into litellm_oss_staging_02_18_2026
Krish Dholakia992510154cfe
diff --cc litellm/responses/litellm_completion_transformation/transformation.pyindex 8daa8e49d1e,19845d7c493..1ab5d7bf239--- a/litellm/responses/litellm_completion_transformation/transformation.py+++ b/litellm/responses/litellm_completion_transformation/transformation.py@@@ -738,15 -739,31 +739,31 @@@ class LiteLLMCompletionResponsesConfig@staticmethoddef _ensure_tool_results_have_corresponding_tool_calls(- messages: List[Union[AllMessageValues, GenericChatCompletionMessage, ChatCompletionResponseMessage]],+ messages: Sequence[+ Union[+ AllMessageValues,+ GenericChatCompletionMessage,+ ChatCompletionResponseMessage,+ ChatCompletionMessageToolCall,+ Message,+ ]+ ],tools: Optional[List[Any]] = None,- ) -> List[Union[AllMessageValues, GenericChatCompletionMessage, ChatCompletionResponseMessage]]:+ ) -> List[+ Union[+ AllMessageValues,+ GenericChatCompletionMessage,+ ChatCompletionResponseMessage,+ ChatCompletionMessageToolCall,+ Message,+ ]+ ]:"""Ensure that tool_result messages have corresponding tool_calls in the previous assistant message.-+This is critical for Anthropic API which requires that each tool_result block has acorresponding tool_use block in the previous assistant message.-+Args:messages: List of messages that may include tool_result messagestools: Optional list of tools that can be used to reconstruct tool_calls if not in cache@@@ -755,13 -772,21 +772,21 @@@List of messages with tool_calls added to assistant messages when needed"""if not messages:- return messages+ return list(messages)- # Create a deep copy to avoid modifying the original+ # Create a deep copy to avoid modifying the original (use list() so we can mutate and return List)import copy- fixed_messages = copy.deepcopy(messages)+ fixed_messages: List[+ Union[+ AllMessageValues,+ GenericChatCompletionMessage,+ ChatCompletionResponseMessage,+ ChatCompletionMessageToolCall,+ Message,+ ]+ ] = list(copy.deepcopy(messages))messages_to_remove = []-+# Count non-tool messages to avoid removing all messages# This prevents empty messages list when using previous_response_id without a databasenon_tool_messages_count = sum(diff --cc tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.pyindex 25e8a1f3304,ef3d7534d97..b29623f626f--- 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@@@ -1280,135 -1327,454 +1327,586 @@@ def test_transform_response_preserves_aprint("✓ Annotations from Responses API are correctly preserved in Chat Completions format")+def test_apply_patch_tool_call_converted_to_chat_completion_tool_call():+ """+ Test that ResponseApplyPatchToolCall items from the Responses API are+ correctly converted to ChatCompletions-style tool calls by the bridge.++ This is a regression test for a bug where litellm.completion() with a+ responses/ model prefix crashed when the model returned an+ apply_patch_call, because _convert_response_output_to_choices did not+ handle ResponseApplyPatchToolCall items. The model DID use the tool,+ but the bridge silently dropped it (or raised an error), while the+ native litellm.responses() path worked correctly.+ """+ import json+ from unittest.mock import Mock++ from openai.types.responses.response_apply_patch_tool_call import (+ OperationCreateFile,+ )+ from openai.types.responses.response_output_item import (+ ResponseApplyPatchToolCall,+ )++ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ LiteLLMResponsesTransformationHandler,+ )+ from litellm.types.llms.openai import (+ InputTokensDetails,+ OutputTokensDetails,+ ResponseAPIUsage,+ ResponsesAPIResponse,+ )+ from litellm.types.utils import ModelResponse, Usage++ handler = LiteLLMResponsesTransformationHandler()++ # Build an apply_patch_call item like the model would return+ operation = OperationCreateFile(+ diff="--- /dev/null\n+++ b/hello.py\n@@ -0,0 +1 @@\n+print('hello world')\n",+ path="hello.py",+ type="create_file",+ )+ apply_patch_item = ResponseApplyPatchToolCall(+ id="apc_001",+ call_id="call_patch_hello",+ operation=operation,+ status="completed",+ type="apply_patch_call",+ )++ # Minimal usage+ usage = ResponseAPIUsage(+ input_tokens=30,+ input_tokens_details=InputTokensDetails(cached_tokens=0),+ output_tokens=40,+ output_tokens_details=OutputTokensDetails(reasoning_tokens=0),+ total_tokens=70,+ )++ raw_response = ResponsesAPIResponse(+ id="resp_apply_patch_test",+ created_at=1234567890,+ error=None,+ incomplete_details=None,+ instructions=None,+ metadata={},+ model="gpt-5.2-codex",+ object="response",+ output=[apply_patch_item],+ parallel_tool_calls=True,+ temperature=1.0,+ tool_choice="auto",+ tools=[],+ top_p=1.0,+ max_output_tokens=None,+ previous_response_id=None,+ reasoning=None,+ status="completed",+ text=None,+ truncation="disabled",+ usage=usage,+ user=None,+ store=True,+ background=False,+ )++ model_response = ModelResponse(+ id="chatcmpl-apply-patch",+ created=1234567890,+ model=None,+ object="chat.completion",+ choices=[],+ usage=Usage(completion_tokens=0, prompt_tokens=0, total_tokens=0),+ )++ logging_obj = Mock()++ result = handler.transform_response(+ model="gpt-5.2-codex",+ raw_response=raw_response,+ model_response=model_response,+ logging_obj=logging_obj,+ request_data={"model": "gpt-5.2-codex"},+ messages=[+ {"role": "system", "content": "You are a coding assistant."},+ {"role": "user", "content": "Create hello.py"},+ ],+ optional_params={},+ litellm_params={},+ encoding=Mock(),+ )++ # Should have exactly one choice with finish_reason="tool_calls"+ assert len(result.choices) == 1, f"Expected 1 choice, got {len(result.choices)}"++ choice = result.choices[0]+ assert choice.finish_reason == "tool_calls"++ # The choice should contain one tool call for apply_patch+ tool_calls = choice.message.tool_calls+ assert tool_calls is not None, "tool_calls should not be None"+ assert len(tool_calls) == 1, f"Expected 1 tool_call, got {len(tool_calls)}"++ tc = tool_calls[0]+ assert tc["id"] == "call_patch_hello"+ assert tc["type"] == "function"+ assert tc["function"]["name"] == "apply_patch"++ # The operation should be serialised as JSON in arguments+ args = json.loads(tc["function"]["arguments"])+ assert args["type"] == "create_file"+ assert args["path"] == "hello.py"+ assert "print('hello world')" in args["diff"]+ def test_multi_tool_call_stream_no_premature_finish():+ """+ Regression test for multi-tool-call streaming bug.++ When a response contains multiple tool calls, the stream used to be prematurely+ terminated after the first output_item.done event because that handler emitted+ finish_reason="tool_calls". This caused ~58% of streaming requests with multiple+ tool calls to fail.++ The fix: output_item.done for function_call emits delta=Delta() and finish_reason=None.+ Only response.completed emits the terminal finish_reason.++ Synthetic event sequence:+ response.created+ response.output_item.added (function_call: read_file, call_id: call_1)+ response.function_call_arguments.delta (read_file args)+ response.output_item.done (function_call: read_file) <- must NOT end stream+ response.output_item.added (function_call: list_dir, call_id: call_2)+ response.function_call_arguments.delta (list_dir args)+ response.output_item.done (function_call: list_dir) <- must NOT end stream+ response.completed (response with 2 function_call outputs) <- terminal+ """+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )++ iterator = OpenAiResponsesToChatCompletionStreamIterator(streaming_response=None, sync_stream=True)++ chunks = [+ # 0: response created+ {"type": "response.created", "response": {"id": "resp_001", "status": "in_progress"}},+ # 1: first tool call added+ {+ "type": "response.output_item.added",+ "item": {"type": "function_call", "name": "read_file", "call_id": "call_1"},+ },+ # 2: first tool call arguments delta+ {"type": "response.function_call_arguments.delta", "delta": '{"path":"/etc/hostname"}'},+ # 3: first tool call done ← must NOT emit finish_reason+ {+ "type": "response.output_item.done",+ "item": {+ "type": "function_call",+ "name": "read_file",+ "call_id": "call_1",+ "arguments": '{"path":"/etc/hostname"}',+ },+ },+ # 4: second tool call added+ {+ "type": "response.output_item.added",+ "item": {"type": "function_call", "name": "list_dir", "call_id": "call_2"},+ },+ # 5: second tool call arguments delta+ {"type": "response.function_call_arguments.delta", "delta": '{"path":"/tmp"}'},+ # 6: second tool call done ← must NOT emit finish_reason+ {+ "type": "response.output_item.done",+ "item": {+ "type": "function_call",+ "name": "list_dir",+ "call_id": "call_2",+ "arguments": '{"path":"/tmp"}',+ },+ },+ # 7: response completed with both tool calls in output ← ONLY terminal chunk+ {+ "type": "response.completed",+ "response": {+ "id": "resp_001",+ "status": "completed",+ "output": [+ {+ "type": "function_call",+ "name": "read_file",+ "call_id": "call_1",+ "arguments": '{"path":"/etc/hostname"}',+ },+ {+ "type": "function_call",+ "name": "list_dir",+ "call_id": "call_2",+ "arguments": '{"path":"/tmp"}',+ },+ ],+ },+ },+ ]++ results = [iterator.chunk_parser(chunk) for chunk in chunks]++ # 1. output_item.done events (indices 3 and 6) must NOT emit finish_reason+ for done_idx, label in [(3, "read_file done"), (6, "list_dir done")]:+ r = results[done_idx]+ assert r is not None, f"{label}: chunk_parser must return a result"+ assert len(r.choices) > 0, f"{label}: result must have choices"+ assert r.choices[0].finish_reason is None, (+ f"{label}: output_item.done must not emit finish_reason (stream would terminate prematurely)"+ )+ assert not r.choices[0].delta.tool_calls, (+ f"{label}: output_item.done must not include a duplicate tool_calls delta"+ )++ # 2. output_item.added events (indices 1 and 4) should carry name + call_id+ for added_idx, expected_name, expected_call_id in [+ (1, "read_file", "call_1"),+ (4, "list_dir", "call_2"),+ ]:+ r = results[added_idx]+ if r is not None and r.choices and r.choices[0].delta.tool_calls:+ tc = r.choices[0].delta.tool_calls[0]+ assert tc.function.name == expected_name, (+ f"output_item.added for {expected_name}: tool_call name mismatch"+ )+ assert tc.id == expected_call_id, (+ f"output_item.added for {expected_name}: call_id mismatch"+ )++ # 3. argument delta events (indices 2 and 5) should carry arguments+ for delta_idx, expected_args, label in [+ (2, '{"path":"/etc/hostname"}', "read_file args"),+ (5, '{"path":"/tmp"}', "list_dir args"),+ ]:+ r = results[delta_idx]+ if r is not None and r.choices and r.choices[0].delta.tool_calls:+ tc = r.choices[0].delta.tool_calls[0]+ assert tc.function.arguments == expected_args, (+ f"{label}: argument delta mismatch"+ )++ # 4. Only response.completed (index 7) emits the terminal finish_reason+ completed_result = results[7]+ assert completed_result is not None, "response.completed must return a result"+ assert len(completed_result.choices) > 0, "response.completed must have choices"+ assert completed_result.choices[0].finish_reason == "tool_calls", (+ "response.completed with function_call outputs must emit finish_reason='tool_calls'"+ )++ # 5. No chunk before the last one should have finish_reason set+ for idx, r in enumerate(results[:-1]):+ if r is not None and r.choices:+ assert r.choices[0].finish_reason is None, (+ f"Chunk at index {idx} (type={chunks[idx]['type']!r}) must not emit finish_reason "+ f"— only response.completed should terminate the stream"+ )++ print("✓ Multi-tool-call stream completes without premature finish_reason termination")+++ # =============================================================================+ # Tests for issue #21331: Parallel tool call indices in streaming+ # =============================================================================+++ def test_streaming_parallel_tool_calls_have_distinct_indices():+ """+ Test that parallel tool calls get distinct indices matching output_index+ from the Responses API streaming chunks.++ Regression test for issue #21331 where all tool calls were emitted with+ index=0, making it impossible to distinguish parallel calls.+ """+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )++ # Simulate two parallel tool calls with output_index 0 and 1+ chunks = [+ {+ "type": "response.output_item.added",+ "output_index": 0,+ "item": {+ "type": "function_call",+ "id": "fc_001",+ "call_id": "call_abc",+ "name": "get_weather",+ "arguments": "",+ },+ },+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 0,+ "item_id": "fc_001",+ "delta": '{"city": "SF"}',+ },+ {+ "type": "response.output_item.done",+ "output_index": 0,+ "item": {+ "type": "function_call",+ "id": "fc_001",+ "call_id": "call_abc",+ "name": "get_weather",+ "arguments": '{"city": "SF"}',+ },+ },+ {+ "type": "response.output_item.added",+ "output_index": 1,+ "item": {+ "type": "function_call",+ "id": "fc_002",+ "call_id": "call_def",+ "name": "get_weather",+ "arguments": "",+ },+ },+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 1,+ "item_id": "fc_002",+ "delta": '{"city": "NY"}',+ },+ {+ "type": "response.output_item.done",+ "output_index": 1,+ "item": {+ "type": "function_call",+ "id": "fc_002",+ "call_id": "call_def",+ "name": "get_weather",+ "arguments": '{"city": "NY"}',+ },+ },+ ]++ for chunk in chunks:+ result = OpenAiResponsesToChatCompletionStreamIterator.translate_responses_chunk_to_openai_stream(+ chunk+ )+ expected_index = chunk["output_index"]+ for choice in result.choices:+ if choice.delta.tool_calls:+ for tc in choice.delta.tool_calls:+ assert tc.index == expected_index, (+ f"Event {chunk['type']}: expected tool_call.index={expected_index}, "+ f"got {tc.index}"+ )+++ # =============================================================================+ # Comprehensive integration test: parallel tool calls with split argument deltas+ # =============================================================================+++ def test_parallel_tool_calls_comprehensive_streaming_integration():+ """+ Comprehensive integration test for parallel tool calls via Responses API streaming.++ Regression test combining all fix invariants in a single end-to-end scenario+ with split argument deltas — the exact event sequence that was broken before+ the fix to output_item.done.++ Synthesized SSE event sequence:+ response.created+ response.output_item.added {output_index:0, type:function_call, call_id:call_1, name:read_file}+ response.function_call_arguments.delta {output_index:0, delta:'{"path"'}+ response.function_call_arguments.delta {output_index:0, delta:'":"/etc/foo"}'}+ response.output_item.done {output_index:0, item:{type:function_call, call_id:call_1}}+ response.output_item.added {output_index:1, type:function_call, call_id:call_2, name:list_dir}+ response.function_call_arguments.delta {output_index:1, delta:'{"path"'}+ response.function_call_arguments.delta {output_index:1, delta:'":"/tmp"}'}+ response.output_item.done {output_index:1, item:{type:function_call, call_id:call_2}}+ response.completed {response:{status:completed, output:[call_1, call_2]}}++ Asserts:+ 1. No output_item.done chunk emits finish_reason (no premature stream termination)+ 2. Each call_id appears exactly once in assembled tool_call IDs (no duplicates)+ 3. Final assembled arguments are correct — split deltas concatenate to valid JSON+ 4. Exactly one finish event, at the final response.completed chunk+ 5. Two parallel tool calls have distinct indices (output_index 0 and 1)+ """+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ OpenAiResponsesToChatCompletionStreamIterator,+ )++ chunks = [+ # 0: response.created+ {"type": "response.created", "response": {"id": "resp_001", "status": "in_progress"}},+ # 1: call_1 (read_file) added — output_index=0+ {+ "type": "response.output_item.added",+ "output_index": 0,+ "item": {"type": "function_call", "name": "read_file", "call_id": "call_1"},+ },+ # 2: call_1 argument delta part 1 — split across two deltas+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 0,+ "delta": '{"path":',+ },+ # 3: call_1 argument delta part 2+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 0,+ "delta": '"/etc/foo"}',+ },+ # 4: call_1 done — must NOT emit finish_reason or duplicate tool_call chunk+ {+ "type": "response.output_item.done",+ "output_index": 0,+ "item": {+ "type": "function_call",+ "name": "read_file",+ "call_id": "call_1",+ "arguments": '{"path":"/etc/foo"}', # full JSON, assembled from the two deltas+ },+ },+ # 5: call_2 (list_dir) added — output_index=1+ {+ "type": "response.output_item.added",+ "output_index": 1,+ "item": {"type": "function_call", "name": "list_dir", "call_id": "call_2"},+ },+ # 6: call_2 argument delta part 1+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 1,+ "delta": '{"path":',+ },+ # 7: call_2 argument delta part 2+ {+ "type": "response.function_call_arguments.delta",+ "output_index": 1,+ "delta": '"/tmp"}',+ },+ # 8: call_2 done — must NOT emit finish_reason or duplicate tool_call chunk+ {+ "type": "response.output_item.done",+ "output_index": 1,+ "item": {+ "type": "function_call",+ "name": "list_dir",+ "call_id": "call_2",+ "arguments": '{"path":"/tmp"}',+ },+ },+ # 9: response.completed — the ONLY terminal chunk+ {+ "type": "response.completed",+ "response": {+ "id": "resp_001",+ "status": "completed",+ "output": [+ {+ "type": "function_call",+ "name": "read_file",+ "call_id": "call_1",+ "arguments": '{"path":"/etc/foo"}',+ },+ {+ "type": "function_call",+ "name": "list_dir",+ "call_id": "call_2",+ "arguments": '{"path":"/tmp"}',+ },+ ],+ },+ },+ ]++ iterator = OpenAiResponsesToChatCompletionStreamIterator(streaming_response=None, sync_stream=True)+ results = [iterator.chunk_parser(chunk) for chunk in chunks]++ # 1. output_item.done events (indices 4 and 8) must NOT emit finish_reason+ for done_idx, label in [(4, "read_file done"), (8, "list_dir done")]:+ r = results[done_idx]+ assert r is not None, f"{label}: chunk_parser must return a result"+ assert len(r.choices) > 0, f"{label}: result must have choices"+ assert r.choices[0].finish_reason is None, (+ f"{label}: output_item.done must not emit finish_reason "+ f"(would prematurely terminate stream before subsequent tool calls arrive)"+ )+ assert not r.choices[0].delta.tool_calls, (+ f"{label}: output_item.done must not emit a duplicate tool_calls delta"+ )++ # 2. Each call_id appears exactly once in assembled tool_call IDs+ # Only output_item.added emits id-bearing tool_call chunks; output_item.done emits Delta()+ all_tool_call_ids = [+ tc.id+ for r in results+ if r is not None and r.choices and r.choices[0].delta.tool_calls+ for tc in r.choices[0].delta.tool_calls+ if tc.id+ ]+ assert all_tool_call_ids.count("call_1") == 1, (+ f"call_1 must appear exactly once in assembled tool_call IDs, "+ f"got {all_tool_call_ids.count('call_1')} (duplicates indicate output_item.done still emits tool_call)"+ )+ assert all_tool_call_ids.count("call_2") == 1, (+ f"call_2 must appear exactly once in assembled tool_call IDs, "+ f"got {all_tool_call_ids.count('call_2')} (duplicates indicate output_item.done still emits tool_call)"+ )++ # 3. Final assembled arguments are correct when split deltas are concatenated+ # output_item.added emits arguments="" (empty); the two deltas provide the content+ assembled_args: dict = {}+ for r in results:+ if r is None or not r.choices:+ continue+ tool_calls = r.choices[0].delta.tool_calls+ if not tool_calls:+ continue+ for tc in tool_calls:+ if tc.function and tc.function.arguments:+ idx = tc.index+ assembled_args[idx] = assembled_args.get(idx, "") + tc.function.arguments++ # delta 1 = '{"path":' + delta 2 = '"/etc/foo"}' → '{"path":"/etc/foo"}'+ assert assembled_args.get(0) == '{"path":"/etc/foo"}', (+ f"Assembled args for index 0 (read_file): "+ f"expected '{{\"path\":\"/etc/foo\"}}', got '{assembled_args.get(0)}'"+ )+ # delta 1 = '{"path":' + delta 2 = '"/tmp"}' → '{"path":"/tmp"}'+ assert assembled_args.get(1) == '{"path":"/tmp"}', (+ f"Assembled args for index 1 (list_dir): "+ f"expected '{{\"path\":\"/tmp\"}}', got '{assembled_args.get(1)}'"+ )++ # 4. Stream terminates with exactly one finish event, at the final response.completed chunk+ finish_events = [+ (i, r.choices[0].finish_reason)+ for i, r in enumerate(results)+ if r is not None and r.choices and r.choices[0].finish_reason+ ]+ assert len(finish_events) == 1, (+ f"Expected exactly 1 finish event, got {len(finish_events)}: {finish_events}"+ )+ assert finish_events[0][0] == len(chunks) - 1, (+ f"Finish event must be at the last chunk (index {len(chunks) - 1}), "+ f"but was at index {finish_events[0][0]}"+ )+ assert finish_events[0][1] == "tool_calls", (+ f"Terminal finish_reason must be 'tool_calls', got '{finish_events[0][1]}'"+ )++ # 5. Parallel tool calls have distinct indices matching output_index (0 and 1)+ # Collect indices from output_item.added chunks only (they carry the call id)+ added_tool_call_indices = [+ tc.index+ for r in results+ if r is not None and r.choices and r.choices[0].delta.tool_calls+ for tc in r.choices[0].delta.tool_calls+ if tc.id # output_item.added chunks carry the id; argument deltas do not+ ]+ assert set(added_tool_call_indices) == {0, 1}, (+ f"Parallel tool calls must have distinct indices {{0, 1}}, got: {set(added_tool_call_indices)}"+ )++ print("✓ Parallel tool calls with split argument deltas stream correctly end-to-end")