Skip to content

fix(anthropic): preserve pi-ai default betas when injecting anthropic-beta header#19789

Merged
Takhoffman merged 2 commits intoopenclaw:mainfrom
minupla:fix/oauth-beta-header-preservation
Feb 20, 2026
Merged

fix(anthropic): preserve pi-ai default betas when injecting anthropic-beta header#19789
Takhoffman merged 2 commits intoopenclaw:mainfrom
minupla:fix/oauth-beta-header-preservation

Conversation

@minupla
Copy link
Copy Markdown
Contributor

@minupla minupla commented Feb 18, 2026

Problem

After #19769 was reported, updating to v2026.2.17 causes all Anthropic API calls to fail with HTTP 401 when using an OAuth token (sk-ant-oat-*).

Root cause: When the context1m feature injects anthropic-beta via options.headers, pi-ai's mergeHeaders uses Object.assign (last-wins). This overwrites createClient's hardcoded defaultHeaders["anthropic-beta"], stripping required betas like oauth-2025-04-20. Without that beta, Anthropic rejects OAuth Bearer auth with a 401.

Fix

  • Detect token type by checking for the sk-ant-oat prefix
  • Pre-seed the appropriate pi-ai default betas (PI_AI_DEFAULT_ANTHROPIC_BETAS or PI_AI_OAUTH_ANTHROPIC_BETAS) before merging custom betas
  • Deduplicate with Set to avoid duplicate entries

Tests

Added a test case covering the OAuth token path with context1m enabled, verifying that oauth-2025-04-20 and claude-code-20250219 survive the header merge.

Fixes #19769

Greptile Summary

Fixes a critical regression where context1m feature injection of the anthropic-beta header via options.headers would overwrite pi-ai's hardcoded defaultHeaders["anthropic-beta"] (due to Object.assign last-wins in pi-ai's mergeHeaders), stripping required betas like oauth-2025-04-20 and causing HTTP 401 for OAuth token users.

  • Adds PI_AI_DEFAULT_ANTHROPIC_BETAS and PI_AI_OAUTH_ANTHROPIC_BETAS constants mirroring pi-ai's internal defaults, pre-seeded into the merged beta header to prevent overwrite
  • Detects OAuth tokens at runtime via options?.apiKey prefix matching to select the appropriate beta set
  • Deduplicates betas with Set to avoid redundant entries
  • Adds test coverage for the OAuth token path with context1m enabled
  • Note: The hardcoded beta constants duplicate pi-ai internals and will need updating if the library changes its defaults — this coupling is documented in code comments

Confidence Score: 4/5

  • This PR is a targeted, well-tested fix for a critical OAuth authentication regression and is safe to merge.
  • The fix correctly addresses the root cause (Object.assign last-wins overwriting critical betas), has good test coverage for both OAuth and standard API key paths, and the implementation is straightforward. The one concern is the hardcoded duplication of pi-ai's internal beta constants, which creates a maintenance coupling — but this is clearly documented and is the pragmatic solution given the upstream library's design.
  • src/agents/pi-embedded-runner/extra-params.ts — hardcoded beta constants will need updating if pi-ai changes its defaults

Last reviewed commit: 4b2db90

(2/5) Greptile learns from your feedback when you react with thumbs up/down!

Context used:

  • Context from dashboard - CLAUDE.md (source)
  • Context from dashboard - AGENTS.md (source)

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Feb 18, 2026
@minupla
Copy link
Copy Markdown
Contributor Author

minupla commented Feb 18, 2026

The protocol:check failure appears to be a pre-existing drift on main unrelated to this PR — it's flagging a missing threadid field in GatewayModels.swift that was added in a recent commit but hasn't been reflected in the generated Swift protocol models yet. Our changes only touch src/agents/pi-embedded-runner/extra-params.ts and the corresponding e2e test file. Happy to rebase once that's resolved upstream if needed.

@minupla minupla force-pushed the fix/oauth-beta-header-preservation branch from 4b2db90 to 6d196af Compare February 18, 2026 06:02
@openclaw-barnacle openclaw-barnacle Bot added the app: macos App: macos label Feb 18, 2026
minupla and others added 2 commits February 19, 2026 21:16
…-beta header

