Skip to content

fix(bedrock): take last 9 chars in normalizeToolCallId to preserve entropy#16406

Open
zhsj0089944 wants to merge 1 commit into
vercel:mainfrom
zhsj0089944:fix/normalize-tool-call-id-entropy
Open

fix(bedrock): take last 9 chars in normalizeToolCallId to preserve entropy#16406
zhsj0089944 wants to merge 1 commit into
vercel:mainfrom
zhsj0089944:fix/normalize-tool-call-id-entropy

Conversation

@zhsj0089944

Copy link
Copy Markdown

Problem

normalizeToolCallId() takes the first 9 alphanumeric characters, but since Bedrock IDs always start with tooluse_ (7 fixed chars), only 2 variable characters survive. This causes collisions when multiple tool calls are made in parallel.

Example with 8 parallel tool calls:

tooluseqr, tooluseY3, tooluseAc, tooluseP3, tooluseIx, tooluseV0, tooluse7r, tooluseAc

Note tooluseAc appears twice → Bedrock ValidationException: duplicate Ids.

Fix

Take the last 9 alphanumeric characters instead of the first 9, preserving more entropy from the random suffix:

- return alphanumericChars.slice(0, 9);
+ return alphanumericChars.slice(-9);

For tooluse_bpe71yCfRu2b5i-nKGDr5g:

  • Before: toolusebp (2 variable chars)
  • After: b5inKGDr5g (9 variable chars, ~62⁹ possible values)

Fixes #16182

…tropy

The previous implementation took the first 9 alphanumeric characters,
but since Bedrock IDs always start with 'tooluse' (7 fixed chars),
only 2 variable characters survived. This caused collisions when
multiple tool calls were made in parallel.

Take the last 9 characters instead, preserving more entropy from
the random suffix and reducing collision risk.

Fixes vercel#16182

it('should handle IDs with various special characters', () => {
expect(normalizeToolCallId('tool-use_123ABC456', true)).toBe('tooluse12');
expect(normalizeToolCallId('tool-use_123ABC456', true)).toBe('oluse123A');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(normalizeToolCallId('tool-use_123ABC456', true)).toBe('oluse123A');
expect(normalizeToolCallId('tool-use_123ABC456', true)).toBe('123ABC456');

Test assertion expects stale value 'oluse123A' instead of the actual slice(-9) output '123ABC456', causing the test suite to fail.

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] @ai-sdk/amazon-bedrock: normalizeToolCallId truncation causes duplicate tool-call IDs (Bedrock ValidationException) for Mistral models

1 participant