Skip to content

fix: surface MaxTokensError when max_tokens truncates tool input JSON#2620

Merged
JackYPCOnline merged 1 commit into
strands-agents:mainfrom
JackYPCOnline:port-maxtokens-tool-input-json-fix
Jun 4, 2026
Merged

fix: surface MaxTokensError when max_tokens truncates tool input JSON#2620
JackYPCOnline merged 1 commit into
strands-agents:mainfrom
JackYPCOnline:port-maxtokens-tool-input-json-fix

Conversation

@JackYPCOnline

@JackYPCOnline JackYPCOnline commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Description

Port of strands-agents/sdk-typescript#1065 by @cmbullock.
Resolves : #2451

When a model hits the max_tokens limit mid-stream, the tool input JSON can be truncated and fail JSON.parse. Previously the resulting SyntaxError was thrown immediately from streamAggregated, masking the more actionable MaxTokensError that the maxTokens stop 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).
  • any other stop reason → ModelError('unable to parse tool input JSON') with the SyntaxError attached as cause.
  • stream ends without a stop event → ModelError('Stream ended without completing a message') with the SyntaxError attached as cause.

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

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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.
Comment thread strands-ts/src/models/model.ts
Comment thread strands-ts/src/models/model.ts
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Assessment: Comment

Clean, well-scoped bug fix that correctly prioritizes MaxTokensError over tool input parse errors. The implementation logic is sound and tests cover all three precedence branches.

Review Details
  • Correctness: The error deferral and precedence logic (maxTokens → parse error → stream-ended) is correctly implemented and matches the TypeScript SDK port.
  • Defensive coding: Two minor pre-existing issues around error handling in the catch block are worth addressing in a follow-up (see inline comments) — neither is blocking.
  • Testing: Good coverage of the three branches. Tests appropriately use try/catch for multi-property error inspection.

Solid fix — the inline comments are suggestions for hardening, not blockers.

Comment thread strands-ts/src/models/model.ts
@JackYPCOnline JackYPCOnline requested a review from zastrowm June 3, 2026 21:28
Comment thread strands-ts/src/models/__tests__/model.test.ts
@JackYPCOnline JackYPCOnline requested a review from zastrowm June 4, 2026 16:06
@JackYPCOnline JackYPCOnline enabled auto-merge (squash) June 4, 2026 16:59
@JackYPCOnline JackYPCOnline merged commit a2f1425 into strands-agents:main Jun 4, 2026
38 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] SyntaxError instead of MaxTokensError when tool_use JSON is truncated

2 participants