{"record":{"id":"434b35e50d2f12a4","repo":"BerriAI/litellm","slug":"failed-to-decode-chunk-chunk-data-error-e","errorCode":null,"errorMessage":"Failed to decode chunk: {chunk_data}. Error: {e}","messagePattern":"Failed to decode chunk: (.+?)\\. Error: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/bedrock/chat/invoke_handler.py","lineNumber":822,"sourceCode":"                        arguments=_model_response_tool_call[0].function.arguments,\n                    ),\n                    index=0,\n                )\n            processed_chunk: Final = GChunk(\n                text=text,\n                tool_use=tool_use,\n                is_finished=True,\n                finish_reason=map_finish_reason(finish_reason=chunk_data.choices[0].finish_reason or \"\"),\n                usage=ChatCompletionUsageBlock(\n                    prompt_tokens=chunk_usage.prompt_tokens,\n                    completion_tokens=chunk_usage.completion_tokens,\n                    total_tokens=chunk_usage.total_tokens,\n                ),\n                index=0,\n            )\n            return processed_chunk\n        except Exception as e:\n            raise ValueError(f\"Failed to decode chunk: {chunk_data}. Error: {e}\")\n\n    def __next__(self):\n        if self.is_done:\n            raise StopIteration\n        self.is_done = True\n        return self._chunk_parser(self.model_response)\n\n    # Async iterator\n    def __aiter__(self):\n        return self\n\n    async def __anext__(self):\n        if self.is_done:\n            raise StopAsyncIteration\n        self.is_done = True\n        return self._chunk_parser(self.model_response)\n","sourceCodeStart":804,"sourceCodeEnd":839,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/bedrock/chat/invoke_handler.py#L804-L839","documentation":"Raised by the model_response-based chunk parser at the end of the Bedrock streaming decoders: it converts an already-built ModelResponse (used for fake/non-AWS-event streams) into a streaming chunk, and wraps any failure in ValueError('Failed to decode chunk: {chunk_data}. Error: {e}'). The message embeds both the chunk payload and the underlying error, so the root cause is visible in the text.","triggerScenarios":"Fake-stream mode (fake_stream=True) or provider paths where the completed ModelResponse is re-parsed chunk-wise: failures occur when chunk_data.choices is empty or malformed, finish_reason mapping returns None, or usage fields (prompt/completion/total tokens) are missing or non-numeric.","commonSituations":"Providers that return empty choices arrays on error, model responses without usage blocks when litellm expects them, or litellm version bugs where the fake-stream re-packing loses fields.","solutions":["Inspect the embedded chunk_data and Error in the ValueError message to see which field was missing or the wrong type.","Upgrade litellm - the fake-stream chunk converter has had several fixes for missing usage/finish_reason.","Disable fake-stream for that model if you set it explicitly, letting the native event-stream path run.","If the provider response is genuinely malformed, log it via LITELLM_LOG=DEBUG and open an issue with the payload.","Route via bedrock/converse/<model> to use the Converse schema instead of the invoke fake-stream path."],"exampleFix":"# before\nresp = litellm.completion(model=\"bedrock/<provider>.<model>\", messages=msgs)  # fake-stream path errors mid-parse\n\n# after\nfrom litellm.exceptions import BedrockError\ntry:\n    resp = litellm.completion(model=\"bedrock/converse/<provider>.<model>\", messages=msgs)\nexcept ValueError as e:\n    msg = str(e)\n    if \"Failed to decode chunk\" in msg:\n        logger.error(\"malformed provider payload: %s\", msg)\n        raise\n    raise","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    chunks = list(litellm.completion(model=\"bedrock/<model>\", messages=msgs, stream=True))\nexcept ValueError as e:\n    if \"Failed to decode chunk\" in str(e):\n        log.error(\"malformed chunk payload: %s\", e)\n        raise\n    raise","preventionTips":["Avoid fake-stream mode unless required; use native streaming or the converse route.","Pin litellm versions whose fake-stream fixes you have verified in staging.","Log the embedded chunk_data when this fires - it identifies the exact missing field."],"tags":["aws","bedrock","fake-stream","chunk-parsing","valueerror"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}