abi/screenshot-to-code · error · ValueError

Invalid prediction creation response.

Error message

Invalid prediction creation response.

What it means

Raised in _run_prediction when the POST that creates a prediction returns 2xx but its JSON body is not an object — the first shape check before _extract_prediction_id even runs. It means the HTTP layer was fine but the payload isn't a prediction dict at all (a JSON array, scalar, or "null").

Source

Thrown at backend/image_generation/replicate.py:87

            raise ValueError(f"Inference errored out: {error_message}")
        if status == "failed":
            raise ValueError("Inference failed")

    raise TimeoutError("Inference timed out")


async def _run_prediction(
    endpoint_url: str, payload: dict[str, Any], api_token: str
) -> Any:
    headers = _build_headers(api_token)

    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(endpoint_url, headers=headers, json=payload)
            response.raise_for_status()
            response_json = response.json()
            if not isinstance(response_json, dict):
                raise ValueError("Invalid prediction creation response.")

            prediction_id = _extract_prediction_id(response_json)
            final_response = await _poll_prediction(client, prediction_id, headers)
            return final_response.get("output")
        except httpx.HTTPStatusError as exc:
            raise ValueError(f"HTTP error occurred: {exc}") from exc
        except httpx.RequestError as exc:
            raise ValueError(f"An error occurred while requesting: {exc}") from exc
        except asyncio.TimeoutError as exc:
            raise TimeoutError("Request timed out") from exc
        except (TimeoutError, ValueError):
            raise
        except Exception as exc:
            raise ValueError(f"An unexpected error occurred: {exc}") from exc


def _extract_output_url(result: Any, context: str) -> str:
    if isinstance(result, str):

View on GitHub (pinned to d026163f58)

Solutions

  1. Log response.text at the failure point to see the actual body.
  2. Confirm REPLICATE_API_BASE_URL is https://api.replicate.com and reachable directly.
  3. Reproduce the POST with curl using the same token and model_path.
  4. Update the check if the API legitimately wraps predictions in an envelope now.
Defensive patterns

Strategy: type-guard

Type guard

from typing import Any, Mapping

def is_prediction_envelope(response_json: Any) -> bool:
    return isinstance(response_json, Mapping)

Try / catch

try:
    output = await call_replicate_model(model, input, token)
except ValueError as e:
    if "Invalid prediction creation response" in str(e):
        log.error("Non-dict creation body from Replicate for %s", model)
    raise

Prevention

When it happens

Trigger: POST {base}/models/{model_path}/predictions responding 200 with a JSON list or scalar body; a gateway returning a bare JSON literal; severe API contract drift on Replicate's side.

Common situations: Rare in practice — usually proxy interference or hitting a non-Replicate endpoint configured via REPLICATE_API_BASE_URL; response shape changes during Replicate migrations.

Related errors


AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14). Data as JSON: /api/errors/e1c4b07437930855. Report an issue: GitHub.