From 00fcb2a6a7fe4545191355f3da61b0f44be9de42 Mon Sep 17 00:00:00 2001 From: Kymi808 Date: Mon, 1 Jun 2026 15:09:23 -0500 Subject: [PATCH] Accept auth_token in AnthropicFoundry.__init__ so .copy() doesn't crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `AnthropicFoundry.copy()` declares an `auth_token` parameter (line 204) and forwards it via `super().copy(auth_token=auth_token, ...)`. The parent `Anthropic.copy()` (`src/anthropic/_client.py:477-479`) then unconditionally passes `auth_token=auth_token or self.auth_token` into `self.__class__(...)`. But `AnthropicFoundry.__init__` did not accept an `auth_token` kwarg, so any call to `.copy()` on a Foundry client raised `TypeError: AnthropicFoundry.__init__() got an unexpected keyword argument 'auth_token'`. The same was true for `AsyncAnthropicFoundry`. Mirror the pattern already used in `AnthropicAWS`/`AsyncAnthropicAWS`: accept `auth_token` in the implementation `__init__` (passed through to the parent but not used for Foundry auth, since Foundry uses an API key or Azure AD token). The typing overloads are left as-is — `auth_token` is plumbing for `.copy()` compatibility, not a public construction option. Adds regression tests for the sync and async clients in tests/lib/test_azure.py. --- src/anthropic/lib/foundry.py | 10 ++++++++++ tests/lib/test_azure.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/anthropic/lib/foundry.py b/src/anthropic/lib/foundry.py index fde5be54f..3ce866eb5 100644 --- a/src/anthropic/lib/foundry.py +++ b/src/anthropic/lib/foundry.py @@ -126,6 +126,10 @@ def __init__( resource: str | None = None, api_key: str | None = None, azure_ad_token_provider: AzureADTokenProvider | None = None, + # Passed through to parent but not used for Foundry auth; accepted so + # the parent's `copy()` (which forwards `auth_token=self.auth_token`) + # doesn't raise TypeError when called on a Foundry client. + auth_token: str | None = None, webhook_key: str | None = None, base_url: str | None = None, timeout: float | Timeout | None | NotGiven = NOT_GIVEN, @@ -166,6 +170,7 @@ def __init__( super().__init__( api_key=api_key, + auth_token=auth_token, webhook_key=webhook_key, base_url=base_url, timeout=timeout, @@ -309,6 +314,10 @@ def __init__( resource: str | None = None, api_key: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, + # Passed through to parent but not used for Foundry auth; accepted so + # the parent's `copy()` (which forwards `auth_token=self.auth_token`) + # doesn't raise TypeError when called on a Foundry client. + auth_token: str | None = None, webhook_key: str | None = None, base_url: str | None = None, timeout: float | Timeout | None | NotGiven = NOT_GIVEN, @@ -349,6 +358,7 @@ def __init__( super().__init__( api_key=api_key, + auth_token=auth_token, webhook_key=webhook_key, base_url=base_url, timeout=timeout, diff --git a/tests/lib/test_azure.py b/tests/lib/test_azure.py index ec3ef7b1b..f95a07509 100644 --- a/tests/lib/test_azure.py +++ b/tests/lib/test_azure.py @@ -65,6 +65,20 @@ def test_missing_resource_error(self) -> None: api_key="test-key", ) + def test_copy_does_not_crash(self) -> None: + """Regression: ``copy()`` forwards ``auth_token`` via the parent client, + so ``AnthropicFoundry.__init__`` must accept it (passed through but + unused), mirroring ``AnthropicAWS``. Previously raised + ``TypeError: ... got an unexpected keyword argument 'auth_token'``. + """ + client = AnthropicFoundry(api_key="test-key", resource="example-resource") + copied = client.copy() + assert isinstance(copied, AnthropicFoundry) + assert copied.api_key == "test-key" + + copied_with_override = client.copy(timeout=30) + assert copied_with_override.timeout == 30 + class TestAsyncAnthropicFoundry: @pytest.mark.asyncio @@ -104,3 +118,17 @@ async def test_initialization_from_environment_variables(self, monkeypatch: pyte assert client.api_key == "env-key" assert "env-resource.services.ai.azure.com" in str(client.base_url) + + def test_copy_does_not_crash(self) -> None: + """Regression: ``copy()`` forwards ``auth_token`` via the parent client, + so ``AsyncAnthropicFoundry.__init__`` must accept it (passed through + but unused), mirroring ``AsyncAnthropicAWS``. Previously raised + ``TypeError: ... got an unexpected keyword argument 'auth_token'``. + """ + client = AsyncAnthropicFoundry(api_key="test-key", resource="example-resource") + copied = client.copy() + assert isinstance(copied, AsyncAnthropicFoundry) + assert copied.api_key == "test-key" + + copied_with_override = client.copy(timeout=30) + assert copied_with_override.timeout == 30