{"record":{"id":"ef5d94bdadcb7a70","repo":"abi/screenshot-to-code","slug":"http-error-occurred-exc","errorCode":null,"errorMessage":"HTTP error occurred: {exc}","messagePattern":"HTTP error occurred: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/image_generation/replicate.py","lineNumber":93,"sourceCode":"\nasync def _run_prediction(\n    endpoint_url: str, payload: dict[str, Any], api_token: str\n) -> Any:\n    headers = _build_headers(api_token)\n\n    async with httpx.AsyncClient() as client:\n        try:\n            response = await client.post(endpoint_url, headers=headers, json=payload)\n            response.raise_for_status()\n            response_json = response.json()\n            if not isinstance(response_json, dict):\n                raise ValueError(\"Invalid prediction creation response.\")\n\n            prediction_id = _extract_prediction_id(response_json)\n            final_response = await _poll_prediction(client, prediction_id, headers)\n            return final_response.get(\"output\")\n        except httpx.HTTPStatusError as exc:\n            raise ValueError(f\"HTTP error occurred: {exc}\") from exc\n        except httpx.RequestError as exc:\n            raise ValueError(f\"An error occurred while requesting: {exc}\") from exc\n        except asyncio.TimeoutError as exc:\n            raise TimeoutError(\"Request timed out\") from exc\n        except (TimeoutError, ValueError):\n            raise\n        except Exception as exc:\n            raise ValueError(f\"An unexpected error occurred: {exc}\") from exc\n\n\ndef _extract_output_url(result: Any, context: str) -> str:\n    if isinstance(result, str):\n        return result\n\n    if isinstance(result, dict):\n        url = cast(Any, result.get(\"url\"))\n        if isinstance(url, str) and url:\n            return url","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/image_generation/replicate.py#L75-L111","documentation":"Wrapped error from _run_prediction: an httpx.HTTPStatusError (a 4xx/5xx HTTP response, surfaced by raise_for_status) is re-raised as ValueError with the exception text appended. The original status code, URL, and response are preserved via `from exc`. It fires for both the creation POST and every polling GET.","triggerScenarios":"401 invalid REPLICATE_API_KEY; 404 unknown model_path; 422 invalid input payload for the model; 429 rate limited. The exc text embeds the status code and server message, e.g. \"Client error '401 Unauthorized' for url ...\".","commonSituations":"Expired or mistyped API token; using a model name that was renamed or deleted; exceeding Replicate rate limits under concurrent generation; malformed input JSON the API rejects.","solutions":["Parse the embedded status: 401 → fix the token, 404 → fix model_path, 422 → fix input schema, 429 → back off.","Inspect `exc.__cause__` (the HTTPStatusError) for the full response body with Replicate's detail message.","Verify REPLICATE_API_KEY in backend/.env (this key only works via .env, not the UI settings).","Add rate-limit backoff if firing under parallel requests."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    output = await call_replicate_model(model, input, token)\nexcept ValueError as e:\n    cause = e.__cause__\n    if isinstance(cause, httpx.HTTPStatusError):\n        status = cause.response.status_code\n        if status == 401:\n            return error(\"Invalid Replicate API token\")\n        if status == 404:\n            return error(f\"Unknown model: {model}\")\n        if status == 429:\n            await asyncio.sleep(backoff); retry = True\n    raise","preventionTips":["Set REPLICATE_API_KEY in backend/.env and restart — the UI settings dialog can't hold it","Verify model_path spelling (owner/name[:version]) against the Replicate site","Handle 401/404/429 distinctly: only 429 merits retry with backoff"],"tags":["replicate","http","api","image-generation","auth"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}