{"record":{"id":"4f7abc549fa7e71b","repo":"abi/screenshot-to-code","slug":"prediction-id-not-found-in-initial-response","errorCode":null,"errorMessage":"Prediction ID not found in initial response.","messagePattern":"Prediction ID not found in initial response\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/image_generation/replicate.py","lineNumber":46,"sourceCode":"DEFAULT_IMAGE_MODEL: ReplicateImageModel = \"z_image_turbo\"\nREMOVE_BACKGROUND_VERSION = (\n    \"a029dff38972b5fda4ec5d75d7d1cd25aeff621d2cf4946a41055d7db66b80bc\"\n)\nPOLL_INTERVAL_SECONDS = 0.1\nMAX_POLLS = 100\n\n\ndef _build_headers(api_token: str) -> dict[str, str]:\n    return {\n        \"Authorization\": f\"Bearer {api_token}\",\n        \"Content-Type\": \"application/json\",\n    }\n\n\ndef _extract_prediction_id(response_json: Mapping[str, Any]) -> str:\n    prediction_id = response_json.get(\"id\")\n    if not isinstance(prediction_id, str) or not prediction_id:\n        raise ValueError(\"Prediction ID not found in initial response.\")\n    return prediction_id\n\n\nasync def _poll_prediction(\n    client: httpx.AsyncClient, prediction_id: str, headers: dict[str, str]\n) -> dict[str, Any]:\n    status_check_url = f\"{REPLICATE_API_BASE_URL}/predictions/{prediction_id}\"\n\n    for _ in range(MAX_POLLS):\n        await asyncio.sleep(POLL_INTERVAL_SECONDS)\n        status_response = await client.get(status_check_url, headers=headers)\n        status_response.raise_for_status()\n        status_response_raw: Any = status_response.json()\n        if not isinstance(status_response_raw, dict):\n            raise ValueError(\"Invalid prediction status response.\")\n        status_response_json = cast(dict[str, Any], status_response_raw)\n\n        status = status_response_json.get(\"status\")","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/image_generation/replicate.py#L28-L64","documentation":"In the Replicate image-generation client, _extract_prediction_id raises ValueError when the JSON body of the prediction-creation POST response lacks a usable `id` — either the field is missing, not a string, or an empty string. This means Replicate accepted the HTTP request (2xx) but returned a payload that doesn't look like a prediction object, e.g. an error envelope or an API shape change.","triggerScenarios":"POST to {REPLICATE_API_BASE_URL}/models/{model_path}/predictions returning 200/201 with a body like {\"detail\": \"...\"} or {\"id\": null} or {\"id\": 12345} (non-string). Also when a proxy or gateway intercepts the response and returns its own JSON.","commonSituations":"Replicate API contract change or model endpoint responding with a different envelope; using a model_path that silently redirects; a corporate proxy rewriting responses; version drift between this client and Replicate's current API.","solutions":["Log the full response body when this fires to see what Replicate actually returned.","Verify model_path is a valid, current Replicate model (owner/name or owner/name:version).","Check REPLICATE_API_BASE_URL is the official API base (https://api.replicate.com) and not being intercepted.","If the API shape changed, update _extract_prediction_id to read the new id field."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from typing import Mapping, Any\n\ndef has_prediction_id(response_json: Any) -> bool:\n    return isinstance(response_json, Mapping) and isinstance(\n        response_json.get(\"id\"), str\n    ) and bool(response_json[\"id\"])","tryCatchPattern":"try:\n    result = await call_replicate_model(model, input, token)\nexcept ValueError as e:\n    if \"Prediction ID not found\" in str(e):\n        log.error(\"Replicate returned unexpected envelope: %s\", model)\n    raise","preventionTips":["Log the raw creation-response body once when this fires to identify envelope drift","Pin model versions and verify model_path exists before calling","Keep REPLICATE_API_BASE_URL pointed at the official API"],"tags":["replicate","api","image-generation","response-validation"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}