Skip to content

Python: Add base_url parameter to AnthropicClient and RawAnthropicClient#5685

Merged
TaoChenOSU merged 4 commits into
microsoft:mainfrom
moonbox3:agent/fix-5683-1
May 7, 2026
Merged

Python: Add base_url parameter to AnthropicClient and RawAnthropicClient#5685
TaoChenOSU merged 4 commits into
microsoft:mainfrom
moonbox3:agent/fix-5683-1

Conversation

@moonbox3
Copy link
Copy Markdown
Contributor

@moonbox3 moonbox3 commented May 7, 2026

Motivation and Context

Foundry users need to point AnthropicClient at a custom endpoint (e.g., Azure AI Foundry) just as the Anthropic SDK itself supports via base_url. Without this parameter, switching between Anthropic-hosted and Foundry-hosted deployments requires different client code, which is developer-unfriendly.

Fixes #5683

Description

AnthropicSettings TypedDict, RawAnthropicClient.__init__, and AnthropicClient.__init__ all lacked a base_url field. The fix adds base_url: str | None to AnthropicSettings and as a keyword argument to both client constructors, then threads it through load_settings and into the AsyncAnthropic(base_url=...) call, matching the pattern already used by AnthropicFoundryClient. The value also falls back to the ANTHROPIC_BASE_URL environment variable via the existing settings loader.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Note: PR autogenerated by an agent

Copilot and others added 2 commits May 7, 2026 03:45
…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>
Copilot AI review requested due to automatic review settings May 7, 2026 03:54
@moonbox3 moonbox3 added the python label May 7, 2026
Copy link
Copy Markdown
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Reviewers: 4 | Confidence: 97% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by automated agents

@moonbox3
Copy link
Copy Markdown
Contributor Author

moonbox3 commented May 7, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py4423592%461, 464, 545, 638, 640, 783, 810–811, 889, 891, 921–922, 967, 983–984, 991–993, 997–999, 1003–1006, 1120, 1130, 1182, 1330–1331, 1348, 1361, 1374, 1399–1400
packages/core/agent_framework
   _skills.py5921098%522, 532, 1808–1809, 1882, 1887, 1936, 1941, 2144–2145
TOTAL33586390888% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
6558 30 💤 0 ❌ 0 🔥 1m 46s ⏱️

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Reviewers: 4 | Confidence: 90% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by moonbox3's agents

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | None to AnthropicSettings, and plumb it through RawAnthropicClient/AnthropicClient into AsyncAnthropic(base_url=...).
  • Add unit tests verifying base_url passed 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.

Comment thread python/packages/anthropic/tests/test_anthropic_client.py


Add unit tests verifying that both AnthropicClient and RawAnthropicClient
pick up base_url from the ANTHROPIC_BASE_URL environment variable via
load_settings when base_url is not passed explicitly as a constructor arg.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_URL env-var fallback path for both AnthropicClient and RawAnthropicClient. The fixture wiring is correct: override_env_param_dict with indirect=True is handled by conftest.py (returns request.param), anthropic_unit_test_env merges it into the base dict via env_vars.update(...) and calls monkeypatch.setenv for each key including ANTHROPIC_BASE_URL, so both the env var and the dict key are properly set. The assertion anthropic_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.py passes base_url=None into load_settings with env_prefix="ANTHROPIC_", which maps ANTHROPIC_BASE_URLbase_url correctly. One missing scenario: there is no test confirming that an explicitly-passed base_url takes precedence over a simultaneously set ANTHROPIC_BASE_URL env 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_URL fallback on both the raw and wrapped clients, and that behavior matches the existing load_settings(..., base_url=base_url) flow in agent_framework_anthropic/_chat_client.py where explicit arguments win and environment values fill in omitted settings.

Suggestions

  • Consider adding a test that sets ANTHROPIC_BASE_URL in the environment and passes an explicit base_url kwarg, 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>
Copy link
Copy Markdown
Contributor Author

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Reviewers: 4 | Confidence: 95% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by automated agents

@TaoChenOSU TaoChenOSU added this pull request to the merge queue May 7, 2026
Merged via the queue into microsoft:main with commit 8bb4692 May 7, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Add base_url parameter to base AnthropicClient

5 participants