{"record":{"id":"580eeb5d387a0054","repo":"BerriAI/litellm","slug":"cannot-use-sync-iteration-on-async-iterator","errorCode":null,"errorMessage":"Cannot use sync iteration on async iterator","messagePattern":"Cannot use sync iteration on async iterator","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"litellm/responses/mcp/mcp_streaming_iterator.py","lineNumber":844,"sourceCode":"        return self\n\n    def __next__(self) -> ResponsesAPIStreamingResponse:\n        # First, emit any queued MCP events\n        if self.mcp_events:\n            return self.mcp_events.pop(0)\n\n        # Then delegate to the base iterator\n        if not self.is_async:\n            try:\n                if self.base_iterator and hasattr(self.base_iterator, \"__next__\"):\n                    return next(cast(Any, self.base_iterator))\n                else:\n                    raise StopIteration\n            except StopIteration:\n                self.finished = True\n                raise\n        else:\n            raise RuntimeError(\"Cannot use sync iteration on async iterator\")\n","sourceCodeStart":826,"sourceCodeEnd":845,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/responses/mcp/mcp_streaming_iterator.py#L826-L845","documentation":"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.","triggerScenarios":"Thrown at litellm/responses/mcp/mcp_streaming_iterator.py:844 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Iterate with 'async for' instead of a synchronous for loop over the async iterator."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}