fix(client): treat empty-string env credentials as unset#1673
Open
Zawwarsami16 wants to merge 2 commits into
Open
fix(client): treat empty-string env credentials as unset#1673Zawwarsami16 wants to merge 2 commits into
Zawwarsami16 wants to merge 2 commits into
Conversation
When ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN are present in the environment but set to an empty string, the SDK treated the empty string as a real credential (guards use `is None`). It then built an `Authorization: Bearer ` header (trailing space) and an empty `X-Api-Key`, which the HTTP layer (h11) rejects at write time, failing every request with a confusing APIConnectionError. Empty-string env vars are common in CI, containers, and wrapper shells (e.g. `export VAR=`, and the Claude Code CLI exports both empty). Coerce an empty env credential to None at read time so it is treated as absent and credential auto-discovery runs as expected.
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
Closes #1640.
When
ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKENare present in the environment but set to an empty string, the client treats the empty value as a real credential rather than as absent. It then builds anAuthorization: Bearerheader (literallyBearer+ trailing space) and an emptyX-Api-Key. The HTTP layer (h11, via httpx) validates header values at write time and rejects the trailing space, so every request fails:surfaced as
anthropic.APIConnectionError. Empty-string env vars are common in CI, containers, and wrapper shells (export VAR=), and the Claude Code CLI exports bothANTHROPIC_AUTH_TOKEN=andANTHROPIC_API_KEY=empty — so any script there that constructsAnthropic()without an explicit key fails on every call.Root cause
The env-credential guards use
is None, so an empty string slips through every check: the env read keeps"", the "missing auth" fallback (api_key is None and auth_token is None) never fires, and the header builders emit onis not None— producing{"X-Api-Key": "", "Authorization": "Bearer "}.Fix
Coerce an empty env credential to
Noneat read time in both the sync and async clients, so an empty value is treated as unset and credential auto-discovery runs as expected. Explicitly-passed credentials are untouched.Verification
Reproduced the
LocalProtocolErroronmainbefore the change; after the fixauth_headers == {}and auto-discovery runs. A real key still produces the correctX-Api-Keyheader.tests/test_client.py).pytest tests/test_client.py— 170 passed.ruff check/ruff format --checkclean; no new pyright errors on changed lines.