fix: add missing count_tokens to Bedrock beta.messages#1233
Open
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Open
fix: add missing count_tokens to Bedrock beta.messages#1233MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
Conversation
Add the count_tokens method alias to the Bedrock beta Messages and AsyncMessages classes, matching the pattern used in Vertex beta.messages. Also update the _prepare_options guard to catch the beta count_tokens URL (/v1/messages/count_tokens?beta=true) in addition to the non-beta URL, so users get a clear "Token counting is not supported in Bedrock yet" error instead of an unhandled request. Fixes anthropics#1103
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.
Summary
Calling
client.beta.messages.count_tokens()on anAnthropicBedrockclient raisesAttributeErrorbecause thecount_tokensmethod alias is missing from the Bedrock betaMessages/AsyncMessagesclasses.This PR:
Adds
count_tokensaliases to the Bedrock betaMessagesandAsyncMessagesclasses in_beta_messages.py, matching the pattern already used in the Vertex beta.messages.Fixes the
_prepare_optionsguard in_client.pyto also catch the beta count_tokens URL (/v1/messages/count_tokens?beta=true). Previously, only the non-beta URL (/v1/messages/count_tokens) was blocked, so a betacount_tokenscall would bypass the guard and send an unsupported request to the Bedrock API.After this fix,
client.beta.messages.count_tokens()raises the existing clear error message —AnthropicError("Token counting is not supported in Bedrock yet")— instead of anAttributeError.Before
After
Related
Files changed
src/anthropic/lib/bedrock/_beta_messages.py— addedcount_tokensaliasessrc/anthropic/lib/bedrock/_client.py— extended URL guard to cover beta variant