abi/screenshot-to-code · critical · SystemExit

Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN

Error message

Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN

What it means

SystemExit("Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN") raised by main() when neither environment variable is set after load_dotenv(Path(".env")) runs. load_dotenv resolves .env relative to the current working directory, so running the script from anywhere other than backend/ silently skips backend/.env and the key is never loaded. The eval script needs a Replicate token for every image-generation call, so it aborts before loading prompts.

Source

Thrown at backend/run_image_generation_evals.py:292

    parser = argparse.ArgumentParser(description="Run Replicate image generation evals.")
    parser.add_argument("--prompt-file", type=Path, default=DEFAULT_PROMPT_FILE)
    parser.add_argument("--output-root", type=Path, default=DEFAULT_OUTPUT_ROOT)
    parser.add_argument(
        "--model",
        choices=["flux_2_klein", "z_image_turbo", "both"],
        default="z_image_turbo",
    )
    return parser.parse_args()


async def main() -> None:
    load_dotenv(Path(".env"))
    args = parse_args()
    api_key = os.environ.get("REPLICATE_API_KEY") or os.environ.get(
        "REPLICATE_API_TOKEN"
    )
    if not api_key:
        raise SystemExit("Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN")

    prompts = load_prompts(args.prompt_file)
    models: list[ReplicateEvalModel]
    if args.model == "both":
        models = ["flux_2_klein", "z_image_turbo"]
    else:
        models = [args.model]

    for model in models:
        await run_model(model, prompts, api_key, args.output_root)


if __name__ == "__main__":
    asyncio.run(main())

View on GitHub (pinned to d026163f58)

Solutions

  1. Run from backend/: cd backend && REPLICATE_API_KEY=r8_... poetry run python run_image_generation_evals.py ...
  2. Or export the key in the shell/CI environment: export REPLICATE_API_KEY=r8_... (REPLICATE_API_TOKEN is accepted as an alias)
  3. Verify with: grep -c REPLICATE_API_KEY backend/.env (checks presence by name without printing the value)

Example fix

# before
poetry run python run_image_generation_evals.py --prompt-file prompts.json  # run from repo root -> SystemExit

# after
cd backend && poetry run python run_image_generation_evals.py --prompt-file prompts.json  # picks up backend/.env
Defensive patterns

Strategy: validation

Validate before calling

import os

def require_replicate_key() -> str:
    key = os.environ.get("REPLICATE_API_KEY") or os.environ.get("REPLICATE_API_TOKEN")
    if not key:
        raise SystemExit(
            "Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN — add it to backend/.env "
            "and run from backend/ so load_dotenv finds it"
        )
    return key

Prevention

When it happens

Trigger: Running `python run_image_generation_evals.py` from the repo root (no .env there) with the key only in backend/.env; or the key defined only in the interactive shell profile of another machine/CI runner.

Common situations: Repo's documented workflow keeps REPLICATE_API_KEY in backend/.env (it cannot be set via the UI per project docs); CI jobs or cron that do not cd into backend/; key named differently (e.g. REPLICATE_TOKEN).

Related errors


AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14). Data as JSON: /api/errors/dee86141208ef65d. Report an issue: GitHub.