huggingface/open-r1 · error
MorphCloud API key not found. Please set the MORPH_API_KEY e
Error message
MorphCloud API key not found. Please set the MORPH_API_KEY environment variable.
What it means
After passing the availability check, MorphProvider reads MORPH_API_KEY from the environment and raises ValueError if it is unset or empty. MorphCloudClient requires an API key to authenticate sandbox requests, so construction aborts early rather than failing on the first API call.
Source
Thrown at src/open_r1/utils/code_providers.py:203
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
print("Warning: python-dotenv not installed. Environment variables must be set directly.")
self.num_parallel = num_parallel
self.morph_router_url = morph_router_url
if self.morph_router_url is not None:
self.routed_sandbox = RoutedMorphSandbox(router_url=self.morph_router_url)
return
import os
self.api_key = os.getenv("MORPH_API_KEY")
if not self.api_key:
raise ValueError("MorphCloud API key not found. Please set the MORPH_API_KEY environment variable.")
try:
self.client = MorphCloudClient(api_key=self.api_key)
self.Sandbox = Sandbox
except ImportError as e:
raise ImportError(f"Required MorphCloud dependencies not installed: {e}")
def execute_scripts(self, scripts: List[str], languages: List[str]) -> List[float]:
"""Execute scripts using MorphCloud Sandbox API.
Args:
scripts: List of Python scripts to execute
language: Programming language
Returns:
List of float rewards (one per script)
"""
View on GitHub (pinned to 1416fa0cf2)
Solutions
- Export MORPH_API_KEY in the shell/CI environment before launching the job
- Add MORPH_API_KEY=... to a .env file in the directory from which the training script is launched (the provider loads .env via load_dotenv)
- Verify: python -c "import os; print(bool(os.getenv('MORPH_API_KEY')))"
- Check secrets propagation into containers/SLURM jobs (e.g. --export or env-file flags)
Example fix
// before provider = MorphProvider() # ValueError: MORPH_API_KEY not found // after # .env in working directory # MORPH_API_KEY=morph_xxxx provider = MorphProvider()
Defensive patterns
Strategy: validation
Validate before calling
import os
from pathlib import Path
def require_morph_api_key():
if os.getenv("MORPH_API_KEY"):
return
env_file = Path.cwd() / ".env"
if env_file.exists() and "MORPH_API_KEY=" in env_file.read_text():
return
raise SystemExit("MORPH_API_KEY not set: export it or add MORPH_API_KEY=... to .env in the launch directory") Type guard
def has_morph_api_key() -> bool:
import os
return bool(os.getenv("MORPH_API_KEY")) Try / catch
try:
provider = MorphProvider()
except ValueError as e:
if "MORPH_API_KEY" in str(e):
logger.error("Provide MORPH_API_KEY via environment or .env in cwd")
raise SystemExit(1) from e
raise Prevention
- Export MORPH_API_KEY in CI/cluster secrets, not just local shells
- Launch jobs from the directory containing .env, or load it explicitly with python-dotenv
- Verify with `python -c "import os; print(bool(os.getenv('MORPH_API_KEY')))"` on the target node
- Watch for whitespace/newlines corrupting the value in .env
When it happens
Trigger: Constructing MorphProvider when the morphcloud package is installed but the MORPH_API_KEY environment variable is not set (or set to empty string) at the time os.getenv runs in __init__.
Common situations: Forgot to add MORPH_API_KEY to .env or the .env is in the wrong working directory; env var exported in a shell profile but the job runs under a different user/CI context; secrets not passed into the Docker/SLURM job; typos like MORPH_API_KEY vs MORPH_CLOUD_API_KEY.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- CF_TESTS_FOLDER environment variable not set! Please downloa
- Either `dataset_name` or `dataset_mixture` must be provided
- dataset_mixture must be a dictionary with a 'datasets' key.
- 'datasets' must be a list of dataset configurations
- Column names must be consistent across all dataset configura
AI-assisted analysis of huggingface/open-r1@1416fa0cf2 (2026-08-30).
Data as JSON: /api/errors/6f32460eda809987.
Report an issue: GitHub.