-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Fix: Prevent crash when parsing empty or malformed JSON response #5905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -319,34 +319,44 @@ const abortChatMessage = async (req: Request, res: Response, next: NextFunction) | |||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+326
to
+328
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+322
to
+330
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This To fix this,
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const parseAPIResponse = (apiResponse: ChatMessage | ChatMessage[]): ChatMessage | ChatMessage[] => { | ||||||||||||||||||||||||||||||||||||||
| const parseResponse = (response: ChatMessage): ChatMessage => { | ||||||||||||||||||||||||||||||||||||||
| const parsedResponse = { ...response } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.sourceDocuments) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.sourceDocuments = JSON.parse(parsedResponse.sourceDocuments) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.sourceDocuments = safeJsonParse(parsedResponse.sourceDocuments) ?? parsedResponse.sourceDocuments | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.usedTools) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.usedTools = JSON.parse(parsedResponse.usedTools) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.usedTools = safeJsonParse(parsedResponse.usedTools) ?? parsedResponse.usedTools | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.fileAnnotations) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.fileAnnotations = JSON.parse(parsedResponse.fileAnnotations) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.fileAnnotations = safeJsonParse(parsedResponse.fileAnnotations) ?? parsedResponse.fileAnnotations | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.agentReasoning) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.agentReasoning = JSON.parse(parsedResponse.agentReasoning) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.agentReasoning = safeJsonParse(parsedResponse.agentReasoning) ?? parsedResponse.agentReasoning | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.reasonContent) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.reasonContent = JSON.parse(parsedResponse.reasonContent) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.fileUploads) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.fileUploads = JSON.parse(parsedResponse.fileUploads) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.fileUploads = safeJsonParse(parsedResponse.fileUploads) ?? parsedResponse.fileUploads | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.action) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.action = JSON.parse(parsedResponse.action) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.action = safeJsonParse(parsedResponse.action) ?? parsedResponse.action | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| if (parsedResponse.artifacts) { | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.artifacts = JSON.parse(parsedResponse.artifacts) | ||||||||||||||||||||||||||||||||||||||
| parsedResponse.artifacts = safeJsonParse(parsedResponse.artifacts) ?? parsedResponse.artifacts | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||
| console.error('Error parsing chat message response', e) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
safeJsonParsereturnsnullon failure, and callers use?? parsedResponse.<field>. This changes behavior for valid JSON that parses tonull(e.g. the stored string'null'): previously it would set the field tonull, but now it falls back to the original string. Consider returningundefined(or a non-null sentinel) on parse failure/blank input so a successfully parsednullis preserved.