{"record":{"id":"8ae43dee12ced9b7","repo":"nexu-io/open-design","slug":"image-api-response-did-not-contain-data-0","errorCode":null,"errorMessage":"image API response did not contain data[0]","messagePattern":"image API response did not contain data\\[0\\]","errorType":"exception","errorClass":"SystemExit","httpStatus":null,"severity":"error","filePath":"skills/hatch-pet/scripts/generate_pet_images.py","lineNumber":155,"sourceCode":"    }).encode()\n    req = urllib.request.Request(\n        \"https://api.openai.com/v1/images/generations\",\n        data=payload,\n        headers={\"Authorization\": f\"Bearer {api_key}\", \"Content-Type\": \"application/json\"},\n        method=\"POST\",\n    )\n    with urllib.request.urlopen(req, timeout=300) as resp:\n        output_json.write_bytes(resp.read())\n    response = json.loads(output_json.read_text(encoding=\"utf-8\"))\n    if response.get(\"error\"):\n        raise SystemExit(json.dumps(response[\"error\"], indent=2))\n    return response\n\n\ndef decode_response(response: dict[str, object], output_image: Path) -> None:\n    data = response.get(\"data\")\n    if not isinstance(data, list) or not data:\n        raise SystemExit(\"image API response did not contain data[0]\")\n    first = data[0]\n    if not isinstance(first, dict) or not isinstance(first.get(\"b64_json\"), str):\n        raise SystemExit(\"image API response did not contain data[0].b64_json\")\n    output_image.parent.mkdir(parents=True, exist_ok=True)\n    output_image.write_bytes(base64.b64decode(first[\"b64_json\"]))\n\n\ndef file_sha256(path: Path) -> str:\n    digest = hashlib.sha256()\n    with path.open(\"rb\") as file:\n        for chunk in iter(lambda: file.read(1024 * 1024), b\"\"):\n            digest.update(chunk)\n    return digest.hexdigest()\n\n\ndef complete_job(job: dict[str, object], output_path: Path) -> None:\n    job[\"status\"] = \"complete\"\n    job[\"source_path\"] = str(output_path)","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/nexu-io/open-design/blob/5be4028344c2eb4c667c5a97bda8f750c5597ef7/skills/hatch-pet/scripts/generate_pet_images.py#L137-L173","documentation":"Raised by decode_response when the parsed OpenAI image response has no usable \"data\" array (it is missing, not a list, or empty). decode_response expects response[\"data\"][0] to exist before it reads b64_json, so a response without a data array cannot be decoded into an image.","triggerScenarios":"The API returned a success-shaped body whose top-level shape differs from {data: [...]}, e.g. an error enveloped at the root, a quota/billing notice, or a model that returns a different response contract (url instead of b64_json, or a streaming-style envelope).","commonSituations":"Account out of image-generation credits returning a body without data; model name resolving to a variant that returns image URLs rather than b64_json; an OpenAI-compatible proxy that strips the data array; a partial/garbled response body written to the raw .response.json file.","solutions":["Open run_dir/raw/<job_id>.response.json and inspect the actual top-level keys to see what the API returned.","If the body shows a quota/billing error, add credits or switch to a key with quota, then rerun the job.","If data[] entries use url instead of b64_json, request b64 output explicitly or extend decode_response to fetch the url.","If the response is garbled/truncated, rerun; if it persists, check network/proxy stability."],"exampleFix":"// before\ndata = response.get(\"data\")\nif not isinstance(data, list) or not data:\n    raise SystemExit(\"image API response did not contain data[0]\")\n\n// after - include the response shape so the failure is self-diagnosing\ndata = response.get(\"data\")\nif not isinstance(data, list) or not data:\n    raise SystemExit(\n        \"image API response did not contain data[0]; top-level keys: \"\n        + \", \".join(sorted(response.keys()))\n    )","handlingStrategy":"validation","validationCode":"def has_data_array(response: dict) -> bool:\n    data = response.get(\"data\")\n    return isinstance(data, list) and len(data) > 0\n\n# before calling decode_response:\nif not has_data_array(response):\n    raise SystemExit(f\"unexpected response shape; keys={sorted(response)}\")","typeGuard":"from typing import Any\n\ndef is_image_response(value: Any) -> bool:\n    return (\n        isinstance(value, dict)\n        and isinstance(value.get(\"data\"), list)\n        and len(value[\"data\"]) > 0\n        and isinstance(value[\"data\"][0], dict)\n        and isinstance(value[\"data\"][0].get(\"b64_json\"), str)\n    )","tryCatchPattern":null,"preventionTips":["Always inspect the raw .response.json written under run_dir/raw/ when the shape is unexpected.","Confirm the model returns b64_json (not url) for the size you request before batching.","Keep an integration test that asserts the expected response schema against a recorded fixture."],"tags":["openai-api","image-generation","response-shape","system-exit"],"backgroundTag":null,"analyzedSha":"5be4028344c2eb4c667c5a97bda8f750c5597ef7","analyzedAt":"2026-08-12T12:03:58.812Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}