{"record":{"id":"7c8d4c2a2bb5d728","repo":"datawhalechina/hello-agents","slug":"llm-model-id-llm-api-key-and-llm-base-url-must-b","errorCode":null,"errorMessage":"LLM_MODEL_ID, LLM_API_KEY, and LLM_BASE_URL must be set (via constructor args or .env file).","messagePattern":"LLM_MODEL_ID, LLM_API_KEY, and LLM_BASE_URL must be set \\(via constructor args or \\.env file\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"Co-creation-projects/zjzhou-SREOnCallAgent/src/core/llm_client.py","lineNumber":27,"sourceCode":"class HelloAgentsLLM:\n    \"\"\"OpenAI-compatible LLM client (works with AIHubmix, ModelScope, OpenAI).\"\"\"\n\n    def __init__(\n        self,\n        model: str = None,\n        api_key: str = None,\n        base_url: str = None,\n        timeout: int = None,\n        verbose: bool = True,\n    ):\n        self.model = model or os.getenv(\"LLM_MODEL_ID\")\n        api_key = api_key or os.getenv(\"LLM_API_KEY\")\n        base_url = base_url or os.getenv(\"LLM_BASE_URL\")\n        timeout = timeout or int(os.getenv(\"LLM_TIMEOUT\", \"60\"))\n        self.verbose = verbose\n\n        if not all([self.model, api_key, base_url]):\n            raise ValueError(\n                \"LLM_MODEL_ID, LLM_API_KEY, and LLM_BASE_URL must be set \"\n                \"(via constructor args or .env file).\"\n            )\n\n        self.client = OpenAI(api_key=api_key, base_url=base_url, timeout=timeout)\n\n    def think(self, messages: List[Dict[str, str]], temperature: float = 0) -> str:\n        if self.verbose:\n            print(f\"🧠 Calling {self.model}...\")\n        try:\n            response = self.client.chat.completions.create(\n                model=self.model,\n                messages=messages,\n                temperature=temperature,\n                stream=True,\n            )\n            collected = []\n            for chunk in response:","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/zjzhou-SREOnCallAgent/src/core/llm_client.py#L9-L45","documentation":"ValueError raised by the LLMClient constructor when any of model, api_key, or base_url is None after checking constructor args and then the LLM_MODEL_ID / LLM_API_KEY / LLM_BASE_URL environment variables (or .env file). It is a fail-fast guard so the OpenAI client is never constructed with unusable credentials.","triggerScenarios":"Instantiating LLMClient() with no arguments when the .env file is missing, not loaded (python-dotenv load_dotenv() not called), or lacks one of the three variables; misspelled variable names like LLM_APIKEY; running in a fresh shell/CI where env vars were never exported.","commonSituations":"Cloning the repo without copying .env.example to .env; forgetting load_dotenv() at program start; CI pipelines with no secrets injected; switching between OpenAI-compatible providers and forgetting to update LLM_BASE_URL.","solutions":["Create a .env file with LLM_MODEL_ID, LLM_API_KEY, and LLM_BASE_URL set (all three are required)","Ensure load_dotenv() runs before LLMClient is constructed, or export the variables in the shell","Alternatively pass the values explicitly: LLMClient(model='...', api_key='...', base_url='...')","Check for typos in variable names — the constructor does not warn about near-misses"],"exampleFix":"# before\nclient = LLMClient()  # ValueError if env not set\n\n# after\nfrom dotenv import load_dotenv\nload_dotenv()  # loads .env with LLM_MODEL_ID / LLM_API_KEY / LLM_BASE_URL\nclient = LLMClient()","handlingStrategy":"validation","validationCode":"import os\n\ndef llm_env_ready() -> bool:\n    return all([os.getenv('LLM_MODEL_ID'), os.getenv('LLM_API_KEY'), os.getenv('LLM_BASE_URL')])\n\nassert llm_env_ready(), 'Set LLM_MODEL_ID, LLM_API_KEY, LLM_BASE_URL in .env first'","typeGuard":null,"tryCatchPattern":"try:\n    client = LLMClient()\nexcept ValueError as e:\n    if 'LLM_MODEL_ID' in str(e):\n        sys.exit('Missing LLM configuration: check .env / load_dotenv()')\n    raise","preventionTips":["Commit a .env.example with all three required keys documented","Call load_dotenv() once at program entry, before any client construction","Add a startup smoke test that constructs LLMClient() so config errors fail fast at boot"],"tags":["configuration","environment-variables","llm","openai"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}