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_consistent_id_streaming_responses
Sameer Kankutea3c1f4758dc1
diff --cc litellm/responses/litellm_completion_transformation/streaming_iterator.pyindex ece94cd4214,dd7936059a0..b128452d9a4--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py@@@ -79,8 -81,161 +81,163 @@@ class LiteLLMCompletionStreamingIteratoUnion[ModelResponse, TextCompletionResponse]] = Noneself.final_text: str = ""+ self._cached_item_id: Optional[str] = None+ self._cached_response_id: Optional[str] = None+ self._pending_tool_events: List[BaseLiteLLMOpenAIResponseObject] = []+ self._tool_output_index_by_call_id: dict[str, int] = {}+ self._tool_args_by_call_id: dict[str, str] = {}+ self._next_tool_output_index: int = 1 # output_index=0 reserved for the message item+ self._final_tool_events_queued: bool = False++ def _get_or_assign_tool_output_index(self, call_id: str) -> int:+ existing = self._tool_output_index_by_call_id.get(call_id)+ if existing is not None:+ return existing+ idx = self._next_tool_output_index+ self._next_tool_output_index += 1+ self._tool_output_index_by_call_id[call_id] = idx+ return idx++ def _queue_tool_call_delta_events(self, tool_calls: object) -> None:+ """+ Convert chat-completions streaming `tool_calls` deltas into Responses API streaming events.++ We emit:+ - response.output_item.added (function_call)+ - response.function_call_arguments.delta+ """+ if not isinstance(tool_calls, list):+ return++ for tc in tool_calls:+ call_id_raw = tc.get("id") if isinstance(tc, dict) else getattr(tc, "id", None)+ if not call_id_raw:+ continue+ call_id = str(call_id_raw)++ fn = tc.get("function") if isinstance(tc, dict) else getattr(tc, "function", None)+ fn_name = ""+ fn_args_delta = ""+ if isinstance(fn, dict):+ fn_name = str(fn.get("name") or "")+ fn_args_delta = str(fn.get("arguments") or "")+ else:+ fn_name = str(getattr(fn, "name", "") or "")+ fn_args_delta = str(getattr(fn, "arguments", "") or "")++ output_index = self._get_or_assign_tool_output_index(call_id)++ if call_id not in self._tool_args_by_call_id:+ self._tool_args_by_call_id[call_id] = ""+ self._pending_tool_events.append(+ OutputItemAddedEvent(+ type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED,+ output_index=output_index,+ item=BaseLiteLLMOpenAIResponseObject(+ **{+ "type": "function_call",+ "id": call_id,+ "call_id": call_id,+ "name": fn_name,+ "arguments": "",+ "status": "in_progress",+ }+ ),+ )+ )++ if fn_args_delta:+ self._tool_args_by_call_id[call_id] += fn_args_delta+ self._pending_tool_events.append(+ FunctionCallArgumentsDeltaEvent(+ type=ResponsesAPIStreamEvents.FUNCTION_CALL_ARGUMENTS_DELTA,+ item_id=call_id,+ output_index=output_index,+ delta=fn_args_delta,+ )+ )++ def _queue_final_tool_call_done_events(self, litellm_complete_object: ModelResponse) -> None:+ """+ Ensure tool calls that were not streamed as deltas still get emitted before response.completed.+ """+ if self._final_tool_events_queued:+ return+ self._final_tool_events_queued = True++ try:+ message = litellm_complete_object.choices[0].message # type: ignore+ tool_calls = getattr(message, "tool_calls", None)+ except Exception:+ tool_calls = None++ if not tool_calls or not isinstance(tool_calls, list):+ return++ for tc in tool_calls:+ call_id_raw = tc.get("id") if isinstance(tc, dict) else getattr(tc, "id", None)+ if not call_id_raw:+ continue+ call_id = str(call_id_raw)+ output_index = self._get_or_assign_tool_output_index(call_id)++ fn = tc.get("function") if isinstance(tc, dict) else getattr(tc, "function", None)+ fn_name = ""+ fn_args = ""+ if isinstance(fn, dict):+ fn_name = str(fn.get("name") or "")+ fn_args = str(fn.get("arguments") or "")+ else:+ fn_name = str(getattr(fn, "name", "") or "")+ fn_args = str(getattr(fn, "arguments", "") or "")++ # If we never sent output_item.added for this call_id, emit it now.+ if call_id not in self._tool_args_by_call_id:+ self._tool_args_by_call_id[call_id] = ""+ self._pending_tool_events.append(+ OutputItemAddedEvent(+ type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED,+ output_index=output_index,+ item=BaseLiteLLMOpenAIResponseObject(+ **{+ "type": "function_call",+ "id": call_id,+ "call_id": call_id,+ "name": fn_name,+ "arguments": "",+ "status": "in_progress",+ }+ ),+ )+ )++ final_args = fn_args or self._tool_args_by_call_id.get(call_id, "")+ self._pending_tool_events.append(+ FunctionCallArgumentsDoneEvent(+ type=ResponsesAPIStreamEvents.FUNCTION_CALL_ARGUMENTS_DONE,+ item_id=call_id,+ output_index=output_index,+ arguments=final_args,+ )+ )++ self._pending_tool_events.append(+ OutputItemDoneEvent(+ type=ResponsesAPIStreamEvents.OUTPUT_ITEM_DONE,+ output_index=output_index,+ sequence_number=1,+ item=BaseLiteLLMOpenAIResponseObject(+ **{+ "type": "function_call",+ "id": call_id,+ "call_id": call_id,+ "name": fn_name,+ "arguments": final_args,+ "status": "completed",+ }+ ),+ )+ )def _default_response_created_event_data(self) -> dict:response_created_event_data = {