{"record":{"id":"cd14c206820d7f00","repo":"chenfei-wu/TaskMatrix","slug":"openai-api-error","errorCode":null,"errorMessage":"OpenAI API error.","messagePattern":"OpenAI API error\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"LowCodeLLM/src/executingLLM.py","lineNumber":42,"sourceCode":"\"\"\"\r\n\r\nclass executingLLM:\r\n    def __init__(self, temperature) -> None:\r\n        self.prefix = EXECUTING_LLM_PREFIX\r\n        self.suffix = EXECUTING_LLM_SUFFIX\r\n        self.LLM = OpenAIWrapper(temperature)\r\n        self.messages = [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\r\n                         {\"role\": \"system\", \"content\": self.prefix}]\r\n\r\n    def execute(self, current_prompt, history):\r\n        ''' provide LLM the dialogue history and the current prompt to get response '''\r\n        messages = self.messages + history\r\n        messages.append({'role': 'user', \"content\": current_prompt + self.suffix})\r\n        response, status = self.LLM.run(messages)\r\n        if status:\r\n            return response\r\n        else:\r\n            return \"OpenAI API error.\"","sourceCodeStart":24,"sourceCodeEnd":42,"githubUrl":"https://github.com/chenfei-wu/TaskMatrix/blob/4b7664f8d3a23804ac1b795d75a73efd162769f0/LowCodeLLM/src/executingLLM.py#L24-L42","documentation":"Returned (not raised) by executingLLM.execute when OpenAIWrapper.run reports failure. The wrapper catches every exception from openai.ChatCompletion.create and returns ('', False), so this string masks the true OpenAI error (auth, rate limit, timeout, quota, invalid deployment).","triggerScenarios":"Calling llm.execute with any conditions that make openai.ChatCompletion.create throw: OPENAIKEY unset/invalid, USE_AZURE=true but API_BASE/API_VERSION/MODEL unset or pointing at a wrong deployment, rate limit/quota exceeded, or network unreachable. Also triggered by malformed history entries missing 'role'/'content'.","commonSituations":"Env vars not exported into the Flask process; Azure deployment name not matching MODEL; expired billing/quota; using the deprecated openai<1.0 API against a newer key/provider that requires it (or vice versa).","solutions":["Log the exception inside OpenAIWrapper._post_request_chat instead of swallowing it, then rerun to see the real OpenAI error.","Verify OPENAIKEY is set and valid; if USE_AZURE=true verify API_BASE, API_VERSION, MODEL (deployment name).","Fix malformed history (list of {'role','content'} dicts) that the API would reject.","Retry with backoff for transient rate-limit/network errors."],"exampleFix":"# before (openAIWrapper.py)\nexcept Exception as e:\n    return \"\", False\n# after\nexcept Exception as e:\n    print('OpenAI call failed:', repr(e))\n    return \"\", False","handlingStrategy":"retry","validationCode":"import os\nassert os.environ.get('OPENAIKEY'), 'OPENAIKEY not set'\nif os.environ.get('USE_AZURE','').lower() == 'true':\n    for v in ('API_BASE','API_VERSION','MODEL'): assert os.environ.get(v), v + ' not set'","typeGuard":"def is_llm_error(resp) -> bool:\n    return isinstance(resp, str) and resp.strip() == 'OpenAI API error.'","tryCatchPattern":"for attempt in range(3):\n    out = llm.execute(prompt, history)\n    if not is_llm_error(out): break\n    time.sleep(2 ** attempt)","preventionTips":["Export OpenAI/Azure env vars in the process running app.py","Validate history messages are role/content dicts","Log wrapper exceptions instead of swallowing them"],"tags":["openai","chatcompletion","azure-openai","api-key","lowcodellm"],"backgroundTag":"openai-api-error","analyzedSha":"4b7664f8d3a23804ac1b795d75a73efd162769f0","analyzedAt":"2026-08-27T13:26:11.787Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}