locustio/locust · critical · Exception

You need to set OPENAI_API_KEY env var to use OpenAIUser

Error message

You need to set OPENAI_API_KEY env var to use OpenAIUser

What it means

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.

Source

Thrown at locust/contrib/oai.py:14

# Note: this User is experimental and may change without notice.
# The filename is oai.py so it doesnt clash with the openai package.
from locust.user import User

import os
import time
from collections.abc import Generator
from contextlib import contextmanager

import httpx
from openai import OpenAI  # dont forget to install openai

if not "OPENAI_API_KEY" in os.environ:
    raise Exception("You need to set OPENAI_API_KEY env var to use OpenAIUser")


class OpenAIClient(OpenAI):
    def __init__(self, request_event, user, *args, **kwargs):
        self.request_name = None  # used to override url-based request names
        self.user = user  # currently unused, but could be useful later

        def request_start(request):
            request.start_time = time.time()
            request.start_perf_counter = time.perf_counter()

        def request_end(response):
            exception = None
            response.read()
            response_time = (time.perf_counter() - response.request.start_perf_counter) * 1000
            try:
                response.raise_for_status()
            except httpx.HTTPStatusError as e:

View on GitHub (pinned to f391a716e1)

Solutions

  1. export OPENAI_API_KEY=<your key> before running locust
  2. Load the key into the environment from a .env file or CI secret
  3. Avoid importing contrib/oai in locustfiles that don't use OpenAIUser (the raise happens at import time)

Example fix

// before
$ locust -f openai_locustfile.py   # ImportError-time Exception: need OPENAI_API_KEY
// after
$ export OPENAI_API_KEY=sk-...
$ locust -f openai_locustfile.py
Defensive patterns

Strategy: validation

Validate before calling

import os
if not os.environ.get("OPENAI_API_KEY"):
    raise SystemExit("Set OPENAI_API_KEY before running this locustfile")

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of locustio/locust@f391a716e1 (2026-08-29). Data as JSON: /api/errors/51a3ff14ec754161. Report an issue: GitHub.