Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/remove-sanitize-tool-call-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"workers-ai-provider": patch
---

Remove tool_call_id sanitization that truncated IDs to 9 alphanumeric chars, which caused all tool call IDs to collide after round-trip
33 changes: 0 additions & 33 deletions packages/workers-ai-provider/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,11 @@ import type { WorkersAIChatPrompt } from "./workersai-chat-prompt";
// Workers AI quirk workarounds
// ---------------------------------------------------------------------------

/**
* Strip non-alphanumeric characters and ensure the ID is exactly 9 chars,
* matching Workers AI binding's `[a-zA-Z0-9]{9}` validation pattern.
*
* The Workers AI binding validates `tool_call_id` with a strict regex, but
* generates IDs like `chatcmpl-tool-875d3ec6179676ae` (with dashes, >9 chars).
* Those IDs are rejected when sent back in a follow-up request.
*
* Once Workers AI fixes the validation, this becomes an idempotent no-op for
* IDs that already match the pattern.
*/
export function sanitizeToolCallId(id: string): string {
const alphanumeric = id.replace(/[^a-zA-Z0-9]/g, "");
return alphanumeric.slice(0, 9).padEnd(9, "0");
}

/**
* Normalize messages before passing to the Workers AI binding.
*
* The binding has strict schema validation that differs from the OpenAI API:
* - `content` must be a string (not null)
* - `tool_call_id` must match `[a-zA-Z0-9]{9}` pattern
*
* This patches fields so the full tool-call round-trip works even though
* the binding's own generated IDs may not pass its own validation.
*/
export function normalizeMessagesForBinding(messages: WorkersAIChatPrompt): WorkersAIChatPrompt {
return messages.map((msg) => {
Expand All @@ -41,19 +21,6 @@ export function normalizeMessagesForBinding(messages: WorkersAIChatPrompt): Work
(normalized as { content: string }).content = "";
}

// Normalize tool_call_id on tool messages
if ("tool_call_id" in normalized && typeof normalized.tool_call_id === "string") {
normalized.tool_call_id = sanitizeToolCallId(normalized.tool_call_id);
}

// Normalize tool_calls[].id on assistant messages
if ("tool_calls" in normalized && Array.isArray(normalized.tool_calls)) {
normalized.tool_calls = normalized.tool_calls.map((tc) => ({
...tc,
id: sanitizeToolCallId(tc.id),
}));
}

return normalized;
});
}
Expand Down
59 changes: 18 additions & 41 deletions packages/workers-ai-provider/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,11 @@ import {
processPartialToolCalls,
processToolCalls,
processText,
sanitizeToolCallId,
normalizeMessagesForBinding,
prepareToolsAndToolChoice,
createRun,
} from "../src/utils";

// ---------------------------------------------------------------------------
// sanitizeToolCallId
// ---------------------------------------------------------------------------

describe("sanitizeToolCallId", () => {
it("should strip non-alphanumeric characters and truncate to 9 chars", () => {
expect(sanitizeToolCallId("chatcmpl-tool-875d3ec6179676ae")).toBe("chatcmplt");
});

it("should pad short IDs with zeros", () => {
expect(sanitizeToolCallId("abc")).toBe("abc000000");
});

it("should pass through already-valid 9-char alphanumeric IDs", () => {
expect(sanitizeToolCallId("abcdef123")).toBe("abcdef123");
});

it("should handle empty string", () => {
expect(sanitizeToolCallId("")).toBe("000000000");
});

it("should handle IDs with only special characters", () => {
expect(sanitizeToolCallId("---!!!---")).toBe("000000000");
});

it("should handle mixed content", () => {
expect(sanitizeToolCallId("call_abc_123")).toBe("callabc12");
});
});

// ---------------------------------------------------------------------------
// normalizeMessagesForBinding
// ---------------------------------------------------------------------------
Expand All @@ -53,7 +22,18 @@ describe("normalizeMessagesForBinding", () => {
expect(result).toEqual(messages);
});

it("should sanitize tool_call_id on tool messages", () => {
it("should convert null content to empty string", () => {
const messages = [
{
role: "assistant" as const,
content: null as unknown as string,
},
];
const result = normalizeMessagesForBinding(messages);
expect(result[0]).toHaveProperty("content", "");
});

it("should pass through tool_call_id unchanged", () => {
const messages = [
{
role: "tool" as const,
Expand All @@ -63,10 +43,10 @@ describe("normalizeMessagesForBinding", () => {
},
];
const result = normalizeMessagesForBinding(messages);
expect(result[0]).toHaveProperty("tool_call_id", "chatcmplt");
expect(result[0]).toHaveProperty("tool_call_id", "chatcmpl-tool-875d3ec6179676ae");
});

it("should sanitize tool_calls[].id on assistant messages", () => {
it("should pass through tool_calls[].id unchanged", () => {
const messages = [
{
role: "assistant" as const,
Expand All @@ -84,21 +64,18 @@ describe("normalizeMessagesForBinding", () => {
const assistant = result[0] as {
tool_calls?: Array<{ id: string }>;
};
expect(assistant.tool_calls?.[0].id).toBe("chatcmplt");
expect(assistant.tool_calls?.[0].id).toBe("chatcmpl-tool-abc123def456");
});

it("should not mutate the original messages array", () => {
const original = [
{
role: "tool" as const,
name: "fn",
content: "result",
tool_call_id: "chatcmpl-tool-abc",
role: "assistant" as const,
content: null as unknown as string,
},
];
const originalId = original[0].tool_call_id;
normalizeMessagesForBinding(original);
expect(original[0].tool_call_id).toBe(originalId);
expect(original[0].content).toBeNull();
});
});

Expand Down
Loading