Repository navigation
fix(translation): report Anthropic context-window stops as a token limit - #918
Open
bharadwaj-pendyala wants to merge 4 commits into
Open
bharadwaj-pendyala wants to merge 4 commits into
bharadwaj-pendyala wants to merge 4 commits into
Conversation
Signed-off-by: Bharadwaj Pendyala <bharadwajpendyala@gmail.com>
Contributor
Contributor
Author
|
@ayushag-nv could you approve the CI run here? |
Signed-off-by: Bharadwaj Pendyala <bharadwajpendyala@gmail.com>
Contributor
Author
|
@dagardner-nv CI never ran here: the Oct 6 runs expired at action_required, so I merged today's main (c365efe) to get fresh ones, and those need a workflow approval too. After #1220 and #1224 the patch is unchanged, the translation tests pass, and both context-window tests still fail without the fix. |
…-window-stop Signed-off-by: Bharadwaj Pendyala <bharadwajpendyala@gmail.com> # Conflicts: # crates/switchyard-translation/tests/response_translation.rs
Signed-off-by: Bharadwaj Pendyala <bharadwajpendyala@gmail.com>
This branch has not been deployed
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.
What
Treat Anthropic's
model_context_window_exceededstop reason as a token limit, likemax_tokens. This updatesmap_anthropic_stop_reasonfor buffered responses and the Anthropic stream decoder'smessage_deltahandling.Why
Sonnet 4.5 and newer return
model_context_window_exceededwithout a beta header when the answer runs into the context window. Anthropic's docs say to treat it as truncated. Switchyard doesn't know the spelling, so a cut-off answer reaches other clients like this onb9e7ccce:finish_reason: "stop"status: "completed"finish_reason: "model_context_window_exceeded"response.completedIn the streamed Chat row, the raw Anthropic string isn't one of the values the OpenAI spec allows for
finish_reason. The other three rows report a truncated answer as complete.max_tokenson the same inputs giveslengthandincompleteeverywhere, as set up in #300.The stream decoder change also covers streams buffered by the server.
ResponseAccumulatorpasses the chunk reason tostop_reason_from_strinprotocol/src/stream.rs, which returnsUnknownfor this spelling today. Emittingmax_tokensinstead producesMaxTokenswithout changingprotocol. The Responses decoder already mapsresponse.incompletetomax_tokens.Notes for reviewers
max_tokenswhere it used to sayend_turn.max_tokensingen_ai.response.finish_reasonsfor a context-window stop. The decoder rewrites the string before the chunk reachesobserve_chunk, so those traces no longer tell a context-window cutoff apart from an output limit.pause_turn, which also passes through raw to Chat stream clients. It means "send the turn back to continue", and there's no Chat or Responses equivalent, so it needs its own decision.stop_reason_from_strfor Bedrock. This PR doesn't touch that file, so the two don't conflict.Validation
Two regression tests, one buffered and one streamed, fail on
b9e7ccceand pass on this branch. The streamed test also folds the decoded chunk throughResponseAccumulatorand checks forStopReason::MaxTokens.cargo fmt --all --check: cleancargo clippy --workspace --all-targets --exclude switchyard-py -- -D warnings: cleancargo test --workspace --exclude switchyard-py: 913 passed, 0 failedI didn't build
switchyard-pylocally. Nothing in it changes here, and CI runs it.Summary by CodeRabbit