fix(gen-ai): Gracefully handle malformed JSON in AI message parsing#109645
Merged
fix(gen-ai): Gracefully handle malformed JSON in AI message parsing#109645
Conversation
static/app/views/performance/newTraceDetails/traceDrawer/details/utils.tsx
Outdated
Show resolved
Hide resolved
static/app/views/performance/newTraceDetails/traceDrawer/details/utils.tsx
Show resolved
Hide resolved
obostjancic
approved these changes
Mar 3, 2026
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| // will have been parsed successfully above. | ||
| if (containsFilteredPlaceholder(value)) { | ||
| return {parsed: null, fixedInvalidJson: true}; | ||
| } |
Contributor
There was a problem hiding this comment.
Filtered check skips recoverable JSON repair
Medium Severity
parseJsonWithFix now returns early whenever the raw string contains [Filtered] after a parse failure, so it never attempts fixJson. This can drop recoverable payloads where parsing failed for truncation or minor corruption unrelated to the placeholder, causing callers to lose structured message data and fall back to raw/empty output.
obostjancic
approved these changes
Mar 4, 2026
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.


This PR addresses the issue of
SyntaxErrorexceptions occurring when parsing AI messages, specifically in thetransformPartsMessagesfunction within the AI Agents insight UI.The root causes identified were:
[Filtered], which is not valid JSON.\p) that causeJSON.parseto fail.Changes Made:
parseJsonWithFix(utils.tsx): Modified to gracefully handle the above failure modes. It now explicitly checks for[Filtered]content and returnsnullforparseddata, and also wraps thefixJsonfallback in an additionaltry/catchblock to prevent unrepairable malformed JSON from throwing. In both cases, it returns{parsed: null, fixedInvalidJson: true}.transformPartsMessages(aiInput.tsx): Exported this function for improved testability.utils.spec.tsx: Updated existing tests forparseJsonWithFixto accurately reflect the new graceful handling of[Filtered]content and bad escape sequences.aiInput.spec.tsx(new file): Added a comprehensive test suite fortransformPartsMessages, covering valid JSON transformations, passthrough of non-parts messages, and specific test cases for the[Filtered]placeholder and malformed escape sequences to ensure the fixes work as expected.These changes prevent the UI from crashing due to unparseable AI message data, providing a more robust user experience.
Closes TET-2021