garak/generators/openai.py defines the OpenAICompatible generator base class, which provides shared initialization and execution for any OpenAI-compatible REST API (chat and completion endpoints).[1] The OpenAI generator reads the OPENAI_API_KEY environment variable and whitelists recognised model types to determine which sub-API (Completion vs ChatCompletion) to use.[2]
OpenAICompatible.DEFAULT_PARAMS sets these generation defaults: temperature=0.7, top_p=1.0, uri="http://localhost:8000/v1/", frequency_penalty=0.0, presence_penalty=0.0, seed=None, stop=["#", ";"], retry_json=True, suppressed_params=set(), and extra_params={}.[1] As of v0.13.3, OpenAICompatible generator classes perform one generation at a time by default, and supports_multiple_generations is False; subclasses that support n > 1 must override this flag.[3][1]
OpenAICompatible._load_unsafe() instantiates the OpenAI client with base_url=self.uri and raises ValueError if self.name is empty or None, because the model name is required.[1] After _load_unsafe() runs, OpenAICompatible.__init__() raises ValueError if self.generator is neither client.chat.completions nor client.completions, preventing unsupported endpoint types from silently failing.[1] OpenAICompatible marks client and generator as _unsafe_attributes, excluding them from safe serialization and pickling paths in the garak plugin system.[1]
OpenAICompatible._call_model() is decorated with @backoff.on_exception using Fibonacci backoff (max interval 70 s) that retries on openai.RateLimitError, openai.InternalServerError, openai.APITimeoutError, openai.APIConnectionError, and garak.exception.GeneratorBackoffTrigger.[1] At call time, if self.client is None, _call_model() reloads the OpenAI client via _load_unsafe(), allowing recovery after close() is called.[1] Parameters listed in self.suppressed_params are omitted from the create_args dict built in _call_model(), overriding even attribute values set on the generator instance.[1] OpenAICompatible._call_model() merges self.extra_params into create_args unconditionally, so extra_params can override any built-in argument — including suppressed ones.[1] OpenAICompatible._call_model() includes the actual HTTP status code in raised exceptions rather than generic error messages, giving clearer diagnostics for transient failures.
OpenAICompatible.close() silently swallows AttributeError, OSError, RuntimeError, and ValueError during client teardown (logging them at DEBUG level) and always sets self.client and self.generator to None.[1]
Supported audio formats in garak/generators/openai.py are wav and mp3, detected via audio_pattern = re.compile("|".join(audio_formats)).[1] In garak/generators/openai.py, the models gpt-4-32k, gpt-4-32k-0314, and gpt-4-32k-0613 are listed as deprecated with a scheduled shutdown of 2025-06-06.[1]
Sources