{"record":{"id":"9c5cbcf31abf2a02","repo":"BerriAI/litellm","slug":"error-parsing-chunk-e-received-chunk-chunk-9c5cbc","errorCode":null,"errorMessage":"Error parsing chunk: {e},\nReceived chunk: {chunk}","messagePattern":"Error parsing chunk: (.+?),\nReceived chunk: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cohere/common_utils.py","lineNumber":177,"sourceCode":"\n    # Sync iterator\n    def __iter__(self):\n        return self\n\n    def __next__(self):\n        try:\n            chunk: Final = self.response_iterator.__next__()\n        except StopIteration:\n            raise StopIteration\n        except ValueError as e:\n            raise RuntimeError(f\"Error receiving chunk from stream: {e}\")\n\n        try:\n            return self.convert_str_chunk_to_generic_chunk(chunk=chunk)\n        except StopIteration:\n            raise StopIteration\n        except ValueError as e:\n            raise RuntimeError(f\"Error parsing chunk: {e},\\nReceived chunk: {chunk}\")\n\n    def convert_str_chunk_to_generic_chunk(self, chunk: str) -> GenericStreamingChunk:\n        \"\"\"\n        Convert a string chunk to a GenericStreamingChunk\n\n        Note: This is used for Cohere pass through streaming logging\n        \"\"\"\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: Final = str_line.find(\"data:\")\n            if index != -1:\n                str_line = str_line[index:]\n\n        data_json: Final = json.loads(str_line)\n        return self.chunk_parser(chunk=data_json)\n\n    # Async iterator","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/cohere/common_utils.py#L159-L195","documentation":"RuntimeError raised by the Cohere sync streaming iterator when convert_str_chunk_to_generic_chunk raises ValueError — i.e. the raw line was received fine but could not be turned into a GenericStreamingChunk (JSON decode of the payload, or unexpected event shape). Both the original error and the full received line are included in the message, making the offending SSE event directly visible.","triggerScenarios":"A Cohere stream line that is not valid JSON or lacks the fields litellm expects — gateway-injected text lines, mid-stream plain-error events, or a Cohere streaming-format change not handled by the installed litellm version.","commonSituations":"Proxy-altered SSE payloads; litellm/Cohere version skew after event schema updates (e.g. new event types on v2); truncated final events after connection issues; endpoints that close streams with a plain-text error.","solutions":["Read the 'Received chunk:' portion of the message to see the exact malformed event and its source","Upgrade litellm to pick up current Cohere v2 streaming event handling","Remove/fix intermediary proxies that modify SSE, and disable response buffering","Reproduce with stream=False to check whether the account/endpoint returns an error at all"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"stream = litellm.completion(model=\"cohere/command-r\", messages=messages, stream=True)\ntry:\n    for chunk in stream:\n        handle(chunk)\nexcept RuntimeError as e:\n    if \"Error parsing chunk\" in str(e):\n        # message includes 'Received chunk:' — log it and degrade to non-streaming\n        log.warning(\"Cohere stream malformed: %s\", e)\n        resp = litellm.completion(model=\"cohere/command-r\", messages=messages)\n        handle(resp)\n    else:\n        raise","preventionTips":["Log the embedded Received chunk payload when this fires — it identifies the intermediary at fault","Upgrade litellm alongside Cohere API changes","Ensure no middleware rewrites SSE event payloads"],"tags":["cohere","streaming","json-parse","sse","chunk-format"],"backgroundTag":"stream-json-decode-error","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}