{"record":{"id":"3abe1a6f03b0fd31","repo":"BerriAI/litellm","slug":"error-watsonx-project-id-and-space-id-not-set-se","errorCode":null,"errorMessage":"Error: Watsonx project_id and space_id not set. Set WX_PROJECT_ID or WX_SPACE_ID in environment variables or pass in as a parameter.","messagePattern":"Error: Watsonx project_id and space_id not set\\. Set WX_PROJECT_ID or WX_SPACE_ID in environment variables or pass in as a parameter\\.","errorType":"exception","errorClass":"WatsonXAIError","httpStatus":401,"severity":"error","filePath":"litellm/llms/watsonx/common_utils.py","lineNumber":111,"sourceCode":"        )  # consistent with how vertex ai + aws regions are accepted\n\n    # Load auth variables from environment variables\n    if project_id is None:\n        project_id = (\n            get_secret_str(\"WATSONX_PROJECT_ID\") or get_secret_str(\"WX_PROJECT_ID\") or get_secret_str(\"PROJECT_ID\")\n        )\n    if region_name is None:\n        region_name = get_secret_str(\"WATSONX_REGION\") or get_secret_str(\"WX_REGION\") or get_secret_str(\"REGION\")\n    if space_id is None:\n        space_id = (\n            get_secret_str(\"WATSONX_DEPLOYMENT_SPACE_ID\")\n            or get_secret_str(\"WATSONX_SPACE_ID\")\n            or get_secret_str(\"WX_SPACE_ID\")\n            or get_secret_str(\"SPACE_ID\")\n        )\n\n    if project_id is None and space_id is None and model is not None and not model.startswith(\"deployment/\"):\n        raise WatsonXAIError(\n            status_code=401,\n            message=\"Error: Watsonx project_id and space_id not set. Set WX_PROJECT_ID or WX_SPACE_ID in environment variables or pass in as a parameter.\",\n        )\n\n    return WatsonXAPIParams(\n        project_id=project_id,\n        space_id=space_id,\n        region_name=region_name,\n    )\n\n\nasync def _aconvert_watsonx_messages_core(\n    model: str,\n    messages: list[AllMessageValues],\n    provider: str,\n    custom_prompt_dict: dict,\n    apply_template_fn,\n) -> str:","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/watsonx/common_utils.py#L93-L129","documentation":"WatsonX requires scoping every request to a project or a deployment space. _get_api_params resolves project_id from params (project_id / watsonx_project) or env (WATSONX_PROJECT_ID / WX_PROJECT_ID / PROJECT_ID), and space_id from params or env (WATSONX_DEPLOYMENT_SPACE_ID / WATSONX_SPACE_ID / WX_SPACE_ID / SPACE_ID). If both are None, a model is given, and the model is not a 'deployment/...' id, this WatsonXAIError (401) is raised before the request.","triggerScenarios":"litellm.completion(model=\"watsonx/meta-llama/llama-3-8b-instruct\", ...) with no project_id/space_id anywhere; env vars named PROJECT_ID set to empty string in some shells; calling a base model instead of a deployed one without scoping.","commonSituations":"Copying model calls from IBM docs without copying the project setup; per-tenant processes where PROJECT_ID is set only for some tenants; switching from deployment models (which bypass the check) to foundation models.","solutions":["export WX_PROJECT_ID=<your-project-id> (or WATSONX_PROJECT_ID / PROJECT_ID).","Or pass project_id as a call param: litellm.completion(..., project_id=...).","For deployed models, use model=\"deployment/<deployment-id>\" and set space_id instead.","In the proxy, put project_id in the watsonx model's litellm_params."],"exampleFix":"# before\nresp = litellm.completion(model=\"watsonx/meta-llama/llama-3-8b-instruct\", messages=[{\"role\": \"user\", \"content\": \"hi\"}])\n# -> WatsonXAIError: project_id and space_id not set\n\n# after\nresp = litellm.completion(\n    model=\"watsonx/meta-llama/llama-3-8b-instruct\",\n    messages=[{\"role\": \"user\", \"content\": \"hi\"}],\n    project_id=os.environ[\"WX_PROJECT_ID\"],\n)","handlingStrategy":"validation","validationCode":"import os\n\nproject_id = os.getenv(\"WATSONX_PROJECT_ID\") or os.getenv(\"WX_PROJECT_ID\")\nspace_id = os.getenv(\"WATSONX_SPACE_ID\") or os.getenv(\"WX_SPACE_ID\")\nif not project_id and not space_id and not model.startswith(\"deployment/\"):\n    raise RuntimeError(\"WatsonX needs WX_PROJECT_ID or WX_SPACE_ID for non-deployment models\")\nresp = litellm.completion(model=model, messages=msgs, project_id=project_id)","typeGuard":"const isDeploymentModel = (model: string): boolean => model.startsWith(\"deployment/\");\n\nconst watsonxScoped = (model: string, env: Record<string, string | undefined>): boolean =>\n  isDeploymentModel(model) || Boolean(env.WX_PROJECT_ID ?? env.WX_SPACE_ID ?? env.WATSONX_PROJECT_ID);","tryCatchPattern":"from litellm.llms.watsonx.common_utils import WatsonXAIError\n\ntry:\n    resp = litellm.completion(model=model, messages=msgs)\nexcept WatsonXAIError as e:\n    if \"project_id and space_id not set\" in e.message:\n        raise RuntimeError(\"Set WX_PROJECT_ID (or WX_SPACE_ID) for WatsonX foundation models\") from e\n    raise","preventionTips":["Export WX_PROJECT_ID next to WATSONX_APIKEY in all environments.","For deployed models use the 'deployment/<id>' prefix, which bypasses the project check.","Store project ids per environment (dev/stage/prod) in config, not in code."],"tags":["watsonx","project-id","space-id","configuration","environment-variable","litellm"],"backgroundTag":"missing-required-config","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}