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
refactor(anthropic): type the responses refusal stream iterator state
mateo-berrib21315fe754d
diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.pyindex 5bd662e0f94..bcae732ad42 100644--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/streaming_iterator.py@@ -4,7 +4,7 @@ import asyncioimport jsonimport tracebackfrom collections import deque-from collections.abc import AsyncIterator, Mapping+from collections.abc import AsyncIterator, Iterator, Mappingfrom typing import TYPE_CHECKING, Any, Finalfrom litellm import verbose_logger@@ -55,7 +55,7 @@ class AnthropicResponsesStreamWrapper:self._sent_message_stop = Falseself._chunk_queue: deque[dict[str, object]] = deque()self._refusal_text_parts: list[str] = [] # mutable-ok: accumulates streamed refusal delta text across chunks- self._sync_responses_iterator: Any = None+ self._sync_responses_iterator: Iterator[object] | None = Nonedef _make_message_start(self) -> dict[str, object]:return {@@ -312,11 +312,9 @@ class AnthropicResponsesStreamWrapper:else:if self._sync_responses_iterator is None:self._sync_responses_iterator = iter(self.responses_stream)+ sync_iterator: Final = self._sync_responses_iteratormissing: Final = object()- while True:- event = await asyncio.to_thread(next, self._sync_responses_iterator, missing)- if event is missing:- break+ while (event := await asyncio.to_thread(next, sync_iterator, missing)) is not missing:self._process_event(event)if self._chunk_queue:return self._chunk_queue.popleft()