-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(langchain): Fix structured parser with triple backticks, adds tes…
…ts (#7199) Co-authored-by: Harris <[email protected]>
- Loading branch information
1 parent
7124f18
commit 986ab14
Showing
2 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { expect, test } from "@jest/globals"; | ||
import { z } from "zod"; | ||
import { StructuredOutputParser } from "../structured.js"; | ||
|
||
test("StructuredOutputParser handles valid JSON wrapped in triple backticks", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
const text = '```json\n{"name": "John Doe", "age": 30}```'; | ||
|
||
const result = await parser.parse(text); | ||
|
||
expect(result).toHaveProperty("name", "John Doe"); | ||
expect(result).toHaveProperty("age", 30); | ||
}); | ||
|
||
test("StructuredOutputParser handles JSON without triple backticks", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
const text = '{"name": "John Doe", "age": 30}'; | ||
|
||
const result = await parser.parse(text); | ||
|
||
expect(result).toHaveProperty("name", "John Doe"); | ||
expect(result).toHaveProperty("age", 30); | ||
}); | ||
|
||
test("StructuredOutputParser throws error for invalid JSON wrapped in triple backticks", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
// Invalid JSON | ||
const text = '```json\n{"name": "John Doe", "age": }```'; | ||
|
||
await expect(parser.parse(text)).rejects.toThrow("Failed to parse"); | ||
}); | ||
|
||
test("StructuredOutputParser throws error for normal text without triple backticks", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
// Invalid JSON | ||
const text = "This is just a plain text without JSON."; | ||
|
||
await expect(parser.parse(text)).rejects.toThrow("Failed to parse"); | ||
}); | ||
|
||
test("StructuredOutputParser handles JSON with backticks inside text but not at the start", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
const text = 'Some random text ```json\n{"name": "John Doe", "age": 30}```'; | ||
const result = await parser.parse(text); | ||
|
||
expect(result).toHaveProperty("name", "John Doe"); | ||
expect(result).toHaveProperty("age", 30); | ||
}); | ||
|
||
test("StructuredOutputParser handles JSON with backticks inside JSON", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
const text = '{"name": "John ```Doe```", "age": 30}'; | ||
|
||
const result = await parser.parse(text); | ||
|
||
expect(result).toHaveProperty("name", "John ```Doe```"); | ||
expect(result).toHaveProperty("age", 30); | ||
}); | ||
|
||
test("StructuredOutputParser throws error for JSON with backticks both inside and outside the JSON", async () => { | ||
const parser = StructuredOutputParser.fromZodSchema( | ||
z.object({ | ||
name: z.string().describe("Human name"), | ||
age: z.number().describe("Human age"), | ||
}) | ||
); | ||
const text = | ||
'Some random text ```json\n{"name": "John ```Doe```", "age": 30}```'; | ||
|
||
await expect(parser.parse(text)).rejects.toThrow("Failed to parse"); | ||
}); |