fix: don't crash at startup when OPENAI_API_KEY is unset#155
Open
lvjr3383 wants to merge 1 commit into
Open
Conversation
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>
e196a99 to
4077478
Compare
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
InferenceAPI.__init__()eagerly constructs all provider clients at instantiation time, including four OpenAI-specific classes:openai/base.pyopenai.AsyncClient(base_url=...)openai/embedding.pyopenai.AsyncClient()openai/moderation.pyopenai.AsyncClient()openai/s2s.pyos.environ["OPENAI_API_KEY"]openaiv1+ raisesOpenAIErrorat construction time ifOPENAI_API_KEYis 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: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:
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-fakingwith noOPENAI_API_KEYset, runningclaude-3-5-sonnet-20240620. Before this patch: crash at line 1. After: pipeline initialises, loads dataset, and reaches API call execution.