{"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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/cohere/common_utils.py#L159-L195","documentation":"The second stage of the sync Cohere v1 streaming iterator: the raw chunk was received fine, but convert_str_chunk_to_generic_chunk raised ValueError while transforming it (bad JSON inside the data: payload, unexpected event type). It is re-raised as RuntimeError including both the original error and the full received chunk, making diagnosis direct.","triggerScenarios":"Sync streaming cohere v1 chat where a data: line contains invalid JSON or a v1-unrecognized event shape — in-stream error events, keep-alives with stray payloads, or schema drift between the Cohere API and the litellm version installed.","commonSituations":"Cohere changes stream event shapes and the installed litellm predates the change; mid-stream error events; binary/UTF-8 decode issues in bytes chunks.","solutions":["Read the 'Received chunk:' portion to see the exact malformed payload.","If the payload is a Cohere error event, address its stated cause (context length, overloaded) and retry.","Upgrade litellm to pick up current Cohere stream-event parsing.","As a mitigation, catch RuntimeError around stream iteration and retry or degrade to non-streaming."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    for chunk in stream:\n        consume(chunk)\nexcept RuntimeError as e:\n    if \"Error parsing chunk\" in str(e):\n        capture_diagnostics(str(e))  # contains 'Received chunk: ...'\n        if looks_like_schema_drift(str(e)):\n            suggest_upgrade()\n        raise","preventionTips":["Pin and regularly upgrade litellm to track Cohere v1 stream schema changes.","Log the 'Received chunk' payload on failure — it is the evidence needed for bug reports.","Wrap stream consumption in a helper that catches RuntimeError uniformly across providers."],"tags":["cohere","streaming","sync","json-parsing"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}