Python: Add base_url parameter to AnthropicClient and RawAnthropicClient#5685
Conversation
…hropicClient Add base_url support to AnthropicSettings TypedDict, RawAnthropicClient, and AnthropicClient so users can point the client at Foundry or other Anthropic-compatible endpoints without having to construct AsyncAnthropic manually. - Add base_url field to AnthropicSettings (resolved from ANTHROPIC_BASE_URL env var) - Add base_url parameter to RawAnthropicClient.__init__ and pass it to AsyncAnthropic - Add base_url parameter to AnthropicClient.__init__ and forward to super - Add unit tests for base_url on both client classes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 97% | Result: All clear
Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach
Automated review by automated agents
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
Adds configurable base_url support to the Python Anthropic clients so callers can target custom Anthropic-compatible endpoints (e.g., Azure AI Foundry) without changing client code, aligning behavior with the upstream Anthropic SDK and the existing Foundry-specific wrapper.
Changes:
- Add
base_url: str | NonetoAnthropicSettings, and plumb it throughRawAnthropicClient/AnthropicClientintoAsyncAnthropic(base_url=...). - Add unit tests verifying
base_urlpassed as an explicit constructor kwarg is applied to the underlying SDK client. - Minor formatting-only updates in skills-related core tests and error messages.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| python/packages/anthropic/agent_framework_anthropic/_chat_client.py | Adds base_url to settings + client constructors and passes it into AsyncAnthropic. |
| python/packages/anthropic/tests/test_anthropic_client.py | Adds tests for explicit base_url passthrough on both public and raw clients. |
| python/packages/core/agent_framework/_skills.py | Reflows a few exception messages (formatting-only). |
| python/packages/core/tests/core/test_skills.py | Formatting-only changes to test call sites/spacing. |
| python/packages/core/agent_framework/init.py | Minor __all__ ordering adjustment. |
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 92%
✓ Correctness
The two new tests correctly exercise the ANTHROPIC_BASE_URL environment variable path for both AnthropicClient and RawAnthropicClient. The fixture machinery (override_env_param_dict → anthropic_unit_test_env) properly sets the env var via monkeypatch and returns it in the dict. The client's load_settings call uses env_prefix='ANTHROPIC_', so ANTHROPIC_BASE_URL maps to base_url in AnthropicSettings, which is then passed to AsyncAnthropic. No correctness issues found.
✓ Security Reliability
The diff adds two new unit tests verifying that AnthropicClient and RawAnthropicClient read ANTHROPIC_BASE_URL from the environment when no explicit base_url argument is provided. Both tests use the existing anthropic_unit_test_env fixture, which safely scopes env-var overides via monkeypatch. The conftest.py shows override_env_param_dict is merged into the env dict before monkeypatching, so the URL will be present. No injection risks, resource leaks, or reliability issues are present.
✓ Test Coverage
The two new tests correctly exercise the
ANTHROPIC_BASE_URLenv-var fallback path for bothAnthropicClientandRawAnthropicClient. The fixture wiring is correct:override_env_param_dictwithindirect=Trueis handled byconftest.py(returnsrequest.param),anthropic_unit_test_envmerges it into the base dict viaenv_vars.update(...)and callsmonkeypatch.setenvfor each key includingANTHROPIC_BASE_URL, so both the env var and the dict key are properly set. The assertionanthropic_unit_test_env["ANTHROPIC_BASE_URL"]is valid because the key is present in the returned dict after the update. Production code in_chat_client.pypassesbase_url=Noneintoload_settingswithenv_prefix="ANTHROPIC_", which mapsANTHROPIC_BASE_URL→base_urlcorrectly. One missing scenario: there is no test confirming that an explicitly-passedbase_urltakes precedence over a simultaneously setANTHROPIC_BASE_URLenv variable, which would close the priority-ordering contract.
✓ Design Approach
I did not find any design-approach issues in this diff. The added tests exercise the documented
ANTHROPIC_BASE_URLfallback on both the raw and wrapped clients, and that behavior matches the existingload_settings(..., base_url=base_url)flow inagent_framework_anthropic/_chat_client.pywhere explicit arguments win and environment values fill in omitted settings.
Suggestions
- Consider adding a test that sets
ANTHROPIC_BASE_URLin the environment and passes an explicitbase_urlkwarg, asserting the explicit value wins. This closes the priority-ordering contract (explicit arg > env var) that the existing tests leave implicit.
Automated review by automated agents
… var (microsoft#5683) Add regression tests asserting that when both ANTHROPIC_BASE_URL is set in the environment *and* an explicit base_url kwarg is passed to AnthropicClient / RawAnthropicClient, the explicit kwarg wins. This closes the priority-ordering contract (explicit arg > env var) that the existing tests left implicit. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
moonbox3
left a comment
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 95% | Result: All clear
Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach
Automated review by automated agents
Motivation and Context
Foundry users need to point
AnthropicClientat a custom endpoint (e.g., Azure AI Foundry) just as the Anthropic SDK itself supports viabase_url. Without this parameter, switching between Anthropic-hosted and Foundry-hosted deployments requires different client code, which is developer-unfriendly.Fixes #5683
Description
AnthropicSettingsTypedDict,RawAnthropicClient.__init__, andAnthropicClient.__init__all lacked abase_urlfield. The fix addsbase_url: str | NonetoAnthropicSettingsand as a keyword argument to both client constructors, then threads it throughload_settingsand into theAsyncAnthropic(base_url=...)call, matching the pattern already used byAnthropicFoundryClient. The value also falls back to theANTHROPIC_BASE_URLenvironment variable via the existing settings loader.Contribution Checklist