BerriAI/litellm · error · RuntimeError
Cannot use sync iteration on async iterator
Error message
Cannot use sync iteration on async iterator
What it means
Guard in MCPStreamingIterator.__next__: the underlying base_iterator is async while the consumer is iterating synchronously, a usage/programming error — the stream must be consumed with anext/async for.
Source
Thrown at litellm/responses/mcp/mcp_streaming_iterator.py:844
return self
def __next__(self) -> ResponsesAPIStreamingResponse:
# First, emit any queued MCP events
if self.mcp_events:
return self.mcp_events.pop(0)
# Then delegate to the base iterator
if not self.is_async:
try:
if self.base_iterator and hasattr(self.base_iterator, "__next__"):
return next(cast(Any, self.base_iterator))
else:
raise StopIteration
except StopIteration:
self.finished = True
raise
else:
raise RuntimeError("Cannot use sync iteration on async iterator")
View on GitHub (pinned to 77b7c6c40c)
Solutions
- Iterate with 'async for' instead of a synchronous for loop over the async iterator.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at litellm/responses/mcp/mcp_streaming_iterator.py:844 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/580eeb5d387a0054.
Report an issue: GitHub.