fix: treat empty-string ANTHROPIC_API_KEY/AUTH_TOKEN env vars as absent#1734
Open
Yadidiah-k wants to merge 1 commit into
Open
fix: treat empty-string ANTHROPIC_API_KEY/AUTH_TOKEN env vars as absent#1734Yadidiah-k wants to merge 1 commit into
Yadidiah-k wants to merge 1 commit into
Conversation
os.environ.get() returns "" when a var is exported but empty. That empty string passed through as self.api_key / self.auth_token, causing _bearer_auth to build "Authorization: Bearer " (trailing space), which h11 rejects with LocalProtocolError. This is common in CI environments and inside the Claude Code CLI shell where both vars are exported empty. Fixes anthropics#1640
paulpham157
pushed a commit
to paulpham157/anthropic-sdk-python
that referenced
this pull request
Jul 7, 2026
anthropics#1734) Mirrors the us/eu region branches from the sync AnthropicVertex into AsyncAnthropicVertex.__init__. Previously these regions fell through to the generic fallback and built {region}-aiplatform.googleapis.com, which returns 404 from the GCP URL router. Adds matching unit tests on TestAsyncAnthropicVertex to mirror the existing sync test pattern. Picked up from anthropics#1406 (Alex Toberoff).
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("ANTHROPIC_API_KEY")returns""when the variable is exported but set to an empty string. That empty string was stored asself.api_key/self.auth_tokenbecause the existing check only guarded againstNone. When building headers,_bearer_auththen producedAuthorization: Bearer(with a trailing space), which h11 rejects at write time withLocalProtocolError: Illegal header value b'Bearer '.This is especially common in:
ANTHROPIC_AUTH_TOKEN=andANTHROPIC_API_KEY=into child processesFix: apply
or Noneafteros.environ.get()so empty strings are treated as absent, consistent with how every other env-var credential system works.Both the
Anthropic(sync) andAsyncAnthropicinitializers had the same issue — both are fixed.Changes
src/anthropic/_client.py: two-line fix in the hand-written credentials section (lines marked# hand-written, upstream to Stainless) — both sync and async clientstests/test_client.py: regression tests forTestAnthropicandTestAsyncAnthropicasserting that empty-string env vars leaveapi_keyandauth_tokenasNoneCloses #1640