Skip to content

fix: treat empty string api_key same as None for Together/GraySwan clients#156

Open
lvjr3383 wants to merge 1 commit into
safety-research:mainfrom
lvjr3383:fix/empty-api-key-together-grayswan
Open

fix: treat empty string api_key same as None for Together/GraySwan clients#156
lvjr3383 wants to merge 1 commit into
safety-research:mainfrom
lvjr3383:fix/empty-api-key-together-grayswan

Conversation

@lvjr3383

Copy link
Copy Markdown

Problem

When TOGETHER_API_KEY or GRAYSWAN_API_KEY is present but blank in a .env file (e.g. TOGETHER_API_KEY=), python-dotenv loads it as an empty string "". The previous guard:

if api_key is not None:
    self.aclient = AsyncTogether(api_key=api_key)

passes for "", so AsyncTogether(api_key="") is called — which raises together.error.AuthenticationError at startup, even when Together AI is never used in the current run.

The same issue exists in gray_swan.py.

Fix

Change if api_key is not None:if api_key: in both files.

An empty string is now treated the same as None (key not provided), so the client is left as None and initialization succeeds. Any actual call to a Together/GraySwan model when the key is absent will still fail with a clear error at call time, not at startup.

Impact

  • Researchers with an Anthropic-only or OpenAI-only .env file no longer crash at pipeline startup due to blank TOGETHER_API_KEY= / GRAYSWAN_API_KEY= lines.
  • Consistent with how api_key is not None was originally intended to work.

Files changed

  • safetytooling/apis/inference/together.pyif api_key is not None:if api_key:
  • safetytooling/apis/inference/gray_swan.py — same

Found while independently reproducing the Alignment Faking paper (Greenblatt et al., 2024).

🤖 Generated with Claude Code

When TOGETHER_API_KEY or GRAYSWAN_API_KEY is present but blank in a
.env file (e.g. `TOGETHER_API_KEY=`), python-dotenv loads it as an
empty string "". The previous `if api_key is not None:` check passes
for "", causing AsyncTogether(api_key="") and AsyncGraySwan(api_key="")
to raise AuthenticationError at startup — even when those providers are
never used in the current run.

Switch to `if api_key:` (truthy check) so an empty string is treated
the same as None, matching the intended "key not provided" semantics.

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