Skip to content

Fix: Prevent crash when parsing empty or malformed JSON response#5905

Open
TeamInfinixdevcom wants to merge 2 commits intoFlowiseAI:mainfrom
TeamInfinixdevcom:fix-parse-text-error
Open

Fix: Prevent crash when parsing empty or malformed JSON response#5905
TeamInfinixdevcom wants to merge 2 commits intoFlowiseAI:mainfrom
TeamInfinixdevcom:fix-parse-text-error

Conversation

@TeamInfinixdevcom
Copy link

Fixes #336

This PR prevents the application from crashing when JSON.parse()
receives an empty or malformed string in chat message fields.

Changes:

  • Added safeJsonParse() helper
  • Replaced direct JSON.parse() calls with safe parsing
  • Preserves original value if parsing fails

This avoids the error:
SyntaxError: Unexpected end of JSON input

Copilot AI review requested due to automatic review settings March 4, 2026 21:15
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the application's resilience by implementing a robust JSON parsing mechanism. It addresses potential crashes caused by malformed or empty JSON data in chat message fields, ensuring a more stable user experience by safely processing incoming data and preserving its original form when parsing is unsuccessful.

Highlights

  • Robust JSON Parsing: Introduced a new safeJsonParse() helper function to gracefully handle empty or malformed JSON strings.
  • Crash Prevention: Replaced direct JSON.parse() calls with safeJsonParse() across several chat message fields to prevent application crashes.
  • Data Preservation: Ensured that if JSON parsing fails, the original string value is preserved instead of being replaced by null or throwing an error.
Changelog
  • packages/server/src/controllers/chat-messages/index.ts
    • Added a new safeJsonParse utility function to handle JSON parsing with error handling.
    • Replaced direct JSON.parse calls with safeJsonParse for sourceDocuments, usedTools, fileAnnotations, agentReasoning, fileUploads, action, and artifacts fields.
    • Implemented nullish coalescing (??) to ensure that if safeJsonParse returns null, the original string value of the field is retained.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a safeJsonParse helper to prevent crashes when parsing potentially empty or malformed JSON strings in chat message fields, which is a good improvement. My review includes a high-severity bug fix for the new helper to correctly handle parsing of JSON null values, and a medium-severity refactoring suggestion to improve the maintainability of the parsing logic by reducing code repetition.

Comment on lines +322 to +330
const safeJsonParse = (input: string) => {
if (!input || input.trim() === '') return null
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return null
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This safeJsonParse function has a subtle bug when parsing a valid JSON string that evaluates to null, such as 'null'. In this case, JSON.parse('null') correctly returns the value null. However, your function also returns null on parsing errors or for empty strings. At the call sites, you use safeJsonParse(...) ?? originalValue. When safeJsonParse returns null, this expression incorrectly falls back to originalValue.

To fix this, safeJsonParse should return undefined on failure. This allows distinguishing a failed parse from a successful parse that results in null.

  • safeJsonParse('null') would return null, and null ?? 'null' correctly evaluates to null.
  • safeJsonParse('invalid') would return undefined, and undefined ?? 'invalid' correctly evaluates to 'invalid'.
Suggested change
const safeJsonParse = (input: string) => {
if (!input || input.trim() === '') return null
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return null
}
}
const safeJsonParse = (input: string) => {
if (!input || input.trim() === '') return undefined
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return undefined
}
}

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses issue #336 by preventing crashes when chat message JSON-string fields contain empty/whitespace-only or malformed JSON, by introducing a safe parsing helper in the chat messages controller.

Changes:

  • Added a safeJsonParse() helper to guard JSON.parse() calls.
  • Updated chat message response parsing to use safe parsing and fall back to the original stored value on parse failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +323 to +328
if (!input || input.trim() === '') return null
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return null
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

safeJsonParse returns null on failure, and callers use ?? parsedResponse.<field>. This changes behavior for valid JSON that parses to null (e.g. the stored string 'null'): previously it would set the field to null, but now it falls back to the original string. Consider returning undefined (or a non-null sentinel) on parse failure/blank input so a successfully parsed null is preserved.

Suggested change
if (!input || input.trim() === '') return null
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return null
if (!input || input.trim() === '') return undefined
try {
return JSON.parse(input)
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return undefined

Copilot uses AI. Check for mistakes.
Comment on lines +326 to +328
} catch (error) {
console.warn('Unable to parse chat message JSON field', error)
return null
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The warning log in safeJsonParse doesn’t indicate which chat message field failed to parse (sourceDocuments vs usedTools vs artifacts, etc.), which will make production troubleshooting harder and may create noisy logs. Consider passing the field name into safeJsonParse (or logging it at the call site) and/or lowering the log level if malformed JSON is expected occasionally.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Failed to parse. Text

2 participants