{"record":{"id":"acac052c05ab8a62","repo":"BerriAI/litellm","slug":"error-receiving-chunk-from-stream-e","errorCode":null,"errorMessage":"Error receiving chunk from stream: {e}","messagePattern":"Error receiving chunk from stream: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"litellm/llms/anthropic/chat/handler.py","lineNumber":1114,"sourceCode":"    def __iter__(self):\n        return self\n\n    def __next__(self):\n        while True:\n            try:\n                chunk = self.response_iterator.__next__()\n            except StopIteration:\n                # If we have accumulated JSON when stream ends, try to parse it\n                if self.accumulated_json:\n                    try:\n                        data_json = json.loads(self.accumulated_json)\n                        self.accumulated_json = \"\"\n                        return self.chunk_parser(chunk=data_json)\n                    except json.JSONDecodeError:\n                        pass\n                raise StopIteration\n            except ValueError as e:\n                raise RuntimeError(f\"Error receiving chunk from stream: {e}\")\n\n            try:\n                str_line = chunk\n                if isinstance(chunk, bytes):  # Handle binary data\n                    str_line = chunk.decode(\"utf-8\")  # Convert bytes to string\n                    index = str_line.find(\"data:\")\n                    if index != -1:\n                        str_line = str_line[index:]\n\n                if str_line.startswith(\"data:\"):\n                    result = self._parse_sse_data(str_line)\n                    if result is not None:\n                        return result\n                    # If None, continue loop to get more chunks for accumulation\n                else:\n                    return GenericStreamingChunk(\n                        text=\"\",\n                        is_finished=False,","sourceCodeStart":1096,"sourceCodeEnd":1132,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/anthropic/chat/handler.py#L1096-L1132","documentation":"RuntimeError raised in the sync Anthropic stream iterator's __next__ when the underlying response_iterator itself raises a ValueError while producing the next raw chunk. It is a wrapper: '{e}' carries the original error, so the root cause is whatever made the upstream/httpx iterator fail with a ValueError (most often the chunk_parser's JSON-decode ValueError at line 977 bubbling up through the iterator chain).","triggerScenarios":"Sync streaming call (litellm.completion(..., stream=True) with anthropic/) where the provider stream yields data that fails parsing upstream of this loop: malformed SSE JSON frames, a decoder error in the httpx response iterator, or a ValueError from an embedded transformation step.","commonSituations":"Calling a non-Anthropic-compatible endpoint under the anthropic/ prefix (e.g. a fake/mock server or an OpenAI-compatible gateway) whose stream format the Anthropic parser cannot consume; corporate proxies injecting keep-alive or non-data lines; partial frames from network interruptions.","solutions":["Read the inner exception text after 'Error receiving chunk from stream:' — it names the actual failure (usually 'Failed to decode JSON from chunk: ...').","Print/log the offending chunk from the inner message and compare it with Anthropic's documented SSE format (event: ... / data: {json}).","If the base URL is an OpenAI-compatible gateway, call it without the anthropic/ prefix so LiteLLM uses the OpenAI parser.","Test the same request against api.anthropic.com directly to isolate intermediary interference.","Wrap the stream loop in try/except RuntimeError so a single bad frame can be logged instead of crashing the whole consumer."],"exampleFix":"# before\nchunks = list(response)  # RuntimeError aborts everything\n\n# after\nchunks = []\nfor c in response:\n    try:\n        chunks.append(c)\n    except RuntimeError as e:\n        log.error(\"stream error, keeping partial result: %s\", e)\n        break","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    chunks = list(stream)\nexcept RuntimeError as e:\n    if \"Error receiving chunk from stream\" in str(e):\n        log.error(\"upstream stream failure: %s\", e)\n        # decide: retry request or degrade to partial results\n    raise_or_return_partial()","preventionTips":["Do not route OpenAI-format gateways under the anthropic/ prefix.","Set realistic read timeouts so streams do not get truncated by intermediaries.","Always log the inner exception text; it contains the offending chunk."],"tags":["anthropic","streaming","sync","runtimeerror","wrapper"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}