nexu-io/open-design · error · SystemExit

image API response did not contain data[0]

Error message

image API response did not contain data[0]

What it means

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.

Source

Thrown at skills/hatch-pet/scripts/generate_pet_images.py:155

    }).encode()
    req = urllib.request.Request(
        "https://api.openai.com/v1/images/generations",
        data=payload,
        headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
        method="POST",
    )
    with urllib.request.urlopen(req, timeout=300) as resp:
        output_json.write_bytes(resp.read())
    response = json.loads(output_json.read_text(encoding="utf-8"))
    if response.get("error"):
        raise SystemExit(json.dumps(response["error"], indent=2))
    return response


def decode_response(response: dict[str, object], output_image: Path) -> None:
    data = response.get("data")
    if not isinstance(data, list) or not data:
        raise SystemExit("image API response did not contain data[0]")
    first = data[0]
    if not isinstance(first, dict) or not isinstance(first.get("b64_json"), str):
        raise SystemExit("image API response did not contain data[0].b64_json")
    output_image.parent.mkdir(parents=True, exist_ok=True)
    output_image.write_bytes(base64.b64decode(first["b64_json"]))


def file_sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as file:
        for chunk in iter(lambda: file.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def complete_job(job: dict[str, object], output_path: Path) -> None:
    job["status"] = "complete"
    job["source_path"] = str(output_path)

View on GitHub (pinned to 5be4028344)

Solutions

  1. Open run_dir/raw/<job_id>.response.json and inspect the actual top-level keys to see what the API returned.
  2. If the body shows a quota/billing error, add credits or switch to a key with quota, then rerun the job.
  3. If data[] entries use url instead of b64_json, request b64 output explicitly or extend decode_response to fetch the url.
  4. If the response is garbled/truncated, rerun; if it persists, check network/proxy stability.

Example fix

// before
data = response.get("data")
if not isinstance(data, list) or not data:
    raise SystemExit("image API response did not contain data[0]")

// after - include the response shape so the failure is self-diagnosing
data = response.get("data")
if not isinstance(data, list) or not data:
    raise SystemExit(
        "image API response did not contain data[0]; top-level keys: "
        + ", ".join(sorted(response.keys()))
    )
Defensive patterns

Strategy: validation

Validate before calling

def has_data_array(response: dict) -> bool:
    data = response.get("data")
    return isinstance(data, list) and len(data) > 0

# before calling decode_response:
if not has_data_array(response):
    raise SystemExit(f"unexpected response shape; keys={sorted(response)}")

Type guard

from typing import Any

def is_image_response(value: Any) -> bool:
    return (
        isinstance(value, dict)
        and isinstance(value.get("data"), list)
        and len(value["data"]) > 0
        and isinstance(value["data"][0], dict)
        and isinstance(value["data"][0].get("b64_json"), str)
    )

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of nexu-io/open-design@5be4028344 (2026-08-12). Data as JSON: /api/errors/8ae43dee12ced9b7. Report an issue: GitHub.