nexu-io/open-design · error · SystemExit

OPENAI_API_KEY is not set

Error message

OPENAI_API_KEY is not set

What it means

Raised by main() before any API call: the OPENAI_API_KEY environment variable is unset or empty. The script reads the key once from os.environ and refuses to proceed without it because every image request needs a Bearer token.

Source

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

        if not path.is_file():
            raise SystemExit(f"input image for job {job.get('id')} not found: {path}")
        paths.append(path)
    return paths


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--run-dir", required=True)
    parser.add_argument("--model", default="gpt-image-2")
    parser.add_argument("--size", default="1024x1024")
    parser.add_argument("--states", default="all")
    parser.add_argument("--job-id", action="append", default=[])
    parser.add_argument("--skip-base", action="store_true")
    args = parser.parse_args()

    api_key = os.environ.get("OPENAI_API_KEY")
    if not api_key:
        raise SystemExit("OPENAI_API_KEY is not set")

    run_dir = Path(args.run_dir).expanduser().resolve()
    manifest_path = run_dir / "imagegen-jobs.json"
    manifest = load_manifest(run_dir)
    jobs = select_jobs(
        manifest,
        states=parse_states(args.states),
        skip_base=args.skip_base,
        job_ids=args.job_id,
    )
    raw_dir = run_dir / "raw"

    completed = []
    for job in jobs:
        job_id = str(job.get("id"))
        prompt_raw = job.get("prompt_file")
        output_raw = job.get("output_path")
        if not isinstance(prompt_raw, str) or not isinstance(output_raw, str):

View on GitHub (pinned to 5be4028344)

Solutions

  1. Export the key in the same shell: export OPENAI_API_KEY=sk-... then rerun.
  2. For repeated runs, put the export in your shell rc or a sourced .env file (do not commit it).
  3. In CI, set the OPENAI_API_KEY secret on the job environment.
  4. Verify with: echo "${OPENAI_API_KEY:+set}" prints set.

Example fix

// before
python skills/hatch-pet/scripts/generate_pet_images.py --run-dir ./out
# SystemExit: OPENAI_API_KEY is not set

// after
export OPENAI_API_KEY="$(cat ~/.openai_key)"
python skills/hatch-pet/scripts/generate_pet_images.py --run-dir ./out
Defensive patterns

Strategy: validation

Validate before calling

import os

api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
    raise SystemExit("OPENAI_API_KEY is not set; export it or set it in the CI secret")

Prevention

When it happens

Trigger: Launching generate_pet_images.py in a shell that did not export OPENAI_API_KEY, exported it as an empty string, or in a container/CI job that did not receive the secret.

Common situations: Forgot to source the .env file; ran the script in a new terminal without exporting the key; CI secret name typo; running as a different user that lacks the env var.

Related errors


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