fix: surface MaxTokensError when max_tokens truncates tool input JSON#2620
Merged
JackYPCOnline merged 1 commit intoJun 4, 2026
Conversation
When a model hits the max_tokens limit mid-stream, the tool input JSON can be truncated and fail to parse. Previously the SyntaxError from JSON.parse was thrown immediately, masking the more actionable MaxTokensError that the maxTokens stop reason should produce. Defer the tool input parse error and only surface it after checking the stop reason: MaxTokensError takes precedence when stopReason is maxTokens, otherwise the SyntaxError is attached as the cause of a ModelError. If the stream ends without a stop event, the SyntaxError is attached as the cause of the "Stream ended" ModelError. Ported from strands-agents/sdk-typescript#1065 by @cmbullock.
Contributor
|
Assessment: Comment Clean, well-scoped bug fix that correctly prioritizes Review Details
Solid fix — the inline comments are suggestions for hardening, not blockers. |
zastrowm
reviewed
Jun 3, 2026
zastrowm
reviewed
Jun 4, 2026
zastrowm
approved these changes
Jun 4, 2026
7 tasks
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.
Description
Port of strands-agents/sdk-typescript#1065 by @cmbullock.
Resolves : #2451
When a model hits the
max_tokenslimit mid-stream, the tool input JSON can be truncated and failJSON.parse. Previously the resultingSyntaxErrorwas thrown immediately fromstreamAggregated, masking the more actionableMaxTokensErrorthat themaxTokensstop reason is supposed to surface.The fix defers the tool input parse error instead of throwing it inline, then resolves precedence after the stop reason is known:
stopReason === 'maxTokens'→MaxTokensError(the real, actionable failure).ModelError('unable to parse tool input JSON')with theSyntaxErrorattached ascause.ModelError('Stream ended without completing a message')with theSyntaxErrorattached ascause.Type of Change
Bug fix
Testing
npm run test -w strands-ts— full unit suite passes (lint, format, and type-check included via the prepare checks).Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.