{"record":{"id":"37c5c98bd1b18986","repo":"BerriAI/litellm","slug":"file-content-stream-does-not-support-async-iterati","errorCode":null,"errorMessage":"File content stream does not support async iteration","messagePattern":"File content stream does not support async iteration","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"litellm/files/streaming.py","lineNumber":67,"sourceCode":"            raise TypeError(\"File content stream does not support sync iteration\")\n        return self\n\n    def __next__(self) -> bytes:\n        if not hasattr(self.stream_iterator, \"__next__\"):\n            raise TypeError(\"File content stream does not support sync iteration\")\n\n        try:\n            return next(cast(Iterator[bytes], self.stream_iterator))\n        except StopIteration:\n            self._log_success_sync()\n            raise\n        except Exception as e:\n            self._log_failure_sync(e)\n            raise\n\n    def __aiter__(self) -> \"FileContentStreamingResponse\":\n        if not hasattr(self.stream_iterator, \"__anext__\"):\n            raise TypeError(\"File content stream does not support async iteration\")\n        return self\n\n    async def __anext__(self) -> bytes:\n        if not hasattr(self.stream_iterator, \"__anext__\"):\n            raise TypeError(\"File content stream does not support async iteration\")\n\n        try:\n            return await cast(AsyncIterator[bytes], self.stream_iterator).__anext__()\n        except StopAsyncIteration:\n            await self._log_success_async()\n            raise\n        except Exception as e:\n            await self._log_failure_async(e)\n            raise\n\n    async def aclose(self) -> None:\n        if self._close_completed:\n            return","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/files/streaming.py#L49-L85","documentation":"The async mirror of the sync guard: __aiter__ verifies stream_iterator implements __anext__. If the underlying provider stream is sync-only (only __next__), `async for chunk in response` raises TypeError because there is nothing to asynchronously await.","triggerScenarios":"Using `async for` over a FileContentStreamingResponse created from a synchronous file-content call (sync httpx stream wrapped by the sync path); awaiting iteration on a response obtained without an async client.","commonSituations":"Porting sync example code into an async FastAPI handler and changing only the loop to `async for` while keeping the sync retrieval call; mixing litellm sync file APIs inside async endpoints for 'convenience'.","solutions":["Use the async file-content retrieval so the wrapper holds an async iterator, then `async for chunk in response`","If you must bridge, wrap the sync iterator with a thread-based async adapter or read it fully to bytes before entering async code","Check hasattr(resp.stream_iterator, '__anext__') before choosing the loop flavor"],"exampleFix":"# before\nresp = litellm.file_content(fid, custom_llm_provider='openai')  # sync stream\nasync for chunk in resp:  # TypeError\n    ...\n\n# after\nresp = await litellm.afile_content(fid, custom_llm_provider='openai')\nasync for chunk in resp:\n    ...","handlingStrategy":"type-guard","validationCode":"if not hasattr(resp.stream_iterator, \"__anext__\"):\n    raise TypeError(\"response is sync-only; use `for`\")","typeGuard":"from collections.abc import AsyncIterator\n\ndef is_async_stream(resp) -> bool:\n    return hasattr(resp.stream_iterator, \"__anext__\") and not isinstance(resp.stream_iterator, AsyncIterator) is False","tryCatchPattern":"try:\n    async for chunk in resp: await process(chunk)\nexcept TypeError as e:\n    if \"async iteration\" in str(e):\n        for chunk in resp: process_sync(chunk)","preventionTips":["Match create call flavor to consumer flavor","Type responses as the specific sync/async class in your code"],"tags":["files-api","streaming","sync-async-mismatch"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}