Skip to content

Commit 47f594f

Browse files
authored
fix: special tokens should not break task processing (#7540)
1 parent c7d7ad8 commit 47f594f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/utils/__tests__/tiktoken.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ describe("tiktoken", () => {
2525
expect(result).toBe(0)
2626
})
2727

28+
it("should not throw on text content with special tokens", async () => {
29+
const content: Anthropic.Messages.ContentBlockParam[] = [
30+
{ type: "text", text: "something<|endoftext|>something" },
31+
]
32+
33+
const result = await tiktoken(content)
34+
expect(result).toBeGreaterThan(0)
35+
})
36+
2837
it("should handle missing text content", async () => {
2938
// Using 'as any' to bypass TypeScript's type checking for this test case
3039
// since we're specifically testing how the function handles undefined text

src/utils/tiktoken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function tiktoken(content: Anthropic.Messages.ContentBlockParam[]):
2424
const text = block.text || ""
2525

2626
if (text.length > 0) {
27-
const tokens = encoder.encode(text)
27+
const tokens = encoder.encode(text, undefined, [])
2828
totalTokens += tokens.length
2929
}
3030
} else if (block.type === "image") {

0 commit comments

Comments
 (0)