Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/anthropic/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,13 @@ def __init__(
or profile is not None
)
if not has_explicit_credential:
api_key = os.environ.get("ANTHROPIC_API_KEY")
auth_token = os.environ.get("ANTHROPIC_AUTH_TOKEN")
# Treat an empty-string env var as unset. Empty values are common in
# CI/containers/wrapper shells (e.g. `export VAR=`), and a "present"
# empty credential would otherwise build a malformed `Bearer ` /
# empty `X-Api-Key` header that the HTTP layer rejects on every
# request, instead of falling through to credential auto-discovery.
api_key = os.environ.get("ANTHROPIC_API_KEY") or None
auth_token = os.environ.get("ANTHROPIC_AUTH_TOKEN") or None
self.api_key = api_key
self.auth_token = auth_token
# --- end credentials support ---
Expand Down Expand Up @@ -627,8 +632,13 @@ def __init__(
or profile is not None
)
if not has_explicit_credential:
api_key = os.environ.get("ANTHROPIC_API_KEY")
auth_token = os.environ.get("ANTHROPIC_AUTH_TOKEN")
# Treat an empty-string env var as unset. Empty values are common in
# CI/containers/wrapper shells (e.g. `export VAR=`), and a "present"
# empty credential would otherwise build a malformed `Bearer ` /
# empty `X-Api-Key` header that the HTTP layer rejects on every
# request, instead of falling through to credential auto-discovery.
api_key = os.environ.get("ANTHROPIC_API_KEY") or None
auth_token = os.environ.get("ANTHROPIC_AUTH_TOKEN") or None
self.api_key = api_key
self.auth_token = auth_token
# --- end credentials support ---
Expand Down
18 changes: 18 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ def test_validate_headers(self) -> None:
request2 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"X-Api-Key": Omit()}))
assert request2.headers.get("X-Api-Key") is None

def test_empty_env_credentials_treated_as_unset(self) -> None:
with mock.patch("anthropic._client.default_credentials", return_value=None):
with update_env(ANTHROPIC_API_KEY="", ANTHROPIC_AUTH_TOKEN=""):
client = Anthropic(base_url=base_url, _strict_response_validation=True)

assert client.api_key is None
assert client.auth_token is None
assert client.auth_headers == {}

def test_default_query_option(self) -> None:
client = Anthropic(
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_query={"query_param": "bar"}
Expand Down Expand Up @@ -1464,6 +1473,15 @@ def test_validate_headers(self) -> None:
request2 = client2._build_request(FinalRequestOptions(method="get", url="/foo", headers={"X-Api-Key": Omit()}))
assert request2.headers.get("X-Api-Key") is None

def test_empty_env_credentials_treated_as_unset(self) -> None:
with mock.patch("anthropic._client.default_credentials", return_value=None):
with update_env(ANTHROPIC_API_KEY="", ANTHROPIC_AUTH_TOKEN=""):
client = AsyncAnthropic(base_url=base_url, _strict_response_validation=True)

assert client.api_key is None
assert client.auth_token is None
assert client.auth_headers == {}

async def test_default_query_option(self) -> None:
client = AsyncAnthropic(
base_url=base_url, api_key=api_key, _strict_response_validation=True, default_query={"query_param": "bar"}
Expand Down