{"record":{"id":"c81efc1d7b1d2789","repo":"BerriAI/litellm","slug":"failed-to-decode-json-from-chunk-chunk-c81efc","errorCode":null,"errorMessage":"Failed to decode JSON from chunk: {chunk}","messagePattern":"Failed to decode JSON from chunk: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/cohere/common_utils.py","lineNumber":158,"sourceCode":"                finish_reason = chunk[\"finish_reason\"]\n\n            if \"citations\" in chunk:\n                provider_specific_fields = {\"citations\": chunk[\"citations\"]}\n\n            returned_chunk: Final = GenericStreamingChunk(\n                text=text,\n                tool_use=tool_use,\n                is_finished=is_finished,\n                finish_reason=finish_reason,\n                usage=usage,\n                index=index,\n                provider_specific_fields=provider_specific_fields,\n            )\n\n            return returned_chunk\n\n        except json.JSONDecodeError:\n            raise ValueError(f\"Failed to decode JSON from chunk: {chunk}\")\n\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:","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/cohere/common_utils.py#L140-L176","documentation":"In the Cohere v1 streaming iterator, each SSE line is json.loads-ed inside chunk_parser; a json.JSONDecodeError (non-JSON line such as an event comment, an HTML fragment, or a garbled chunk) raises ValueError('Failed to decode JSON from chunk: {chunk}') with the offending chunk included. This is the inner parse error — the iterator wrappers (errors 1433/1434) usually rewrap it as RuntimeError.","triggerScenarios":"Streaming cohere v1 chat (command models) when a stream line is not JSON: 'data:' lines with non-JSON payloads, an in-stream error event, proxy-injected content, or keep-alive/garbage lines from a middlebox.","commonSituations":"Cohere sending an error event mid-stream after a partial response; stream corruption through proxies; using a v2 model through the v1 streaming path where event shapes differ.","solutions":["Look at the chunk embedded in the message — a Cohere error event identifies the real cause (e.g. context length, overloaded).","If the chunk is HTML or noise, remove intercepting proxies or fix api_base.","Retry the request: mid-stream failures are frequently transient under load.","Move to v2 models/command-r family if you are streaming a v2-only model via the v1 path."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    for chunk in cohere_v1_stream:\n        consume(chunk)\nexcept ValueError as e:\n    if \"Failed to decode JSON from chunk\" in str(e):\n        log_offending_chunk(str(e))\n        retry_stream()  # often transient; drop partials unless you track them","preventionTips":["Keep partial-output accumulation so a mid-stream failure does not lose everything.","Stream v1 models only through trusted network paths (no content-rewriting proxies).","Upgrade litellm when Cohere announces stream event changes."],"tags":["cohere","streaming","json-parsing","sse"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}