BerriAI/litellm · error · Exception

Error: {response.status_code} {response.text}

Error message

Error: {response.status_code} {response.text}

What it means

The synchronous Vertex AI image-generation handler POSTs to the model's predict endpoint and, when the HTTP status is not 200, raises a bare Exception embedding the status code and full response body ('Error: {status} {body}'). It is not a typed litellm exception class, so callers must parse the message (or the underlying status) rather than catch a specific exception type. The body usually contains Vertex AI's error JSON explaining the real cause.

Source

Thrown at litellm/llms/vertex_ai/image_generation/image_generation_handler.py:163

        logging_obj.pre_call(
            input=prompt,
            api_key="",
            additional_args={
                "complete_input_dict": optional_params,
                "api_base": api_base,
                "headers": headers,
            },
        )

        response: Final = sync_handler.post(
            url=api_base,
            headers=headers,
            data=json.dumps(request_data),
        )

        if response.status_code != 200:
            raise Exception(f"Error: {response.status_code} {response.text}")

        json_response: Final = response.json()
        return self.process_image_generation_response(json_response, model_response, model)

    async def aimage_generation(
        self,
        prompt: str,
        api_base: str | None,
        vertex_project: str | None,
        vertex_location: str | None,
        vertex_credentials: VERTEX_CREDENTIALS_TYPES | None,
        model_response: ImageResponse,
        logging_obj: Any,
        model: str = "imagegeneration",  # vertex ai uses imagegeneration as the default model
        client: AsyncHTTPHandler | None = None,
        optional_params: dict | None = None,
        timeout: int | None = None,
        extra_headers: dict | None = None,

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Read the status and body from the message text — it contains Vertex AI's exact error JSON
  2. 404: enable the model/API in the target region (e.g. us-central1) in the Model Garden / Vertex AI console
  3. 403: grant the service account roles/aiplatform.user (or runai.user) on the project
  4. 400: verify optional_params (sampleCount, aspectRatio) against the model's docs
  5. 429: check quotas in the console and back off or request an increase

Example fix

# before
resp = litellm.image_generation(model='vertex_ai/imagegeneration', prompt='a cat')

# after — surface the embedded status/body
try:
    resp = litellm.image_generation(
        model='vertex_ai/imagegeneration',
        prompt='a cat',
        vertex_ai_project='my-project',
        vertex_ai_location='us-central1',
    )
except Exception as e:
    print(str(e))  # 'Error: 404 {"error": {"message": "Model not found..."}}'
    raise
Defensive patterns

Strategy: try-catch

Try / catch

try:
    resp = litellm.image_generation(model='vertex_ai/imagegeneration', prompt=p,
                                    vertex_ai_project=PROJECT, vertex_ai_location=REGION)
except Exception as e:
    msg = str(e)
    if 'Error: 429' in msg or 'Error: 503' in msg:
        backoff_and_retry()  # transient
    elif 'Error: 403' in msg:
        raise RuntimeError('service account lacks Vertex AI predict permission') from e
    else:
        raise  # body text contains Vertex AI's error JSON

Prevention

When it happens

Trigger: litellm.image_generation(model='vertex_ai/imagegeneration', prompt='a cat') returning 404 (model not available/enabled in the region), 403 (service account lacks aiplatform.endpoints.predict / Vertex AI User role), 400 (invalid parameters like a bad sampleCount or aspect ratio), or 429 (quota exhausted).

Common situations: Image Generation API not enabled in the GCP project; using a region where the model is not served; IAM roles granted on the wrong project; exceeding the image quota on a new account; typos in optional_params keys after camelCase transformation.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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