{"record":{"id":"a8c9b8c5e3fe6560","repo":"BerriAI/litellm","slug":"error-invalid-response-from-watsonx-ai-api-json","errorCode":null,"errorMessage":"Error: Invalid response from Watsonx.ai API: {json_resp}","messagePattern":"Error: Invalid response from Watsonx\\.ai API: (.+?)","errorType":"exception","errorClass":"WatsonXAIError","httpStatus":500,"severity":"error","filePath":"litellm/llms/watsonx/completion/transformation.py","lineNumber":295,"sourceCode":"        request_data: dict,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        encoding: str,\n        api_key: str | None = None,\n        json_mode: bool | None = None,\n    ) -> ModelResponse:\n        ## LOGGING\n        logging_obj.post_call(\n            input=messages,\n            api_key=\"\",\n            original_response=raw_response.text,\n        )\n\n        json_resp: Final = raw_response.json()\n\n        if \"results\" not in json_resp:\n            raise WatsonXAIError(\n                status_code=500,\n                message=f\"Error: Invalid response from Watsonx.ai API: {json_resp}\",\n            )\n        if model_response is None:\n            model_response = ModelResponse(model=json_resp.get(\"model_id\", None))\n        generated_text: Final = json_resp[\"results\"][0][\"generated_text\"]\n        prompt_tokens: Final = json_resp[\"results\"][0][\"input_token_count\"]\n        completion_tokens: Final = json_resp[\"results\"][0][\"generated_token_count\"]\n        model_response.choices[0].message.content = generated_text\n        model_response.choices[0].finish_reason = map_finish_reason(json_resp[\"results\"][0][\"stop_reason\"])\n        if json_resp.get(\"created_at\"):\n            try:\n                created_datetime = datetime.fromisoformat(json_resp[\"created_at\"])\n            except ValueError:\n                # datetime.fromisoformat cannot handle 'Z' in Python 3.10\n                created_datetime = datetime.fromisoformat(f\"{json_resp['created_at'].rstrip('Z')}+00:00\")\n            model_response.created = int(created_datetime.timestamp())\n        else:","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/watsonx/completion/transformation.py#L277-L313","documentation":"WatsonXAIError (500) raised when the completion response JSON lacks the 'results' key: watsonx.ai returned an unexpected payload (often an error body), and the raw JSON is echoed so the mismatch is visible.","triggerScenarios":"Thrown at litellm/llms/watsonx/completion/transformation.py:295 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the json_resp in the error for the API's error details (bad request, auth, quota).","Verify the request parameters (model, project_id/space_id, prompt format) match the Watsonx.ai API contract."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}