Skip to content

Commit 0381a99

Browse files
JJK801HenryHengZJ
andauthored
[Feature] Repair JSON from LLM in StructuredOutputParserAdvanced (#3723)
Repair JSON from LLM in StructuredOutputParserAdvanced Co-authored-by: Henry <hzj94@hotmail.com>
1 parent 94f67c0 commit 0381a99

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

packages/components/nodes/outputparsers/StructuredOutputParser/StructuredOutputParser.ts

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BaseOutputParser } from '@langchain/core/output_parsers'
33
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
44
import { CATEGORY } from '../OutputParserHelpers'
55
import { convertSchemaToZod, getBaseClasses, INode, INodeData, INodeParams } from '../../../src'
6+
import { jsonrepair } from 'jsonrepair'
67

78
class StructuredOutputParser implements INode {
89
label: string
@@ -74,6 +75,14 @@ class StructuredOutputParser implements INode {
7475
const zodSchema = z.object(convertSchemaToZod(jsonStructure)) as any
7576
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
7677

78+
const baseParse = structuredOutputParser.parse
79+
80+
// Fix broken JSON from LLM
81+
structuredOutputParser.parse = (text) => {
82+
const jsonString = text.includes('```') ? text.trim().split(/```(?:json)?/)[1] : text.trim()
83+
return baseParse.call(structuredOutputParser, jsonrepair(jsonString))
84+
}
85+
7786
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
7887
Object.defineProperty(structuredOutputParser, 'autoFix', {
7988
enumerable: true,

packages/components/nodes/outputparsers/StructuredOutputParserAdvanced/StructuredOutputParserAdvanced.ts

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BaseOutputParser } from '@langchain/core/output_parsers'
33
import { StructuredOutputParser as LangchainStructuredOutputParser } from 'langchain/output_parsers'
44
import { CATEGORY } from '../OutputParserHelpers'
55
import { z } from 'zod'
6+
import { jsonrepair } from 'jsonrepair'
67

78
class AdvancedStructuredOutputParser implements INode {
89
label: string
@@ -62,6 +63,14 @@ class AdvancedStructuredOutputParser implements INode {
6263
try {
6364
const structuredOutputParser = LangchainStructuredOutputParser.fromZodSchema(zodSchema)
6465

66+
const baseParse = structuredOutputParser.parse
67+
68+
// Fix broken JSON from LLM
69+
structuredOutputParser.parse = (text) => {
70+
const jsonString = text.includes('```') ? text.trim().split(/```(?:json)?/)[1] : text.trim()
71+
return baseParse.call(structuredOutputParser, jsonrepair(jsonString))
72+
}
73+
6574
// NOTE: When we change Flowise to return a json response, the following has to be changed to: JsonStructuredOutputParser
6675
Object.defineProperty(structuredOutputParser, 'autoFix', {
6776
enumerable: true,

packages/components/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"ioredis": "^5.3.2",
9393
"jsdom": "^22.1.0",
9494
"jsonpointer": "^5.0.1",
95+
"jsonrepair": "^3.11.1",
9596
"langchain": "^0.3.5",
9697
"langfuse": "3.3.4",
9798
"langfuse-langchain": "^3.3.4",

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)