fix: treat empty string api_key same as None for Together/GraySwan clients#156
Open
lvjr3383 wants to merge 1 commit into
Open
fix: treat empty string api_key same as None for Together/GraySwan clients#156lvjr3383 wants to merge 1 commit into
lvjr3383 wants to merge 1 commit into
Conversation
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>
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
When
TOGETHER_API_KEYorGRAYSWAN_API_KEYis present but blank in a.envfile (e.g.TOGETHER_API_KEY=),python-dotenvloads it as an empty string"". The previous guard:passes for
"", soAsyncTogether(api_key="")is called — which raisestogether.error.AuthenticationErrorat 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 asNoneand 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
.envfile no longer crash at pipeline startup due to blankTOGETHER_API_KEY=/GRAYSWAN_API_KEY=lines.api_key is not Nonewas originally intended to work.Files changed
safetytooling/apis/inference/together.py—if api_key is not None:→if api_key:safetytooling/apis/inference/gray_swan.py— sameFound while independently reproducing the Alignment Faking paper (Greenblatt et al., 2024).
🤖 Generated with Claude Code