Standard client: external bearer callbacks without OAuth beta coupling or placeholder credentials

未关闭
#2,044 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@naitik-ML-AI 已经在做这个了。

开始于 2026年11月15日。

  • #2066 来自 @naitik-ML-AI —— 未关闭

评估

难度
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_token is a static string. Azure Identity's get_bearer_token_provider returns a callable supplying a current string, so it cannot be passed directly.
  • The supported credentials= path requires an Anthropic AccessToken provider and automatically adds anthropic-beta: oauth-2025-04-20, even when the supplied token was issued for a different service.
  • expires_at=None means 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_refresh contract 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

  1. 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 existing AccessToken(..., expires_at=0) adapter without token wrapping. Leave ordinary token caching/renewal to the provider.
  2. Do not infer native OAuth/token-exchange semantics or add its beta header merely because a bearer token is supplied.
  3. Keep the advanced expiry-aware provider API, with a documented distinction between provider-owned caching, SDK-owned caching, and optional forced refresh.
  4. Permit explicit authentication-scheme/header selection when targeting another compatible endpoint, without a platform-specific client.
  5. Provide an explicit, public way to use custom HTTP authentication without triggering ambient credential discovery or needing a placeholder secret.
  6. 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

主要语言
Python
星标
4.1k
派生
915
PR 合并指标
30 天内没有已合并 PR

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

anthropics/anthropic-sdk-python 的其他 Issue

查看 anthropics/anthropic-sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。