feat(types): widen SdkBeta to accept arbitrary anthropic-beta values#877
Open
abhicris wants to merge 1 commit intoanthropics:mainfrom
Open
feat(types): widen SdkBeta to accept arbitrary anthropic-beta values#877abhicris wants to merge 1 commit intoanthropics:mainfrom
abhicris wants to merge 1 commit intoanthropics:mainfrom
Conversation
Closes anthropics#845. `SdkBeta` was `Literal["context-1m-2025-08-07"]`, so any other beta flag was rejected by type checkers even though the runtime path (`subprocess_cli.py: cmd.extend(["--betas", ",".join(self._options.betas)])`) was already happy to forward arbitrary strings. That blocked callers from opting into header-only betas like `token-efficient-tools-2025-02-19`, which on tool-heavy agent loops materially reduces output tokens. Widen the alias to `Literal["context-1m-2025-08-07"] | str` — known values still autocomplete in editors, anything else passes through. This is purely additive: no existing usage breaks, no runtime behavior changes, and the `Literal` arm keeps documenting the value the SDK explicitly supports today. Update the `betas` docstring to reflect the relaxed contract and to point out `token-efficient-tools-2025-02-19` as a representative example. Add two `_build_command` regression tests in `tests/test_transport.py`: - `test_build_command_forwards_known_beta` — guards the existing literal value end-to-end. - `test_build_command_forwards_arbitrary_beta` — passes a known + unknown beta side-by-side and asserts they reach `--betas` in order, joined as the CLI expects. `+51 / -2`. `ruff check` and `ruff format --check` clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #845.
What
SdkBetais currentlyLiteral["context-1m-2025-08-07"], so type checkers reject any other beta flag — even though the runtime path insubprocess_cli.py:is already happy to forward arbitrary strings. That blocks callers from opting into header-only betas like
token-efficient-tools-2025-02-19, which on tool-heavy agent loops materially reduces output tokens (~14% on the workloads the issue cites).Why this approach
The issue (#845) raises two options. This PR takes option 1 (loosen the literal) because it is purely additive — no existing usage breaks, no runtime behavior changes — and the
Literalarm keeps documenting the value the SDK explicitly supports today. Option 2 (extra_headers) is a larger surface change and worth keeping for a follow-up if maintainers want a separate channel for non-anthropic-betaheaders.Diff shape
src/claude_agent_sdk/types.py:SdkBeta = Literal[\"context-1m-2025-08-07\"] | strplus a couple of comment lines explaining the intent. Updated thebetasfield docstring onClaudeAgentOptionsto spell out that arbitrary header values are accepted, withtoken-efficient-tools-2025-02-19as the canonical example.tests/test_transport.py: two new_build_commandtests:test_build_command_forwards_known_beta— guards the existing literal value end-to-end.test_build_command_forwards_arbitrary_beta— passes a known + unknown beta side-by-side and asserts they reach--betasin order, joined as the CLI expects.+51 / -2. The widened alias compiles to the same widerstrat runtime, so type checkers will seebetas: list[str]. TheLiteralmember remains useful for IDE autocomplete on the value the SDK officially documents.Test plan
ruff checkandruff format --checkclean on the touched files.pytest tests/test_transport.py tests/test_types.py— 130 passed (the 2 unrelated failures both also fail onmainfor me, they need atriobackend not relevant to this change).betas=[\"context-1m-2025-08-07\", \"token-efficient-tools-2025-02-19\", \"some-future-beta-2099-12-31\"]throughClaudeAgentOptionsconstructor — runtime accepts; new test asserts the comma-joined value reaches--betasexactly.Notes for reviewers
\"context-1m-2025-08-07\"highlighted.list[SdkBeta]continues to admit the literal.extra_headersfor non-anthropic-betaheaders) could be pursued separately if there's a need.