{"record":{"id":"39d6ffa49d431db8","repo":"BerriAI/litellm","slug":"file-content-stream-does-not-support-sync-iteratio","errorCode":null,"errorMessage":"File content stream does not support sync iteration","messagePattern":"File content stream does not support sync iteration","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"litellm/files/streaming.py","lineNumber":49,"sourceCode":"        self.stream_iterator = stream_iterator\n        self.file_id = file_id\n        self.model = model\n        self.custom_llm_provider = custom_llm_provider\n        self.logging_obj = logging_obj\n        self.standard_logging_object: StandardLoggingPayload | None = None\n        self._hidden_params: dict[str, Any] = {}\n        self._logging_completed = False\n        self._close_completed = False\n        self._start_time = (\n            logging_obj.start_time\n            if logging_obj is not None and getattr(logging_obj, \"start_time\", None)\n            else datetime.datetime.now()\n        )\n        self._sync_hidden_params()\n\n    def __iter__(self) -> \"FileContentStreamingResponse\":\n        if not hasattr(self.stream_iterator, \"__next__\"):\n            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\")","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/files/streaming.py#L31-L67","documentation":"FileContentStreamingResponse wraps the raw provider iterator returned by file-content retrieval. __iter__ checks that the wrapped stream_iterator exposes __next__ (a sync iterator); if the provider returned an async-only iterator (it only has __anext__), sync iteration with `for chunk in response` raises TypeError.","triggerScenarios":"Iterating the returned streaming response synchronously when the underlying call was async — e.g. holding a response produced via the async file-content path and then calling iter()/next() or a `for` loop over it inside sync code.","commonSituations":"Mixing sync and async lifecycles (async with ... : resp = await acreate_file_content... then sync iteration); passing an httpx.AsyncClient-produced byte stream into the sync wrapper; test helpers that iterate without regard to flavor.","solutions":["Iterate the same flavor you created with: use async iteration (`async for chunk in response`) for async responses","If you need sync bytes, make the original file-content call through the sync path so the wrapper gets a sync iterator","Read the whole body once via the provided aclose()/read-style helpers on the async response instead of forcing sync iteration"],"exampleFix":"# before\nresp = await client.files.acontent(fid)  # async-backed stream\nfor chunk in resp:  # TypeError\n    ...\n\n# after\nasync for chunk in resp:\n    ...","handlingStrategy":"type-guard","validationCode":"if not hasattr(resp.stream_iterator, \"__next__\"):\n    raise TypeError(\"response is async-only; use `async for`\")","typeGuard":"def is_sync_stream(resp) -> bool:\n    return hasattr(resp.stream_iterator, \"__next__\")","tryCatchPattern":"try:\n    for chunk in resp: process(chunk)\nexcept TypeError as e:\n    if \"sync iteration\" in str(e):\n        asyncio.run(consume_async(resp))","preventionTips":["Keep retrieval and consumption in the same sync/async flavor","Write helpers that check the iterator protocol before looping"],"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"}