Skip to content

fix(provider): adapt anthropic source to httpx2 rename in anthropic 1.0.0 - #9791

Merged
RC-CHN merged 1 commit into
AstrBotDevs:masterfrom
RC-CHN:fix/anthropic-httpx2-compat
Aug 24, 2026
Merged

fix(provider): adapt anthropic source to httpx2 rename in anthropic 1.0.0#9791
RC-CHN merged 1 commit into
AstrBotDevs:masterfrom
RC-CHN:fix/anthropic-httpx2-compat

Conversation

@RC-CHN

@RC-CHN RC-CHN commented Aug 24, 2026

Copy link
Copy Markdown
Member

anthropic 1.0.0 renamed the bundled _base_client.httpx module to _base_client.httpx2. The previous getattr(_base_client, "httpx", httpx) fallback silently reverted to the global httpx import, which can break the SDK's isinstance validation when a proxy client is used. Resolve the SDK's own module in version-agnostic order (httpx → httpx2 → global) and update the corresponding unit test to assert against the same resolution.

Modifications / 改动点

  • astrbot/core/provider/sources/anthropic_source.py

    • _create_http_client() previously resolved the SDK's bundled httpx via getattr(_base_client, "httpx", httpx). anthropic 1.0.0 renamed that attribute to _base_client.httpx2, so the old code silently fell back to the global httpx import and could fail the SDK's own isinstance validation when a proxy client is used.
    • Now resolves in version-agnostic order: _base_client.httpx_base_client.httpx2 → global httpx.
  • tests/test_anthropic_kimi_code_provider.py

    • test_create_http_client_uses_anthropic_httpx_module hardcoded anthropic_base_client.httpx, which raises AttributeError on anthropic 1.0.0 (Did you mean: 'httpx2'?). The assertion now derives the expected module using the same fallback chain as the production code, so it passes on both anthropic <1.0.0 and >=1.0.0.
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Support the Anthropic SDK’s httpx module rename while maintaining compatibility with older releases.

Bug Fixes:

  • Ensure Anthropic HTTP clients use the SDK’s bundled httpx module across anthropic versions, including the httpx2 rename in anthropic 1.0.0, preserving proxy-client validation.

Tests:

  • Make the Anthropic HTTP client test resolve the expected httpx module across supported anthropic versions.

….0.0

anthropic 1.0.0 renamed the bundled ``_base_client.httpx`` module to
``_base_client.httpx2``. The previous ``getattr(_base_client, "httpx",
httpx)`` fallback silently reverted to the global httpx import, which can
break the SDK's isinstance validation when a proxy client is used. Resolve
the SDK's own module in version-agnostic order (httpx → httpx2 → global)
and update the corresponding unit test to assert against the same
resolution.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels Aug 24, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/test_anthropic_kimi_code_provider.py" line_range="140-144" />
<code_context>
+    # anthropic <1.0.0 exposes the bundled httpx as ``_base_client.httpx``;
+    # 1.0.0+ renamed it to ``_base_client.httpx2``. Resolve the SDK's own module
+    # the same way the production code does so the assertion stays version-agnostic.
+    expected_httpx_module = getattr(
+        anthropic_base_client,
+        "httpx",
+        getattr(anthropic_base_client, "httpx2", None),
+    )
+
     assert captured["provider_label"] == "Anthropic"
</code_context>
<issue_to_address>
**issue (testing):** When `_base_client` imports successfully but exposes neither `httpx` nor `httpx2`, production resolves the global `httpx` module while the test expects `None`, so the assertion fails even though the implementation follows the documented fallback chain.

**Triggers:** When an installed Anthropic SDK lacks both bundled module attributes.

**Suggested fix:** Use `getattr(anthropic_base_client, "httpx2", httpx)` in the test fallback, matching production.

```suggestion
    expected_httpx_module = getattr(
        anthropic_base_client,
        "httpx",
        getattr(anthropic_base_client, "httpx2", httpx),
    )
```
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: tests/test_anthropic_kimi_code_provider.py:144


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +140 to +144
expected_httpx_module = getattr(
anthropic_base_client,
"httpx",
getattr(anthropic_base_client, "httpx2", None),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (testing): When _base_client imports successfully but exposes neither httpx nor httpx2, production resolves the global httpx module while the test expects None, so the assertion fails even though the implementation follows the documented fallback chain.

Triggers: When an installed Anthropic SDK lacks both bundled module attributes.

Suggested fix: Use getattr(anthropic_base_client, "httpx2", httpx) in the test fallback, matching production.

Suggested change
expected_httpx_module = getattr(
anthropic_base_client,
"httpx",
getattr(anthropic_base_client, "httpx2", None),
)
expected_httpx_module = getattr(
anthropic_base_client,
"httpx",
getattr(anthropic_base_client, "httpx2", httpx),
)

@RC-CHN
RC-CHN merged commit c87b773 into AstrBotDevs:master Aug 24, 2026
21 checks passed
BegoniaHe pushed a commit to Xero-Team/AstrBot that referenced this pull request Aug 24, 2026
….0.0 (AstrBotDevs#9791)

Upstream-Commit: c87b773
Upstream-Author: Ruochen Pan <badbatch0x01@gmail.com>
Upstream-PR: AstrBotDevs#9791
Sync-Disposition: adapt
Fork-Adaptation: Apply the version-agnostic httpx/httpx2 resolution on the current Anthropic source and the relocated unit test, keeping the fork proxy-client capture shape.
Tested: uv run pytest tests/unit/provider/test_anthropic_kimi_code_provider.py -q
BegoniaHe added a commit to Xero-Team/AstrBot that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant