BerriAI/litellm · error · OpenAIError

Failed to parse Clarifai response

Error message

Failed to parse Clarifai response: {e}

What it means

OpenAIError raised by the Clarifai adapter when raw_response.json() fails — the Clarifai API replied with a body that is not valid JSON. The status code and headers of the offending response are attached to the error, so a 401 HTML page vs a 200 malformed body is distinguishable. Typically the non-JSON body is an error page from auth failure or a wrong model/user/app path.

Solutions

  1. Verify CLARIFAI_API_KEY is set to a valid Clarifai PAT (starts with 'Key ' or raw PAT depending on setup)
  2. Check the model name has the full user.app.model path, e.g. clarifai/main/your-model
  3. Read the status_code on the error: 401/403 means credentials, 404 means model path, 5xx means retry later
  4. Log raw_response.text (captured in litellm logs) to see the actual non-JSON body returned
Defensive patterns

Strategy: try-catch

Validate before calling

import litellm, os

# smoke-test credentials/path before the real call
try:
    litellm.completion(
        model="clarifai/main/<model_id>",
        messages=[{"role": "user", "content": "ping"}],
        max_tokens=1,
    )
except Exception as e:
    if "Failed to parse Clarifai response" in str(e):
        raise RuntimeError("Clarifai returned a non-JSON body; check CLARIFAI_API_KEY and model path")

Try / catch

try:
    resp = litellm.completion(model=f"clarifai/{user}.{app}.{model_id}", messages=messages)
except litellm.llms.openai.common_utils.OpenAIError as e:
    if "Failed to parse Clarifai response" in str(e):
        if e.status_code in (401, 403):
            raise RuntimeError("Clarifai PAT invalid/expired") from e
        if e.status_code == 404:
            raise RuntimeError("Clarifai model path wrong; use user.app.model") from e
    raise

Prevention

When it happens

Trigger: Calling completion() with a clarifai/* model where CLARIFAI_API_KEY (PAT) is wrong (error page instead of JSON), the model id user.app.model is malformed, or an upstream gateway returns HTML/empty on 5xx.

Common situations: Missing or expired ClarifAI PAT environment variable; model string not in 'user_id.app_id.model_id' form; hitting rate limits or outages that return non-JSON error bodies; region-blocked requests.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at litellm/llms/clarifai/chat/transformation.py:107

        api_key: str | None = None,
        json_mode: bool | None = None,
    ) -> ModelResponse:
        """
        Transform the Clarifai response to a standard ModelResponse.
        Since Clarifai is OpenAI-compatible, we use OpenAI response transformation.
        """
        ## Logging
        logging_obj.post_call(
            input=messages,
            api_key=api_key,
            original_response=raw_response.text,
            additional_args={"complete_input_dict": request_data},
        )
        ## Reponse
        try:
            completion_response: Final = raw_response.json()
        except Exception as e:
            raise OpenAIError(
                status_code=raw_response.status_code,
                message=f"Failed to parse Clarifai response: {e}",
                headers=raw_response.headers,
            ) from e

        response: Final = ModelResponse(**completion_response)

        if response.model is not None:
            response.model = "clarifai/" + model

        return response

    def get_error_class(self, error_message: str, status_code: int, headers: dict | httpx.Headers) -> BaseLLMException:
        """
        Get the appropriate error class for Clarifai errors.
        Since Clarifai is OpenAI-compatible, we use OpenAI error handling.
        """
        return OpenAIError(

View on GitHub (pinned to 77b7c6c40c)