fix: sanitize lone Unicode surrogates before sending to Claude API#1009
Open
vzaliva wants to merge 2 commits intoqwibitai:mainfrom
Open
fix: sanitize lone Unicode surrogates before sending to Claude API#1009vzaliva wants to merge 2 commits intoqwibitai:mainfrom
vzaliva wants to merge 2 commits intoqwibitai:mainfrom
Conversation
WhatsApp messages can contain emoji encoded as lone surrogate characters (invalid UTF-16 that is legal in JS strings but rejected by JSON). When the API request body is serialised, Anthropic returns: 400 "no low surrogate in string" Fix: call String.prototype.toWellFormed() in escapeXml(), which replaces any lone surrogate with U+FFFD before the content enters the prompt. Also bumps tsconfig lib to ES2024 so the method is recognised by tsc (Node 20+ supports it natively).
Author
|
Note for users hitting this error before applying the fix: If you've already received the Session files are stored in |
This was referenced Mar 13, 2026
The previous fix (escapeXml/toWellFormed) only covered incoming WhatsApp message text. Lone surrogates can also enter the session transcript via tool results (file reads, bash output, web fetches) and get replayed in subsequent API calls, triggering the same 400 error. Fix: intercept every outbound API request in the credential proxy and replace lone \uXXXX surrogate escapes with \uFFFD before forwarding. Valid surrogate pairs are preserved. This is the authoritative last line of defence regardless of surrogate source. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Change
.claude/skills/Description
WhatsApp messages can contain emoji encoded as lone surrogate characters — invalid UTF-16 that is legal in JS strings but rejected when serialised to JSON.
When the API request body is built, Anthropic returns:
400 "no low surrogate in string"
Fix: call
String.prototype.toWellFormed()inescapeXml(), which replaces any lone surrogate withU+FFFDbefore the content enters the prompt. Alsobumps
tsconfig.jsonlibtoES2024so the method is recognised bytsc(Node 20+ supports it natively, no runtime dependency change).For Skills
(not applicable)