{"record":{"id":"d487fad2666b3547","repo":"BerriAI/litellm","slug":"unable-to-parse-response-original-response-chun","errorCode":null,"errorMessage":"Unable to parse response. Original response: {chunk}","messagePattern":"Unable to parse response\\. Original response: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/streaming_handler.py","lineNumber":513,"sourceCode":"                \"finish_reason\": finish_reason,\n            }\n        except Exception as e:\n            raise e\n\n    def handle_ai21_chunk(self, chunk):  # fake streaming\n        chunk = chunk.decode(\"utf-8\")\n        data_json: Final[_Ai21StreamData] = json.loads(chunk)\n        try:\n            text: Final = data_json[\"completions\"][0][\"data\"][\"text\"]\n            is_finished: Final = True\n            finish_reason: Final = \"stop\"\n            return {\n                \"text\": text,\n                \"is_finished\": is_finished,\n                \"finish_reason\": finish_reason,\n            }\n        except Exception:\n            raise ValueError(f\"Unable to parse response. Original response: {chunk}\")\n\n    def handle_maritalk_chunk(self, chunk):  # fake streaming\n        chunk = chunk.decode(\"utf-8\")\n        data_json: Final[_MaritalkStreamData] = json.loads(chunk)\n        try:\n            text: Final = data_json[\"answer\"]\n            is_finished: Final = True\n            finish_reason: Final = \"stop\"\n            return {\n                \"text\": text,\n                \"is_finished\": is_finished,\n                \"finish_reason\": finish_reason,\n            }\n        except Exception:\n            raise ValueError(f\"Unable to parse response. Original response: {chunk}\")\n\n    def handle_nlp_cloud_chunk(self, chunk):\n        text = \"\"","sourceCodeStart":495,"sourceCodeEnd":531,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/streaming_handler.py#L495-L531","documentation":"The AI21 handler is fake-streaming: it decodes one JSON body and indexes data_json['completions'][0]['data']['text']. Any deviation — missing 'completions', empty list, nested key rename — throws and is converted to this ValueError including the raw body.","triggerScenarios":"Calling AI21 (j2-era) models with stream=True when the API returns an error body or a schema without completions[0].data.text (e.g. auth error JSON, model deprecation response).","commonSituations":"Using legacy AI21 Jurassic-2 endpoints after schema changes or deprecation; invalid API key producing a JSON error body that still parses as JSON; region mismatch.","solutions":["Read the raw chunk embedded in the message — it shows the actual AI21 response.","Verify the AI21 API key and model name (e.g. j2-ultra) still work with a non-streaming call first.","Migrate to AI21's current models (jamba) which use a different handler.","Pin/upgrade litellm to a version matching the AI21 API you target."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    text = \"\".join(p for p in litellm.completion(model=\"ai21/j2-ultra\", stream=True, ...))\nexcept ValueError as e:\n    if \"Unable to parse response\" in str(e):\n        fallback = litellm.completion(model=\"ai21/j2-ultra\", stream=False, ...)","preventionTips":["Verify legacy AI21 models with non-streaming calls first.","Prefer current AI21 jamba models and a matching litellm version."],"tags":["ai21","streaming","schema","legacy"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}