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

Aperta
#2,044 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@naitik-ML-AI ci sta già lavorando.

Dal 15/11/2026.

  • #2066 di @naitik-ML-AI — aperta

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
4.1k
Fork
915
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di anthropics/anthropic-sdk-python

Tutte le issue di anthropics/anthropic-sdk-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.