{"record":{"id":"21f044789beba90d","repo":"nexu-io/open-design","slug":"image-api-response-did-not-contain-data-0-b64-jso","errorCode":null,"errorMessage":"image API response did not contain data[0].b64_json","messagePattern":"image API response did not contain data\\[0\\]\\.b64_json","errorType":"exception","errorClass":"SystemExit","httpStatus":null,"severity":"error","filePath":"skills/hatch-pet/scripts/generate_pet_images.py","lineNumber":158,"sourceCode":"        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)\n    job[\"source_provenance\"] = \"secondary-fallback-image-api\"\n    job[\"source_sha256\"] = file_sha256(output_path)\n    job[\"output_sha256\"] = file_sha256(output_path)","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/nexu-io/open-design/blob/5be4028344c2eb4c667c5a97bda8f750c5597ef7/skills/hatch-pet/scripts/generate_pet_images.py#L140-L176","documentation":"Raised by decode_response when response[\"data\"][0] exists but is not a dict, or its b64_json field is missing/not a string. The script hard-requires a base64 PNG payload at data[0].b64_json and cannot fall back to a URL or any other field.","triggerScenarios":"The images/generations or images/edits call succeeded but returned data[0] = {\"url\": \"...\"} (url format) instead of b64_json, or returned data[0] as a non-dict element, or b64_json was emitted as null/empty by the model.","commonSituations":"Default response_format for the model is url and the script did not request b64_json; using a third-party OpenAI-compatible image API that only returns URLs; a model variant that omits b64_json on certain sizes.","solutions":["Inspect run_dir/raw/<job_id>.response.json to confirm whether data[0] carries url vs b64_json.","Ensure the request asks for base64: the generations payload already sets output_format png, but if the model needs response_format=b64_json, add it to the payload/fields.","Switch to a model that returns b64_json for the requested size.","If only URLs are available, extend decode_response to download data[0][\"url\"] into output_image."],"exampleFix":"// before\nfirst = data[0]\nif 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\n// after - fall back to url when b64_json is absent\nfirst = data[0]\nif not isinstance(first, dict):\n    raise SystemExit(\"image API response data[0] is not an object\")\nif isinstance(first.get(\"b64_json\"), str):\n    output_image.write_bytes(base64.b64decode(first[\"b64_json\"]))\nelif isinstance(first.get(\"url\"), str):\n    with urllib.request.urlopen(first[\"url\"], timeout=300) as remote:\n        output_image.write_bytes(remote.read())\nelse:\n    raise SystemExit(\"image API response did not contain data[0].b64_json or url\")","handlingStrategy":"type-guard","validationCode":"def extract_image_bytes(first_entry: object) -> bytes:\n    if not isinstance(first_entry, dict):\n        raise SystemExit(\"data[0] is not an object\")\n    b64 = first_entry.get(\"b64_json\")\n    if isinstance(b64, str):\n        return base64.b64decode(b64)\n    url = first_entry.get(\"url\")\n    if isinstance(url, str):\n        with urllib.request.urlopen(url, timeout=300) as remote:\n            return remote.read()\n    raise SystemExit(\"data[0] has neither b64_json nor url\")","typeGuard":"def has_b64(entry: object) -> bool:\n    return isinstance(entry, dict) and isinstance(entry.get(\"b64_json\"), str)","tryCatchPattern":null,"preventionTips":["Request b64 output explicitly in the API payload when the model supports response_format.","Record a known-good response fixture and diff new responses against it during schema drift.","If integrating an OpenAI-compatible proxy, confirm it returns b64_json before relying on it."],"tags":["openai-api","image-generation","response-shape","base64","system-exit"],"backgroundTag":null,"analyzedSha":"5be4028344c2eb4c667c5a97bda8f750c5597ef7","analyzedAt":"2026-08-12T12:03:58.812Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}