When injecting anthropic-beta via options.headers (e.g. for context1m),
pi-ai's mergeHeaders uses Object.assign (last-wins), which overwrites the
hardcoded defaultHeaders["anthropic-beta"] set by createClient. This
strips critical betas like oauth-2025-04-20 for OAuth tokens, causing
HTTP 401 errors.

Fix by detecting the token type (sk-ant-oat-* prefix for OAuth) and
pre-seeding the appropriate pi-ai default betas before merging, ensuring
they survive the Object.assign overwrite.

Fixes openclaw#19769
@Takhoffman Takhoffman force-pushed the fix/oauth-beta-header-preservation branch from c8d33c6 to 41dfb38 Compare February 20, 2026 03:22
@openclaw-barnacle openclaw-barnacle Bot removed the app: macos App: macos label Feb 20, 2026
@Takhoffman Takhoffman merged commit 10dab4f into openclaw:main Feb 20, 2026
13 checks passed
@Takhoffman
Copy link
Copy Markdown
Contributor

PR #19789 - fix(anthropic): preserve pi-ai default betas when injecting anthropic-beta header (#19789)

Merged via squash.

  • Merge commit: 10dab4f
  • Verified: pnpm build, pnpm check, pnpm test:macmini
  • Changes made:
    M CHANGELOG.md
    M src/agents/pi-embedded-runner-extraparams.e2e.test.ts
    M src/agents/pi-embedded-runner/extra-params.ts
  • Why these changes were made:
    Fix v2026.2.17: Anthropic auth fails with OAuth error #19769 by preserving pi-ai default Anthropic OAuth beta headers when context1m injects anthropic-beta, preventing OAuth 401 regressions.
  • Changelog: CHANGELOG.md updated=true required=true opt_out=false

Thanks @minupla!

anisoptera pushed a commit to anisoptera/openclaw that referenced this pull request Feb 20, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
@minupla minupla deleted the fix/oauth-beta-header-preservation branch February 20, 2026 11:21
vincentkoc pushed a commit that referenced this pull request Feb 21, 2026
…-beta header (#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
vincentkoc pushed a commit that referenced this pull request Feb 21, 2026
…-beta header (#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
mmyyfirstb pushed a commit to mmyyfirstb/openclaw that referenced this pull request Feb 21, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
zooqueen pushed a commit to hanzoai/bot that referenced this pull request Mar 6, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
hnykda added a commit to hnykda/openclaw that referenced this pull request Mar 9, 2026
The `createAnthropicBetaHeadersWrapper` was only applied when
`resolveAnthropicBetas` returned non-empty (i.e., when `context1m` or
`anthropicBeta` was set in agent extra params). When `context-1m` was
configured via model-level headers instead of extra params, the wrapper
was never applied, so the critical `oauth-2025-04-20` beta was never
injected for OAuth tokens (`sk-ant-oat-*`). This caused Anthropic to
reject all requests with HTTP 401 "OAuth authentication is currently
not supported."

Now the wrapper is always applied for Anthropic providers, ensuring
OAuth token detection and beta injection runs unconditionally.

Fixes openclaw#41444
Related: openclaw#19769, openclaw#19789

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Baelson added a commit to Baelson/openclaw that referenced this pull request Apr 2, 2026
…efactor

The extension refactor (59c23de) moved stream wrappers into the Anthropic
plugin but broke two assumptions from the original OAuth fix (openclaw#19789):

1. The beta wrapper was only applied when user-configured betas were present,
   so PI_AI_DEFAULT/OAUTH_ANTHROPIC_BETAS were never injected for users
   without explicit beta config (openclaw#41444).

2. options.apiKey is always undefined at the wrapper layer in production —
   the apiKey is resolved deeper in sdk.js — so isAnthropicOAuthApiKey()
   always returned false, selecting the wrong beta set (openclaw#57292).

Fix: always apply the beta wrapper for Anthropic, detect OAuth at setup time
from auth profile config, and pass the flag to the wrapper. The runtime
options.apiKey check is kept as a fallback for callers that still pass it.

Closes openclaw#57292
Closes openclaw#41444
Related openclaw#19789
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…-beta header (openclaw#19789) thanks @minupla

Verified:
- pnpm build
- pnpm check
- pnpm test:macmini

Co-authored-by: minupla <42547246+minupla@users.noreply.github.com>
Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2026.2.17: Anthropic auth fails with OAuth error

2 participants