Skip to content

fix(translator): translate Bedrock errors for Anthropic count_tokens - #2683

Open
akdok wants to merge 1 commit into
theagentrouter:mainfrom
akdok:fix-counttokens-awsanthropic-error
Open

akdok wants to merge 1 commit into
theagentrouter:mainfrom
akdok:fix-counttokens-awsanthropic-error

Conversation

@akdok

@akdok akdok commented Sep 14, 2026

Copy link
Copy Markdown

Description

The AWSAnthropic translator for /v1/messages/count_tokens had a no-op ResponseError, so when Bedrock rejects a request (for example a ValidationException because the model does not support CountTokens) the client received Bedrock's raw error body instead of an Anthropic-style error envelope.

This PR translates Bedrock error responses into anthropicschema.ErrorResponse, mirroring what the Bedrock messages translator already does:

  • JSON bodies are decoded as awsbedrock.BedrockException and the message is preserved.
  • Non-JSON bodies are passed through as the error message.
  • The HTTP status is mapped to the Anthropic error type (invalid_request_error, rate_limit_error, ...). The existing status-to-type mapping is promoted from a method on the messages translator to a package-level helper so both translators share it.
  • Content-Type and Content-Length headers are set for the rewritten body.

Example: a 400 from Bedrock with body {"message": "The provided model doesn't support counting tokens"} now becomes

{"type":"error","error":{"type":"invalid_request_error","message":"The provided model doesn't support counting tokens"},"request_id":""}

Fixes #2546

Notes

  • make precommit and go test ./internal/translator/... pass locally.
  • Per the contributing guidelines' transparency policy: this change was written with AI assistance; I understand and take full ownership of the code.

The AWSAnthropic count_tokens translator's ResponseError was a no-op, so
Bedrock error bodies (e.g. a ValidationException when a model does not
support CountTokens) were passed through to clients as-is instead of as
an Anthropic error envelope. This mirrors the existing error handling in
the Bedrock messages translator and reuses its status-to-error-type
mapping by promoting it to a package-level helper.

Signed-off-by: Kamil Dokumaci <dok@tuta.com>
@akdok
akdok requested a review from a team as a code owner September 14, 2026 02:07
@netlify

netlify Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for theagentrouter canceled.

Name Link
🔨 Latest commit 9e5767f
🔍 Latest deploy log https://app.netlify.com/projects/theagentrouter/deploys/6aa756fcbc6cda0008e6783f

@missBerg missBerg added bug Something isn't working area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) labels Sep 16, 2026
@codecov

codecov Bot commented Sep 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/translator/counttokens_awsanthropic.go 96.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AWSAnthropic count_tokens translator does not translate Bedrock errors to Anthropic error format

2 participants