Standard client: external bearer callbacks without OAuth beta coupling or placeholder credentials
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
Read src/anthropic/_client.py and src/anthropic/lib/credentials/_auth.py and _types.py, then trace how bearer tokens, credentials, and OAuth beta headers are applied to sync and async requests. The issue lists the required behavior and transport-only reproduction; done means the standard clients support the requested auth configuration and the listed tests cover retries, streaming, cancellation, errors, and client copies.
由索引模型根据 Issue 内容生成。
描述
Summary
Please support externally issued, renewable bearer tokens directly on the standard Anthropic / AsyncAnthropic clients, without requiring a platform-specific client, a placeholder credential, or an Anthropic-native OAuth beta.
This is an ergonomics and authentication-policy request, not a claim that dynamic credentials are missing. credentials= already supports synchronous/asynchronous providers, expiry caching and refresh-on-401. A plain callable bearer getter remains a different, simpler integration.
Current behavior
Verified against the released v2.3.0 source (202db72c87d7c6eaa72129c67ce67fd343a3a20c):
auth_tokenis a static string. Azure Identity'sget_bearer_token_providerreturns a callable supplying a current string, so it cannot be passed directly.- The supported
credentials=path requires an AnthropicAccessTokenprovider and automatically addsanthropic-beta: oauth-2025-04-20, even when the supplied token was issued for a different service. expires_at=Nonemeans cache indefinitely.AccessToken(getter(), expires_at=0)is the existing, documented provider-owned-caching adapter: the getter is called again for every request. It works; the request here is to remove this token-wrapping bridge and its automatic native OAuth coupling.- The SDK's
force_refreshcontract cannot be assumed to map to identity libraries whose public getter has no force-refresh parameter. - A compatible gateway may support more than one resource-key header shape. This should be explicit configuration/documentation, not a reason to require a different client type.
Live tests against a freshly provisioned Foundry Haiku 5.5 deployment accepted both native bearer-key and documented x-api-key authentication, both 2023-06-01 and 2026-10-21, and the incidental OAuth beta header. This request is therefore not claiming that Foundry currently rejects the generic SDK's native defaults; it asks to remove unnecessary authentication adapters and clarify the compatibility contract.
Existing requests: https://github.com/anthropics/anthropic-sdk-python/issues/1496 and https://github.com/anthropics/anthropic-sdk-python/pull/1516 are closed; the PR was not merged. The newer async provider support should be preserved rather than reimplemented.
Minimal transport-only reproduction
No Azure resource, token or account is required:
import time
import httpx2 as httpx
from anthropic import AccessToken, Anthropic
headers = []
def respond(request):
headers.append(request.headers)
return httpx.Response(200, json={
"id": "msg_test", "type": "message", "role": "assistant", "model": "test",
"content": [{"type": "text", "text": "OK"}], "stop_reason": "end_turn",
"stop_sequence": None, "usage": {"input_tokens": 1, "output_tokens": 1},
})
with Anthropic(
base_url="https://example.invalid/anthropic",
credentials=lambda: AccessToken("test-token", expires_at=int(time.time()) + 3600),
http_client=httpx.Client(transport=httpx.MockTransport(respond)),
max_retries=0,
) as client:
client.messages.create(model="test", max_tokens=16,
messages=[{"role": "user", "content": "Hi"}])
assert "oauth-2025-04-20" in headers[0]["anthropic-beta"]
The observation is the SDK's unconditional OAuth opt-in. It does not assert that every other endpoint rejects that header.
Requested API and acceptance criteria
- A direct, explicitly selected string bearer getter, for example
auth_token: str | Callable[[], str], and an awaited async equivalent. This would offer the same per-request behavior as the existingAccessToken(..., expires_at=0)adapter without token wrapping. Leave ordinary token caching/renewal to the provider. - Do not infer native OAuth/token-exchange semantics or add its beta header merely because a bearer token is supplied.
- Keep the advanced expiry-aware provider API, with a documented distinction between provider-owned caching, SDK-owned caching, and optional forced refresh.
- Permit explicit authentication-scheme/header selection when targeting another compatible endpoint, without a platform-specific client.
- Provide an explicit, public way to use custom HTTP authentication without triggering ambient credential discovery or needing a placeholder secret.
- Tests for native/custom base URLs, explicit credential precedence over environment/profile state, token changes across retries, async cancellation, streaming, errors and client-copy behavior.
The goal is an unchanged standard client and Messages/streaming application code. Only endpoint/model/authentication configuration and, for Microsoft Entra ID, the identity library should vary.
Source references
- https://github.com/anthropics/anthropic-sdk-python/blob/202db72c87d7c6eaa72129c67ce67fd343a3a20c/src/anthropic/_client.py
- https://github.com/anthropics/anthropic-sdk-python/blob/202db72c87d7c6eaa72129c67ce67fd343a3a20c/src/anthropic/lib/credentials/_auth.py
- https://github.com/anthropics/anthropic-sdk-python/blob/202db72c87d7c6eaa72129c67ce67fd343a3a20c/src/anthropic/lib/credentials/_types.py
- 主要语言
- Python
- 星标
- 4.1k
- 派生
- 915
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
anthropics/anthropic-sdk-python 的其他 Issue
-
beta_tool validation errors reach the model as "Invalid arguments for function X" with no detail可能已有人在做 @mangeshraut712 于 32 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
anthropics/anthropic-sdk-python#2033 · 3 条评论 ·
-
`AnthropicBedrock` 2.x sends `anthropic_version: 2026-10-21`, which Bedrock rejects for Claude Sonnet 4.5, Opus 4.5 and Opus 4.6可能已有人在做 @charan-rathore 于 36 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 70/100
anthropics/anthropic-sdk-python#2031 · 1 条评论 ·
-
Three symlink tests in test_agent_toolset.py are missing the needs_symlinks marker可能已有人在做 @okxint 于 82 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
anthropics/anthropic-sdk-python#1915 · 3 条评论 ·
-
Messages.parse (and AsyncMessages.parse) don't take cache_control?可能已有人在做 @AlphaFDE 于 86 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
anthropics/anthropic-sdk-python#1907 · 2 条评论 ·
-
Windows: os.fchmod in credential write-back bricks the OAuth refresh token可能已有人在做 @OllieinCanada 于 98 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
anthropics/anthropic-sdk-python#1868 · 1 条评论 ·
查看 anthropics/anthropic-sdk-python 的全部 Issue
相似的 Issue
-
bug harness
难度 2/5 1-3 小时 新手友好度 72/100
mruangutai/harness#2687 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
modal-labs/modal-client#4158 ·
-
good first issue
难度 1/5 1 小时以内 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 半天 新手友好度 72/100
The-Swarm-Corporation/swarms-rs#168 ·
维护者通常 1 天内回复
-
[BUG] SMTP listener now binds to EMAIL_PORT instead of 2525 since the gunicorn switch 5.0.3-ls373未关闭
难度 2/5 1-3 小时 新手友好度 72/100
linuxserver/docker-healthchecks#137 · 1 条评论 ·