{"record":{"id":"6f32460eda809987","repo":"huggingface/open-r1","slug":"morphcloud-api-key-not-found-please-set-the-morph","errorCode":null,"errorMessage":"MorphCloud API key not found. Please set the MORPH_API_KEY environment variable.","messagePattern":"MorphCloud API key not found\\. Please set the MORPH_API_KEY environment variable\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/open_r1/utils/code_providers.py","lineNumber":203,"sourceCode":"        try:\n            from dotenv import load_dotenv\n\n            load_dotenv()\n        except ImportError:\n            print(\"Warning: python-dotenv not installed. Environment variables must be set directly.\")\n\n        self.num_parallel = num_parallel\n        self.morph_router_url = morph_router_url\n\n        if self.morph_router_url is not None:\n            self.routed_sandbox = RoutedMorphSandbox(router_url=self.morph_router_url)\n            return\n\n        import os\n\n        self.api_key = os.getenv(\"MORPH_API_KEY\")\n        if not self.api_key:\n            raise ValueError(\"MorphCloud API key not found. Please set the MORPH_API_KEY environment variable.\")\n\n        try:\n            self.client = MorphCloudClient(api_key=self.api_key)\n            self.Sandbox = Sandbox\n        except ImportError as e:\n            raise ImportError(f\"Required MorphCloud dependencies not installed: {e}\")\n\n    def execute_scripts(self, scripts: List[str], languages: List[str]) -> List[float]:\n        \"\"\"Execute scripts using MorphCloud Sandbox API.\n\n        Args:\n            scripts: List of Python scripts to execute\n            language: Programming language\n\n        Returns:\n            List of float rewards (one per script)\n        \"\"\"\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/huggingface/open-r1/blob/1416fa0cf21595d2083b399a2a0bbddd7f6e9563/src/open_r1/utils/code_providers.py#L185-L221","documentation":"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.","triggerScenarios":"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__.","commonSituations":"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.","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)"],"exampleFix":"// before\nprovider = MorphProvider()  # ValueError: MORPH_API_KEY not found\n// after\n# .env in working directory\n# MORPH_API_KEY=morph_xxxx\nprovider = MorphProvider()","handlingStrategy":"validation","validationCode":"import os\nfrom pathlib import Path\n\ndef require_morph_api_key():\n    if os.getenv(\"MORPH_API_KEY\"):\n        return\n    env_file = Path.cwd() / \".env\"\n    if env_file.exists() and \"MORPH_API_KEY=\" in env_file.read_text():\n        return\n    raise SystemExit(\"MORPH_API_KEY not set: export it or add MORPH_API_KEY=... to .env in the launch directory\")","typeGuard":"def has_morph_api_key() -> bool:\n    import os\n    return bool(os.getenv(\"MORPH_API_KEY\"))","tryCatchPattern":"try:\n    provider = MorphProvider()\nexcept ValueError as e:\n    if \"MORPH_API_KEY\" in str(e):\n        logger.error(\"Provide MORPH_API_KEY via environment or .env in cwd\")\n        raise SystemExit(1) from e\n    raise","preventionTips":["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"],"tags":["python","configuration","missing-env-var","api-key"],"backgroundTag":"missing-env-var","analyzedSha":"1416fa0cf21595d2083b399a2a0bbddd7f6e9563","analyzedAt":"2026-08-30T08:56:53.400Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}