BerriAI/litellm · error · WatsonXAIError

Error: Invalid response from Watsonx.ai API: {json_resp}

Error message

Error: Invalid response from Watsonx.ai API: {json_resp}

What it means

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.

Source

Thrown at litellm/llms/watsonx/completion/transformation.py:295

        request_data: dict,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,
        encoding: str,
        api_key: str | None = None,
        json_mode: bool | None = None,
    ) -> ModelResponse:
        ## LOGGING
        logging_obj.post_call(
            input=messages,
            api_key="",
            original_response=raw_response.text,
        )

        json_resp: Final = raw_response.json()

        if "results" not in json_resp:
            raise WatsonXAIError(
                status_code=500,
                message=f"Error: Invalid response from Watsonx.ai API: {json_resp}",
            )
        if model_response is None:
            model_response = ModelResponse(model=json_resp.get("model_id", None))
        generated_text: Final = json_resp["results"][0]["generated_text"]
        prompt_tokens: Final = json_resp["results"][0]["input_token_count"]
        completion_tokens: Final = json_resp["results"][0]["generated_token_count"]
        model_response.choices[0].message.content = generated_text
        model_response.choices[0].finish_reason = map_finish_reason(json_resp["results"][0]["stop_reason"])
        if json_resp.get("created_at"):
            try:
                created_datetime = datetime.fromisoformat(json_resp["created_at"])
            except ValueError:
                # datetime.fromisoformat cannot handle 'Z' in Python 3.10
                created_datetime = datetime.fromisoformat(f"{json_resp['created_at'].rstrip('Z')}+00:00")
            model_response.created = int(created_datetime.timestamp())
        else:

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Inspect the json_resp in the error for the API's error details (bad request, auth, quota).
  2. Verify the request parameters (model, project_id/space_id, prompt format) match the Watsonx.ai API contract.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at litellm/llms/watsonx/completion/transformation.py:295 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/a8c9b8c5e3fe6560. Report an issue: GitHub.