fix: treat empty-string ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN as absent#1726
Open
okxint wants to merge 1 commit into
Open
fix: treat empty-string ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN as absent#1726okxint wants to merge 1 commit into
okxint wants to merge 1 commit into
Conversation
…bsent os.environ.get() returns "" for a present-but-empty var, not None. When the SDK stored an empty string as auth_token, _bearer_auth emitted an "Authorization: Bearer " header (trailing space, no token). h11 rejects that value at write time with LocalProtocolError, surfaced to callers as APIConnectionError. Apply `or None` to both env reads so an empty string is treated the same as the variable being unset. Adds regression tests for both Anthropic and AsyncAnthropic confirming that api_key, auth_token, and auth_headers are all None / empty when the env vars are set to "".
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.
Summary
os.environ.get()returns""for an env var that is present but empty — notNone. When eitherANTHROPIC_API_KEYorANTHROPIC_AUTH_TOKENwas set to an empty string (common in CI, containers, and in the Claude Code shell which exports both as empty), the SDK stored""as the credential.For
ANTHROPIC_AUTH_TOKEN,_bearer_auththen built{"Authorization": "Bearer "}— a header with a trailing space and no token. h11 validates header values at write time and rejectsb'Bearer ':surfaced as
anthropic.APIConnectionErroron every request.Fix
Apply
or Noneto both env reads inAnthropic.__init__andAsyncAnthropic.__init__:This coerces empty strings to
Noneso the SDK treats a present-but-empty var the same as unset.Tests
Added
test_empty_string_env_credentials_treated_as_absentto bothTestAnthropicandTestAsyncAnthropic, verifying thatapi_key,auth_token, andauth_headersare allNone/{}when the env vars are set to"".Closes #1640