abi/screenshot-to-code · error · ValueError

An unexpected error occurred: {exc}

Error message

An unexpected error occurred: {exc}

What it means

Catch-all in _run_prediction: any exception not matching the earlier handlers (HTTPStatusError, RequestError, asyncio.TimeoutError, TimeoutError, ValueError) is re-raised as ValueError with its text, preserving the cause chain. Typical residents: JSONDecodeError when the body isn't valid JSON at all, KeyError/TypeError from unexpected payload shapes, or arbitrary bugs in the try block.

Source

Thrown at backend/image_generation/replicate.py:101

            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):
        return result

    if isinstance(result, dict):
        url = cast(Any, result.get("url"))
        if isinstance(url, str) and url:
            return url

    if isinstance(result, list) and len(result) > 0:
        first = cast(Any, result[0])
        if isinstance(first, str) and first:
            return first
        if isinstance(first, Mapping):
            url = cast(Any, first.get("url"))
            if isinstance(url, str) and url:

View on GitHub (pinned to d026163f58)

Solutions

  1. Inspect `exc.__cause__` — the original exception type tells the real story (JSONDecodeError vs TypeError vs ...).
  2. If JSONDecodeError, log response.text to find what actually came back.
  3. Fix the underlying bug rather than catching the generic ValueError upstream.
  4. During debugging, temporarily re-raise the raw exception to get an unmasked traceback.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    output = await call_replicate_model(model, input, token)
except ValueError as e:
    if str(e).startswith("An unexpected error occurred") and isinstance(
        e.__cause__, json.JSONDecodeError
    ):
        return error("Replicate returned a non-JSON body — likely a proxy page")
    raise

Prevention

When it happens

Trigger: response.json() hitting invalid JSON (HTML error pages with 200 status); a payload mutation bug inside the try block; httpx exceptions outside the two caught classes (e.g. httpx.StreamError on some setups); any programming error in the surrounding code.

Common situations: Proxies returning HTML with 200; partial responses cut off mid-body failing JSON parse; refactors of _run_prediction introducing a new bug that then masquerades as a generic ValueError.

Related errors


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