{"record":{"id":"dee86141208ef65d","repo":"abi/screenshot-to-code","slug":"missing-replicate-api-key-or-replicate-api-token","errorCode":null,"errorMessage":"Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN","messagePattern":"Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN","errorType":"console","errorClass":"SystemExit","httpStatus":null,"severity":"critical","filePath":"backend/run_image_generation_evals.py","lineNumber":292,"sourceCode":"    parser = argparse.ArgumentParser(description=\"Run Replicate image generation evals.\")\n    parser.add_argument(\"--prompt-file\", type=Path, default=DEFAULT_PROMPT_FILE)\n    parser.add_argument(\"--output-root\", type=Path, default=DEFAULT_OUTPUT_ROOT)\n    parser.add_argument(\n        \"--model\",\n        choices=[\"flux_2_klein\", \"z_image_turbo\", \"both\"],\n        default=\"z_image_turbo\",\n    )\n    return parser.parse_args()\n\n\nasync def main() -> None:\n    load_dotenv(Path(\".env\"))\n    args = parse_args()\n    api_key = os.environ.get(\"REPLICATE_API_KEY\") or os.environ.get(\n        \"REPLICATE_API_TOKEN\"\n    )\n    if not api_key:\n        raise SystemExit(\"Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN\")\n\n    prompts = load_prompts(args.prompt_file)\n    models: list[ReplicateEvalModel]\n    if args.model == \"both\":\n        models = [\"flux_2_klein\", \"z_image_turbo\"]\n    else:\n        models = [args.model]\n\n    for model in models:\n        await run_model(model, prompts, api_key, args.output_root)\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","sourceCodeStart":274,"sourceCodeEnd":307,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/run_image_generation_evals.py#L274-L307","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Run from backend/: cd backend && REPLICATE_API_KEY=r8_... poetry run python run_image_generation_evals.py ...","Or export the key in the shell/CI environment: export REPLICATE_API_KEY=r8_... (REPLICATE_API_TOKEN is accepted as an alias)","Verify with: grep -c REPLICATE_API_KEY backend/.env (checks presence by name without printing the value)"],"exampleFix":"# before\npoetry run python run_image_generation_evals.py --prompt-file prompts.json  # run from repo root -> SystemExit\n\n# after\ncd backend && poetry run python run_image_generation_evals.py --prompt-file prompts.json  # picks up backend/.env","handlingStrategy":"validation","validationCode":"import os\n\ndef require_replicate_key() -> str:\n    key = os.environ.get(\"REPLICATE_API_KEY\") or os.environ.get(\"REPLICATE_API_TOKEN\")\n    if not key:\n        raise SystemExit(\n            \"Missing REPLICATE_API_KEY or REPLICATE_API_TOKEN — add it to backend/.env \"\n            \"and run from backend/ so load_dotenv finds it\"\n        )\n    return key","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the eval script from backend/ so load_dotenv(Path('.env')) finds backend/.env","Set REPLICATE_API_KEY explicitly in CI/cron environments instead of relying on .env discovery","Check presence by name only (grep -c REPLICATE_API_KEY backend/.env); never echo the value"],"tags":["environment-variables","dotenv","replicate","cli","secrets"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}