Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions strands-ts/src/models/__tests__/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('Model', () => {
)
})

it('preserves SyntaxError instead of overwriting with MaxTokensError when tool input JSON is malformed', async () => {
it('throws MaxTokensError when stopReason is maxTokens and contentBlockStop carries truncated tool_use JSON', async () => {
const provider = new TestModelProvider(async function* () {
yield { type: 'modelMessageStartEvent', role: 'assistant' }
yield {
Expand All @@ -332,14 +332,36 @@ describe('Model', () => {
}
yield {
type: 'modelContentBlockDeltaEvent',
delta: { type: 'toolUseInputDelta', input: '{invalid json' },
delta: { type: 'toolUseInputDelta', input: '{"location"' },
}
yield { type: 'modelContentBlockStopEvent' }
yield { type: 'modelMessageStopEvent', stopReason: 'maxTokens' }
})

const messages = [new Message({ role: 'user', content: [new TextBlock('Hi')] })]

await expect(async () => await collectGenerator(provider.streamAggregated(messages))).rejects.toThrow(
MaxTokensError
)
})

it('re-throws SyntaxError when tool_use JSON is malformed and stop reason is not maxTokens', async () => {
const provider = new TestModelProvider(async function* () {
yield { type: 'modelMessageStartEvent', role: 'assistant' }
yield {
type: 'modelContentBlockStartEvent',
start: { type: 'toolUseStart', toolUseId: 'tool1', name: 'get_weather' },
}
yield {
type: 'modelContentBlockDeltaEvent',
delta: { type: 'toolUseInputDelta', input: '{invalid json' },
}
yield { type: 'modelContentBlockStopEvent' }
yield { type: 'modelMessageStopEvent', stopReason: 'endTurn' }
})

const messages = [new Message({ role: 'user', content: [new TextBlock('Hi')] })]

try {
await collectGenerator(provider.streamAggregated(messages))
expect.fail('Expected error to be thrown')
Expand Down
15 changes: 14 additions & 1 deletion strands-ts/src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export abstract class Model<T extends BaseModelConfig = BaseModelConfig> {
let finalStopReason: StopReason | null = null
let metadata: ModelMetadataEvent | undefined = undefined
let redactionMessage: string | undefined = undefined
let pendingParseError: SyntaxError | null = null

for await (const event_data of this.stream(messages, options)) {
const event = this._convert_to_class_event(event_data)
Expand Down Expand Up @@ -426,7 +427,7 @@ export abstract class Model<T extends BaseModelConfig = BaseModelConfig> {
} catch (e: unknown) {
if (e instanceof SyntaxError) {
logger.error('unable to parse JSON string', e)
throw e
pendingParseError = e
}
}
break
Expand Down Expand Up @@ -474,6 +475,18 @@ export abstract class Model<T extends BaseModelConfig = BaseModelConfig> {
throw new ModelError('Stream ended without completing a message')
}

// If tool_use JSON failed to parse and the stream stopped due to maxTokens, the JSON was
// truncated by the token limit — surface MaxTokensError rather than a bare SyntaxError.
if (pendingParseError) {
if (finalStopReason === 'maxTokens') {
throw new MaxTokensError(
'Model reached maximum token limit (tool_use JSON was truncated). This is an unrecoverable state that requires intervention.',
stoppedMessage
)
}
throw pendingParseError
}

// Attach metadata after redaction so it applies to the final message.
const messageMetadata: MessageMetadata = {
...(metadata?.usage !== undefined && { usage: metadata.usage }),
Expand Down