Skip to content

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]

Merge branch 'litellm_internal_staging' into litellm_lit4767_empty_choices_streaming_guard

shivam73488599f064
diff --cc litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py
index 367f2585a9b,2d7589a8715..7e2eb14ce6d
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py
@@@ -875,6 -988,22 +988,24 @@@ class AnthropicStreamWrapper(AdapterCom
return False
return bool(delta.get(_delta_payload_field(delta_type)))
+ @staticmethod
+ def _is_blank_delta(chunk: "ModelResponseStream") -> bool:
++ if not chunk.choices:
++ return True
+ choice: Final = chunk.choices[0]
+ if choice.finish_reason is not None:
+ return False
+ delta: Final = choice.delta
+ if getattr(delta, "tool_calls", None):
+ return False
+ if getattr(delta, "content", None):
+ return False
+ if getattr(delta, "reasoning_content", None):
+ return False
+ if getattr(delta, "thinking_blocks", None):
+ return False
+ return True
+
def _should_start_new_content_block(self, chunk: "ModelResponseStream") -> bool:
"""
Determine if we should start a new content block based on the processed chunk.
diff --cc litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index 91efc39670e,22f9bfd30ea..a0867d9d0de
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@@ -1473,11 -1459,11 +1459,11 @@@ class LiteLLMAnthropicMessagesAdapter
self,
response: ModelResponse,
current_content_block_index: int,
- applied_edits: Optional[List[AppliedEdit]] = None,
- ) -> Union[ContentBlockDelta, MessageBlockDelta]:
+ applied_edits: list[AppliedEdit] | None = None,
+ ) -> ContentBlockDelta | MessageBlockDelta:
## base case - final chunk w/ finish reason
- if response.choices[0].finish_reason is not None:
+ if response.choices and response.choices[0].finish_reason is not None:
- delta = MessageDelta(
+ delta: Final = MessageDelta(
stop_reason=self._translate_openai_finish_reason_to_anthropic(response.choices[0].finish_reason),
)
if getattr(response, "usage", None) is not None:
diff --cc litellm/responses/litellm_completion_transformation/streaming_iterator.py
index 439c4715506,ddd05075763..504991d273c
--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py
+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py
@@@ -125,9 -125,7 +125,9 @@@ class LiteLLMCompletionStreamingIterato
return None
def _is_reasoning_end(self, chunk):
+ if not chunk.choices:
+ return False
- delta = chunk.choices[0].delta
+ delta: Final = chunk.choices[0].delta
# if this indicates reasoning content, don't consider reasoning ended
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
@@@ -724,9 -721,7 +723,9 @@@
# Change: Never return a value, just enqueue output item events
if self.sent_output_item_added_event:
return
+ if not chunk.choices:
+ return
- delta = chunk.choices[0].delta
+ delta: Final = chunk.choices[0].delta
self._sequence_number += 1
self.sent_output_item_added_event = True
@@@ -1037,10 -1032,8 +1036,10 @@@
It's unclear how users expect litellm to translate multiple-choices-per-chunk to the responses API output.
"""
+ if not choices:
+ return ""
- choice = choices[0]
- chat_completion_delta: ChatCompletionDelta = choice.delta
+ choice: Final = choices[0]
+ chat_completion_delta: Final[ChatCompletionDelta] = choice.delta
return chat_completion_delta.content or ""
def _emit_response_completed_event(self, litellm_model_response: ModelResponse) -> ResponseCompletedEvent | None: