{"record":{"id":"770f08ab197475ff","repo":"headroomlabs-ai/headroom","slug":"openai-api-key-required-set-openai-api-key-enviro","errorCode":null,"errorMessage":"OpenAI API key required. Set OPENAI_API_KEY environment variable or pass api_key parameter.","messagePattern":"OpenAI API key required\\. Set OPENAI_API_KEY environment variable or pass api_key parameter\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"headroom/evals/prompt_comparison.py","lineNumber":257,"sourceCode":"    Example:\n        result = compare_prompts(\n            original_prompt=\"Explain quantum computing in simple terms.\",\n            headroom_modified_prompt=\"Explain quantum computing in simple terms.\",\n        )\n        if not result.are_equivalent:\n            print(f\"WARNING: Prompts differ! {result.differences}\")\n    \"\"\"\n    try:\n        from openai import OpenAI\n    except ImportError as e:\n        raise ImportError(\n            \"OpenAI package required for prompt comparison. Install with: pip install openai\"\n        ) from e\n\n    # Get API key\n    resolved_api_key = api_key or os.environ.get(\"OPENAI_API_KEY\")\n    if not resolved_api_key:\n        raise ValueError(\n            \"OpenAI API key required. Set OPENAI_API_KEY environment variable \"\n            \"or pass api_key parameter.\"\n        )\n\n    client = OpenAI(api_key=resolved_api_key)\n\n    # Build the judge prompt\n    judge_prompt = SEMANTIC_EQUIVALENCE_JUDGE_PROMPT.format(\n        original_prompt=original_prompt,\n        modified_prompt=headroom_modified_prompt,\n    )\n\n    # Call the judge\n    response = client.chat.completions.create(\n        model=judge_model,\n        messages=[{\"role\": \"user\", \"content\": judge_prompt}],\n        temperature=0.0,  # Deterministic for consistent evaluation\n        max_tokens=500,","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/headroomlabs-ai/headroom/blob/322425c43bffde1ed0b64fecf3cf5951565dd82b/headroom/evals/prompt_comparison.py#L239-L275","documentation":"Raised by the LLM-judge prompt comparison in headroom.evals.prompt_comparison when no OpenAI API key can be resolved. The semantic-equivalence judge needs a live OpenAI call, so the function checks the explicit api_key parameter first, then the OPENAI_API_KEY environment variable, and refuses to proceed if both are empty. It is a configuration error thrown before any network traffic happens.","triggerScenarios":"Calling compare_messages()/SEMANTIC_EQUIVALENCE judge entry point (prompt_comparison.py:257 region) with api_key=None/omitted while OPENAI_API_KEY is unset or empty in the process environment (e.g. fresh shell, CI runner, container without the env var, or env var stripped by a subprocess wrapper).","commonSituations":"Running the prompt-preservation eval suite locally for the first time; CI pipelines that do not export OPENAI_API_KEY; dotenv files loaded after import time; running under a different user/service account than the one holding the key.","solutions":["Export the key: export OPENAI_API_KEY=sk-... in the shell (or add it to your .env / CI secrets) and rerun.","Pass the key explicitly: compare_messages(..., api_key='sk-...') so the code does not depend on the environment.","If the judge model is configurable, point judge_model at a provider that does not need OpenAI credentials.","Verify with: python -c \"import os; print(bool(os.environ.get('OPENAI_API_KEY')))\" before launching the eval."],"exampleFix":"# before\nresult = compare_messages(original_messages, modified_messages)  # ValueError: no key\n\n# after\nresult = compare_messages(\n    original_messages,\n    modified_messages,\n    api_key=os.environ[\"OPENAI_API_KEY\"],  # explicit, fails loudly at your boundary\n)","handlingStrategy":"validation","validationCode":"import os\n\napi_key = os.environ.get(\"OPENAI_API_KEY\")\nif not api_key:\n    raise SystemExit(\"OPENAI_API_KEY not set — the prompt-comparison judge requires it\")","typeGuard":null,"tryCatchPattern":"try:\n    result = compare_messages(original_messages, modified_messages, api_key=api_key)\nexcept ValueError as e:\n    if \"API key\" in str(e):\n        logger.error(\"judge not configured: %s\", e); sys.exit(2)\n    raise","preventionTips":["Set OPENAI_API_KEY in your shell profile, .env (loaded before evals), and CI secrets.","Pass api_key explicitly to compare_messages so config is visible in code.","Add a preflight check at eval startup that fails fast on missing keys."],"tags":["openai","api-key","configuration","evals","llm-judge"],"backgroundTag":null,"analyzedSha":"322425c43bffde1ed0b64fecf3cf5951565dd82b","analyzedAt":"2026-08-15T01:03:05.481Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}