mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-18 05:01:55 +00:00
Add Unicode surrogate sanitization for all providers
Fixes issue where unpaired Unicode surrogates in tool results cause JSON serialization errors in API providers, particularly Anthropic. - Add sanitizeSurrogates() utility function to remove unpaired surrogates - Apply sanitization in all provider convertMessages() functions: - User message text content (string and text blocks) - Assistant message text and thinking blocks - Tool result output - System prompts - Valid emoji (properly paired surrogates) are preserved - Add comprehensive test suite covering all 8 providers Previously only Google and Groq handled unpaired surrogates correctly. Now all providers (Anthropic, OpenAI Completions/Responses, Google, xAI, Groq, Cerebras, zAI) sanitize text before API submission.
This commit is contained in:
parent
949cd4efd8
commit
4e7a340460
6 changed files with 420 additions and 24 deletions
367
packages/ai/test/unicode-surrogate.test.ts
Normal file
367
packages/ai/test/unicode-surrogate.test.ts
Normal file
|
|
@ -0,0 +1,367 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { getModel } from "../src/models.js";
|
||||
import { complete } from "../src/stream.js";
|
||||
import type { Api, Context, Model, OptionsForApi, ToolResultMessage } from "../src/types.js";
|
||||
|
||||
/**
|
||||
* Test for Unicode surrogate pair handling in tool results.
|
||||
*
|
||||
* Issue: When tool results contain emoji or other characters outside the Basic Multilingual Plane,
|
||||
* they may be incorrectly serialized as unpaired surrogates, causing "no low surrogate in string"
|
||||
* errors when sent to the API provider.
|
||||
*
|
||||
* Example error from Anthropic:
|
||||
* "The request body is not valid JSON: no low surrogate in string: line 1 column 197667"
|
||||
*/
|
||||
|
||||
async function testEmojiInToolResults<TApi extends Api>(llm: Model<TApi>, options: OptionsForApi<TApi> = {}) {
|
||||
// Simulate a tool that returns emoji
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Use the test tool",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "test_1",
|
||||
name: "test_tool",
|
||||
arguments: {},
|
||||
},
|
||||
],
|
||||
api: llm.api,
|
||||
provider: llm.provider,
|
||||
model: llm.id,
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "toolUse",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: "test_tool",
|
||||
description: "A test tool",
|
||||
parameters: {} as any,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Add tool result with various problematic Unicode characters
|
||||
const toolResult: ToolResultMessage = {
|
||||
role: "toolResult",
|
||||
toolCallId: "test_1",
|
||||
toolName: "test_tool",
|
||||
output: `Test with emoji 🙈 and other characters:
|
||||
- Monkey emoji: 🙈
|
||||
- Thumbs up: 👍
|
||||
- Heart: ❤️
|
||||
- Thinking face: 🤔
|
||||
- Rocket: 🚀
|
||||
- Mixed text: Mario Zechner wann? Wo? Bin grad äußersr eventuninformiert 🙈
|
||||
- Japanese: こんにちは
|
||||
- Chinese: 你好
|
||||
- Mathematical symbols: ∑∫∂√
|
||||
- Special quotes: "curly" 'quotes'`,
|
||||
isError: false,
|
||||
};
|
||||
|
||||
context.messages.push(toolResult);
|
||||
|
||||
// Add follow-up user message
|
||||
context.messages.push({
|
||||
role: "user",
|
||||
content: "Summarize the tool result briefly.",
|
||||
});
|
||||
|
||||
// This should not throw a surrogate pair error
|
||||
const response = await complete(llm, context, options);
|
||||
|
||||
expect(response.stopReason).not.toBe("error");
|
||||
expect(response.errorMessage).toBeFalsy();
|
||||
expect(response.content.length).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
async function testRealWorldLinkedInData<TApi extends Api>(llm: Model<TApi>, options: OptionsForApi<TApi> = {}) {
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Use the linkedin tool to get comments",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "linkedin_1",
|
||||
name: "linkedin_skill",
|
||||
arguments: {},
|
||||
},
|
||||
],
|
||||
api: llm.api,
|
||||
provider: llm.provider,
|
||||
model: llm.id,
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "toolUse",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: "linkedin_skill",
|
||||
description: "Get LinkedIn comments",
|
||||
parameters: {} as any,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Real-world tool result from LinkedIn with emoji
|
||||
const toolResult: ToolResultMessage = {
|
||||
role: "toolResult",
|
||||
toolCallId: "linkedin_1",
|
||||
toolName: "linkedin_skill",
|
||||
output: `Post: Hab einen "Generative KI für Nicht-Techniker" Workshop gebaut.
|
||||
Unanswered Comments: 2
|
||||
|
||||
=> {
|
||||
"comments": [
|
||||
{
|
||||
"author": "Matthias Neumayer's graphic link",
|
||||
"text": "Leider nehmen das viel zu wenige Leute ernst"
|
||||
},
|
||||
{
|
||||
"author": "Matthias Neumayer's graphic link",
|
||||
"text": "Mario Zechner wann? Wo? Bin grad äußersr eventuninformiert 🙈"
|
||||
}
|
||||
]
|
||||
}`,
|
||||
isError: false,
|
||||
};
|
||||
|
||||
context.messages.push(toolResult);
|
||||
|
||||
context.messages.push({
|
||||
role: "user",
|
||||
content: "How many comments are there?",
|
||||
});
|
||||
|
||||
// This should not throw a surrogate pair error
|
||||
const response = await complete(llm, context, options);
|
||||
|
||||
expect(response.stopReason).not.toBe("error");
|
||||
expect(response.errorMessage).toBeFalsy();
|
||||
expect(response.content.some((b) => b.type === "text")).toBe(true);
|
||||
}
|
||||
|
||||
async function testUnpairedHighSurrogate<TApi extends Api>(llm: Model<TApi>, options: OptionsForApi<TApi> = {}) {
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Use the test tool",
|
||||
},
|
||||
{
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "test_2",
|
||||
name: "test_tool",
|
||||
arguments: {},
|
||||
},
|
||||
],
|
||||
api: llm.api,
|
||||
provider: llm.provider,
|
||||
model: llm.id,
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
},
|
||||
stopReason: "toolUse",
|
||||
},
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
name: "test_tool",
|
||||
description: "A test tool",
|
||||
parameters: {} as any,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// Construct a string with an intentionally unpaired high surrogate
|
||||
// This simulates what might happen if text processing corrupts emoji
|
||||
const unpairedSurrogate = String.fromCharCode(0xd83d); // High surrogate without low surrogate
|
||||
|
||||
const toolResult: ToolResultMessage = {
|
||||
role: "toolResult",
|
||||
toolCallId: "test_2",
|
||||
toolName: "test_tool",
|
||||
output: `Text with unpaired surrogate: ${unpairedSurrogate} <- should be sanitized`,
|
||||
isError: false,
|
||||
};
|
||||
|
||||
context.messages.push(toolResult);
|
||||
|
||||
context.messages.push({
|
||||
role: "user",
|
||||
content: "What did the tool return?",
|
||||
});
|
||||
|
||||
// This should not throw a surrogate pair error
|
||||
// The unpaired surrogate should be sanitized before sending to API
|
||||
const response = await complete(llm, context, options);
|
||||
|
||||
expect(response.stopReason).not.toBe("error");
|
||||
expect(response.errorMessage).toBeFalsy();
|
||||
expect(response.content.length).toBeGreaterThan(0);
|
||||
}
|
||||
|
||||
describe("AI Providers Unicode Surrogate Pair Tests", () => {
|
||||
describe.skipIf(!process.env.GEMINI_API_KEY)("Google Provider Unicode Handling", () => {
|
||||
const llm = getModel("google", "gemini-2.5-flash");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider Unicode Handling", () => {
|
||||
const llm = getModel("openai", "gpt-4o-mini");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider Unicode Handling", () => {
|
||||
const llm = getModel("openai", "gpt-5-mini");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider Unicode Handling", () => {
|
||||
const llm = getModel("anthropic", "claude-3-5-haiku-20241022");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.XAI_API_KEY)("xAI Provider Unicode Handling", () => {
|
||||
const llm = getModel("xai", "grok-3");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.GROQ_API_KEY)("Groq Provider Unicode Handling", () => {
|
||||
const llm = getModel("groq", "openai/gpt-oss-20b");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.CEREBRAS_API_KEY)("Cerebras Provider Unicode Handling", () => {
|
||||
const llm = getModel("cerebras", "gpt-oss-120b");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!process.env.ZAI_API_KEY)("zAI Provider Unicode Handling", () => {
|
||||
const llm = getModel("zai", "glm-4.5-air");
|
||||
|
||||
it("should handle emoji in tool results", async () => {
|
||||
await testEmojiInToolResults(llm);
|
||||
});
|
||||
|
||||
it("should handle real-world LinkedIn comment data with emoji", async () => {
|
||||
await testRealWorldLinkedInData(llm);
|
||||
});
|
||||
|
||||
it("should handle unpaired high surrogate (0xD83D) in tool results", async () => {
|
||||
await testUnpairedHighSurrogate(llm);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue