{"record":{"id":"21a1d6a575844664","repo":"Aider-AI/aider","slug":"please-set-the-openai-api-key-environment-variable","errorCode":null,"errorMessage":"Please set the OPENAI_API_KEY environment variable.","messagePattern":"Please set the OPENAI_API_KEY environment variable\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"aider/voice.py","lineNumber":186,"sourceCode":"            try:\n                transcript = litellm.transcription(\n                    model=\"whisper-1\", file=fh, prompt=history, language=language\n                )\n            except Exception as err:\n                print(f\"Unable to transcribe {filename}: {err}\")\n                return\n\n        if filename != temp_wav:\n            os.remove(filename)\n\n        text = transcript.text\n        return text\n\n\nif __name__ == \"__main__\":\n    api_key = os.getenv(\"OPENAI_API_KEY\")\n    if not api_key:\n        raise ValueError(\"Please set the OPENAI_API_KEY environment variable.\")\n    print(Voice().record_and_transcribe())\n","sourceCodeStart":168,"sourceCodeEnd":188,"githubUrl":"https://github.com/Aider-AI/aider/blob/5dc9490bb35f9729ef2c95d00a19ccd30c26339c/aider/voice.py#L168-L188","documentation":"The __main__ block of aider/voice.py requires OPENAI_API_KEY before constructing Voice(), because transcription uses OpenAI's audio API. It raises ValueError('Please set the OPENAI_API_KEY environment variable.') when os.getenv returns empty. Note this is script-invocation-only (python -m aider.voice or python aider/voice.py); when aider runs normally, voice failures surface as different errors from the configured model.","triggerScenarios":"Running aider/voice.py directly as a script without OPENAI_API_KEY exported. Checked at startup via os.getenv, so it fails before any recording or transcription attempt.","commonSituations":"Quick mic-transcription tests with python aider/voice.py in a fresh shell where the key lives in another profile's rc file; CI/scheduled environments without the secret; users on Azure/local models who never set an OpenAI key.","solutions":["Export the key: export OPENAI_API_KEY=sk-... (or add it to your shell profile) and re-run.","For persistent use, store it in your shell rc or a secrets manager that injects env vars.","If you use a non-OpenAI provider, invoke voice through the main aider CLI with your configured model rather than this script entry point."],"exampleFix":"# before\n$ python aider/voice.py  # ValueError: Please set the OPENAI_API_KEY environment variable.\n\n# after\n$ export OPENAI_API_KEY=\"sk-...\"\n$ python aider/voice.py","handlingStrategy":"validation","validationCode":"import os, sys\n\nif not os.getenv(\"OPENAI_API_KEY\"):\n    sys.exit(\"OPENAI_API_KEY not set — export it before using voice transcription.\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set OPENAI_API_KEY in the shell profile or via a secrets manager before running the voice script.","For non-OpenAI setups, use the main aider CLI with your configured model instead of python aider/voice.py."],"tags":["environment-variables","api-key","openai","voice","aider"],"backgroundTag":null,"analyzedSha":"5dc9490bb35f9729ef2c95d00a19ccd30c26339c","analyzedAt":"2026-08-15T05:40:10.498Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}