fix: cover plain streaming json parse failures

This commit is contained in:
Mason Huang
2026-04-29 17:15:43 +08:00
parent 7addcf4e79
commit 36689751c3
3 changed files with 19 additions and 1 deletions

View File

@@ -483,6 +483,14 @@ describe("sanitizeUserFacingText — streaming JSON parse error (#59076)", () =>
expect(result).toBe("LLM streaming response contained a malformed fragment. Please try again.");
});
it.each([
"Unexpected end of JSON input",
"Unexpected non-whitespace character after JSON at position 4",
])("rewrites plain JSON.parse error variants in error context: %s", (text) => {
const result = sanitizeUserFacingText(text, { errorContext: true });
expect(result).toBe("LLM streaming response contained a malformed fragment. Please try again.");
});
it("does not rewrite JSON parse error when not in error context", () => {
// When not in error context, the text could be legitimate assistant content
// mentioning JSON errors. Don't rewrite.

View File

@@ -19,6 +19,16 @@ describe("formatAssistantErrorText streaming JSON parse classification", () => {
);
});
it.each([
"Unexpected end of JSON input",
"Unexpected non-whitespace character after JSON at position 4",
])("suppresses plain JSON.parse streaming fragment failures: %s", (errorMessage) => {
const msg = makeAssistantError(errorMessage);
expect(formatAssistantErrorText(msg)).toBe(
"LLM streaming response contained a malformed fragment. Please try again.",
);
});
it("suppresses structured Anthropic tool-call delta parse failures", () => {
const msg = makeAssistantError(
'Could not parse Anthropic SSE event content_block_delta: Unexpected end of JSON input; data={"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"{\\"path\\":"},"index":0}',

View File

@@ -223,7 +223,7 @@ export function isStreamingJsonParseError(raw: string): boolean {
return true;
}
return /^(?:Expected (?:',' or '\}' after property value|double-quoted property name|':' after property name|',' or '\]' after array element)|Unterminated string) in JSON at position \d+(?: \(line \d+ column \d+\))?$/i.test(
return /^(?:Unexpected end of JSON input|Unexpected non-whitespace character after JSON at position \d+(?: \(line \d+ column \d+\))?|(?:Expected (?:',' or '\}' after property value|double-quoted property name|':' after property name|',' or '\]' after array element)|Unterminated string) in JSON at position \d+(?: \(line \d+ column \d+\))?)$/i.test(
trimmed,
);
}