{"record":{"id":"51a3ff14ec754161","repo":"locustio/locust","slug":"you-need-to-set-openai-api-key-env-var-to-use-open","errorCode":null,"errorMessage":"You need to set OPENAI_API_KEY env var to use OpenAIUser","messagePattern":"You need to set OPENAI_API_KEY env var to use OpenAIUser","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"locust/contrib/oai.py","lineNumber":14,"sourceCode":"# Note: this User is experimental and may change without notice.\n# The filename is oai.py so it doesnt clash with the openai package.\nfrom locust.user import User\n\nimport os\nimport time\nfrom collections.abc import Generator\nfrom contextlib import contextmanager\n\nimport httpx\nfrom openai import OpenAI  # dont forget to install openai\n\nif not \"OPENAI_API_KEY\" in os.environ:\n    raise Exception(\"You need to set OPENAI_API_KEY env var to use OpenAIUser\")\n\n\nclass OpenAIClient(OpenAI):\n    def __init__(self, request_event, user, *args, **kwargs):\n        self.request_name = None  # used to override url-based request names\n        self.user = user  # currently unused, but could be useful later\n\n        def request_start(request):\n            request.start_time = time.time()\n            request.start_perf_counter = time.perf_counter()\n\n        def request_end(response):\n            exception = None\n            response.read()\n            response_time = (time.perf_counter() - response.request.start_perf_counter) * 1000\n            try:\n                response.raise_for_status()\n            except httpx.HTTPStatusError as e:","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/locustio/locust/blob/f391a716e12c2c712e80b5835e877b7933397453/locust/contrib/oai.py#L1-L32","documentation":"Importing locust.contrib.oai (which defines OpenAIUser) raises a plain Exception at module import time if the OPENAI_API_KEY environment variable is not set, because the OpenAI SDK client cannot authenticate without it.","triggerScenarios":"Any import of locust/contrib/oai.py — e.g. 'from locust.contrib.oai import OpenAIUser' in a locustfile — executed in an environment where OPENAI_API_KEY is unset or empty.","commonSituations":"Running the OpenAI load test locally without exporting the key; CI pipelines missing the secret; virtualenvs or containers that don't inherit the env var; typos like OPEN_AI_API_KEY in the environment.","solutions":["export OPENAI_API_KEY=<your key> before running locust","Load the key into the environment from a .env file or CI secret","Avoid importing contrib/oai in locustfiles that don't use OpenAIUser (the raise happens at import time)"],"exampleFix":"// before\n$ locust -f openai_locustfile.py   # ImportError-time Exception: need OPENAI_API_KEY\n// after\n$ export OPENAI_API_KEY=sk-...\n$ locust -f openai_locustfile.py","handlingStrategy":"validation","validationCode":"import os\nif not os.environ.get(\"OPENAI_API_KEY\"):\n    raise SystemExit(\"Set OPENAI_API_KEY before running this locustfile\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Export OPENAI_API_KEY in your shell profile or CI secrets","Use .env loading (e.g. dotenv) at the top of the locustfile","Only import locust.contrib.oai in locustfiles that actually use OpenAIUser, since the check fires at import time"],"tags":["python","openai","environment-variable","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"f391a716e12c2c712e80b5835e877b7933397453","analyzedAt":"2026-08-29T00:36:13.872Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}