Aider-AI/aider · critical · ValueError
Please set the OPENAI_API_KEY environment variable.
Error message
Please set the OPENAI_API_KEY environment variable.
What it means
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.
Source
Thrown at aider/voice.py:186
try:
transcript = litellm.transcription(
model="whisper-1", file=fh, prompt=history, language=language
)
except Exception as err:
print(f"Unable to transcribe {filename}: {err}")
return
if filename != temp_wav:
os.remove(filename)
text = transcript.text
return text
if __name__ == "__main__":
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("Please set the OPENAI_API_KEY environment variable.")
print(Voice().record_and_transcribe())
View on GitHub (pinned to 5dc9490bb3)
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.
Example fix
# before $ python aider/voice.py # ValueError: Please set the OPENAI_API_KEY environment variable. # after $ export OPENAI_API_KEY="sk-..." $ python aider/voice.py
Defensive patterns
Strategy: validation
Validate before calling
import os, sys
if not os.getenv("OPENAI_API_KEY"):
sys.exit("OPENAI_API_KEY not set — export it before using voice transcription.") Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- summarizer unexpectedly failed for all models
- GITHUB_COPILOT_TOKEN environment variable not found
- GITHUB_COPILOT_TOKEN environment variable is empty
- Device '{device_name}' not found. Available input devices: {
- Unsupported audio format: {audio_format}
AI-assisted analysis of Aider-AI/aider@5dc9490bb3 (2026-08-15).
Data as JSON: /api/errors/21a1d6a575844664.
Report an issue: GitHub.