Skip to content

fix: don't crash at startup when OPENAI_API_KEY is unset#155

Open
lvjr3383 wants to merge 1 commit into
safety-research:mainfrom
lvjr3383:fix/openai-client-init-without-api-key
Open

fix: don't crash at startup when OPENAI_API_KEY is unset#155
lvjr3383 wants to merge 1 commit into
safety-research:mainfrom
lvjr3383:fix/openai-client-init-without-api-key

Conversation

@lvjr3383

Copy link
Copy Markdown

Problem

InferenceAPI.__init__() eagerly constructs all provider clients at instantiation time, including four OpenAI-specific classes:

File Line Failing call
openai/base.py 72 openai.AsyncClient(base_url=...)
openai/embedding.py 23 openai.AsyncClient()
openai/moderation.py 30 openai.AsyncClient()
openai/s2s.py 49 os.environ["OPENAI_API_KEY"]

openai v1+ raises OpenAIError at construction time if OPENAI_API_KEY is not in the environment — even if the caller only intends to use Anthropic, Together, or Gemini models. The result is an immediate crash before any model routing occurs:

openai.OpenAIError: The api_key client option must be set either by
passing api_key to the client or by setting the OPENAI_API_KEY
environment variable

This affects every researcher running the alignment-faking paper replication with Anthropic or Together models and no OpenAI key.

Fix

Replace hard environment access with a sentinel fallback:

# before
self.aclient = openai.AsyncClient()                  # crashes
self.api_key = os.environ["OPENAI_API_KEY"]          # crashes

# after
self.aclient = openai.AsyncClient(
    api_key=os.environ.get("OPENAI_API_KEY", "not-configured")
)
self.api_key = os.environ.get("OPENAI_API_KEY", "not-configured")

The client now initialises without error. If a caller actually invokes an OpenAI model without a real key, the failure happens at call time with HTTP 401 — which is clear and expected.

Testing

Verified on open-source-alignment-faking with no OPENAI_API_KEY set, running claude-3-5-sonnet-20240620. Before this patch: crash at line 1. After: pipeline initialises, loads dataset, and reaches API call execution.

lvjr3383 pushed a commit to lvjr3383/AI_Safety that referenced this pull request Feb 27, 2026
Audited safety-research/open-source-alignment-faking for reproducibility.
Found 10 friction points blocking any researcher from running the pipeline
out of the box, including 3 hard crashes before a single API call.

Fixed all 10 and submitted two upstream PRs:
- safety-research/safety-tooling#155 (OpenAI init crash fix)
- safety-research/open-source-alignment-faking#2 (setup script + Dockerfile)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
openai v1+ raises OpenAIError at AsyncClient() construction time if
no api_key is provided and OPENAI_API_KEY is not in the environment.
InferenceAPI.__init__() eagerly constructs all provider clients, so
this crash hit even when only Anthropic or Together models were used.

Use os.environ.get("OPENAI_API_KEY", "not-configured") as a sentinel
in base.py, embedding.py, moderation.py, and s2s.py. The client now
initialises cleanly; actual OpenAI API calls will fail with HTTP 401
if no real key is set, which is a clear and actionable error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@lvjr3383 lvjr3383 force-pushed the fix/openai-client-init-without-api-key branch from e196a99 to 4077478 Compare February 27, 2026 22:44
lvjr3383 added a commit to lvjr3383/AI_Safety that referenced this pull request Feb 27, 2026
Audited safety-research/open-source-alignment-faking for reproducibility.
Found 10 friction points blocking any researcher from running the pipeline
out of the box, including 3 hard crashes before a single API call.

Fixed all 10 and submitted two upstream PRs:
- safety-research/safety-tooling#155 (OpenAI init crash fix)
- safety-research/open-source-alignment-faking#2 (setup script + Dockerfile)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant