.agents/skills/sentry-javascript-bugs/references/ai-insights-parsing.md
AI Insights parsing errors account for 2 issues and 3,005 events, but this cluster is escalating as new AI model formats are introduced. The core problem is that JSON.parse is called on AI prompt message data (ai.prompt.messages span attribute) that contains invalid JSON — bad escape characters, non-standard serialization, or plain text where JSON is expected.
Sentry: https://sentry.io/issues/7002181641/ Events: 2,681 | Users: 95
Stacktrace:
./app/views/performance/newTraceDetails/traceDrawer/details/span/eapSections/aiInput.tsx
parseAIMessages
-> JSON.parse(messages)
Underlying: SyntaxError: Bad escaped character in JSON at position 430
Root cause: The parseAIMessages function calls JSON.parse on the ai.prompt.messages span attribute. Various LLM providers serialize messages differently and some produce strings with raw newlines, unescaped backslashes, or invalid unicode sequences that are not valid JSON. The function does not catch the parsing error.
Fix pattern:
function parseAIMessages(raw: string): AIMessage[] {
if (!raw) return [];
try {
return JSON.parse(raw);
} catch {
return [{role: 'unknown', content: raw}];
}
}
Sentry: https://sentry.io/issues/7270138341/ Events: 324 | Users: 17
Stacktrace:
./app/views/performance/newTraceDetails/traceDrawer/details/span/eapSections/aiInput.tsx
transformPartsMessages
-> JSON.parse(message)
Underlying: SyntaxError: JSON.parse: unexpected character at line 1 column 2
Root cause: The "parts" format handler encounters message data that is not valid JSON. Some AI models serialize multi-modal content (text + images) in non-standard formats. The parser assumes all parts-format messages are JSON but some are plain text or use custom serialization.
Fix pattern:
function transformPartsMessages(raw: string): TransformedMessage {
if (!raw) return {type: 'text', content: ''};
if (!raw.startsWith('[') && !raw.startsWith('{')) {
return {type: 'text', content: raw};
}
try {
return JSON.parse(raw);
} catch {
return {type: 'text', content: raw};
}
}
JSON.parse called on AI/LLM span data without try-catch?[ or { before attempting JSON parse?