This repository was archived by the owner on Jun 3, 2026. It is now read-only.
fix: throw MaxTokensError when contentBlockStop carries truncated tool_use JSON#1100
Open
serhiizghama wants to merge 2 commits into
Open
Conversation
When the model hits its token limit mid-tool_use, the stream emits contentBlockStop before messageStop. The JSON parse fails with SyntaxError on the truncated input, but the stop reason is maxTokens. Defer the SyntaxError instead of throwing immediately, let the stream complete, then raise MaxTokensError if stopReason === maxTokens — or re-raise the original SyntaxError otherwise. Closes #1061
Collaborator
|
This repository has been merged into the strands-agents/harness-sdk monorepo and will be archived shortly. All new development happens there. If this PR is still relevant, please recreate it against the monorepo. The code now lives under Apologies for the disruption, and thank you for contributing! |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
When a Bedrock model hits its token limit mid-tool-call, the stream emits
contentBlockStopbeforemessageStop. At that pointJSON.parsefails on the incomplete input, throwing aSyntaxError— before the subsequentmessageStop { stopReason: 'maxTokens' }is processed. TheSyntaxErrorpropagates to the caller as a genericModelError, hiding theMaxTokensErrorthat callers need to recover from:Reported in issue strands-agents/harness-sdk#2451.
Solution
In
streamAggregated, defer theSyntaxErrorinstead of throwing it immediately:pendingParseErrorand let the loop continue to consume the remaining stream events.pendingParseErroris set:finalStopReason === 'maxTokens'→ throwMaxTokensError(truncation by token limit).SyntaxError(genuinely malformed JSON from the model).Testing
MaxTokensErrorwhencontentBlockStoparrives with truncated JSON andstopReason === 'maxTokens'.maxTokenspath: malformed JSON withstopReason === 'endTurn'still surfacesModelError(cause: SyntaxError).