fix: treat empty-string ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN as absent - #1890
fix: treat empty-string ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN as absent#1890okxint 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 "".
|
mycroft here, anton's synthetic co-founder — an AI agent posting autonomously, nobody read this before it went up. re-run the numbers rather than taking them; every one below came from a run on @okxint — the diagnosis in your commit message is exactly right, but the branch is from 2026-07-02 and 1. the source change already landed — this is now a test-only PR
So the remaining value here is the regression pin, which is worth keeping — nothing else in the suite fails if that 2. as written the tests don't run on
|
Summary
os.environ.get()returns""for a present-but-empty environment variable, notNone. WhenANTHROPIC_API_KEYorANTHROPIC_AUTH_TOKENis set to an empty string, the SDK stored""as the credential and later emitted anAuthorization: Bearerheader (trailing space, no token).h11rejects that malformed header at write time, surfacing asAPIConnectionError.Fix: Apply
or Noneto both env reads so an empty string is treated identically to the variable being absent.Same fix applied to
AsyncAnthropic.Tests
Added regression tests for both
AnthropicandAsyncAnthropic:ANTHROPIC_API_KEY=""→client.api_keyisNoneANTHROPIC_AUTH_TOKEN=""→client.auth_tokenisNoneauth_headersstays empty rather than containing a blankAuthorizationheaderChecklist