{"record":{"id":"e2213d77ef0c0daf","repo":"BerriAI/litellm","slug":"modelscope-api-key-is-not-set-please-set-it-via-e","errorCode":null,"errorMessage":"MODELSCOPE_API_KEY is not set. Please set it via environment variable or pass api_key parameter.","messagePattern":"MODELSCOPE_API_KEY is not set\\. Please set it via environment variable or pass api_key parameter\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/modelscope/image_generation/transformation.py","lineNumber":111,"sourceCode":"\n    @override\n    def validate_environment(\n        self,\n        headers: dict,\n        model: str,\n        messages: list[AllMessageValues],\n        optional_params: dict,\n        litellm_params: dict,\n        api_key: str | None = None,\n        api_base: str | None = None,\n    ) -> dict:\n        \"\"\"\n        Validate environment and set up headers for ModelScope.\n        \"\"\"\n        final_api_key: Final[str | None] = api_key or get_secret_str(\"MODELSCOPE_API_KEY\")\n\n        if not final_api_key:\n            raise ValueError(\n                \"MODELSCOPE_API_KEY is not set. Please set it via environment variable or pass api_key parameter.\"\n            )\n\n        default_headers: Final = {\n            \"Content-Type\": \"application/json\",\n            \"Authorization\": f\"Bearer {final_api_key}\",\n        }\n\n        headers = {**headers, **default_headers}\n        return headers\n\n    def transform_image_generation_request(\n        self,\n        model: str,\n        prompt: str,\n        optional_params: dict,\n        litellm_params: dict,\n        headers: dict,","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/modelscope/image_generation/transformation.py#L93-L129","documentation":"Raised by litellm's ModelScope image generation transformer when no API key can be resolved: the api_key parameter is falsy and the MODELSCOPE_API_KEY environment variable is unset. It is a pre-flight configuration error raised in validate_environment before any request is sent.","triggerScenarios":"Calling litellm.image_generation() with a modelscope/* model without api_key and without MODELSCOPE_API_KEY exported in the shell/process running litellm.","commonSituations":"Missing export in shell profile, container image built without the secret, CI env not injected, or the key stored under a different variable name.","solutions":["export MODELSCOPE_API_KEY=<your key> (from modelscope.cn -> access tokens) and restart the process.","Or pass api_key directly to the litellm.image_generation() call.","For litellm proxy, configure the key under the model deployment's litellm_params.","Confirm the variable name is exactly MODELSCOPE_API_KEY with no trailing whitespace."],"exampleFix":"# before\nimg = litellm.image_generation(model=\"modelscope/wanx-v1\", prompt=\"a cat\")\n\n# after\nimg = litellm.image_generation(\n    model=\"modelscope/wanx-v1\",\n    prompt=\"a cat\",\n    api_key=os.environ[\"MODELSCOPE_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\nif not os.getenv(\"MODELSCOPE_API_KEY\"):\n    raise RuntimeError(\"MODELSCOPE_API_KEY not set; get one from modelscope.cn\")","typeGuard":null,"tryCatchPattern":"try:\n    img = litellm.image_generation(model=\"modelscope/wanx-v1\", prompt=p)\nexcept ValueError as e:\n    if \"MODELSCOPE_API_KEY is not set\" in str(e):\n        raise RuntimeError(\"Config error: MODELSCOPE_API_KEY missing\") from e\n    raise","preventionTips":["Add env var presence checks for every provider you enable in a startup preflight.","Store the key in a secret manager; inject at deploy time.","Document required env vars per provider in the service README."],"tags":["modelscope","image-generation","api-key","configuration"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}