Skip to content

fix(gen-ai): Gracefully handle malformed JSON in AI message parsing#109645

Merged
shellmayr merged 8 commits intomasterfrom
seer/fix/gen-ai-json-parsing
Mar 4, 2026
Merged

fix(gen-ai): Gracefully handle malformed JSON in AI message parsing#109645
shellmayr merged 8 commits intomasterfrom
seer/fix/gen-ai-json-parsing

Conversation

@sentry
Copy link
Contributor

@sentry sentry bot commented Mar 2, 2026

This PR addresses the issue of SyntaxError exceptions occurring when parsing AI messages, specifically in the transformPartsMessages function within the AI Agents insight UI.

The root causes identified were:

  1. PII-filtered content: Relay replaces sensitive data with the literal string [Filtered], which is not valid JSON.
  2. Malformed JSON escape sequences: Some AI SDKs or user-provided content can contain invalid escape sequences (e.g., \p) that cause JSON.parse to fail.

Changes Made:

  • parseJsonWithFix (utils.tsx): Modified to gracefully handle the above failure modes. It now explicitly checks for [Filtered] content and returns null for parsed data, and also wraps the fixJson fallback in an additional try/catch block 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 for parseJsonWithFix to accurately reflect the new graceful handling of [Filtered] content and bad escape sequences.
  • aiInput.spec.tsx (new file): Added a comprehensive test suite for transformPartsMessages, 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

@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 2, 2026
@linear
Copy link

linear bot commented Mar 2, 2026

@shellmayr shellmayr added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 2, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 3, 2026
@shellmayr shellmayr marked this pull request as ready for review March 3, 2026 08:48
@shellmayr shellmayr requested a review from a team as a code owner March 3, 2026 08:48
@shellmayr shellmayr requested a review from obostjancic March 3, 2026 08:49
@shellmayr shellmayr added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 3, 2026
@shellmayr shellmayr requested a review from obostjancic March 4, 2026 08:06
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 4, 2026
@shellmayr shellmayr added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Mar 4, 2026
Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

@shellmayr shellmayr merged commit 3d7c9ed into master Mar 4, 2026
75 of 76 checks passed
@shellmayr shellmayr deleted the seer/fix/gen-ai-json-parsing branch March 4, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants