Skip to content

feat(core): allow JsonOutputParser to stream markdown JSON #8091

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

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 77 additions & 7 deletions langchain-core/src/output_parsers/tests/json.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { test, expect } from "@jest/globals";
import { describe, test, expect } from "@jest/globals";
import { ChatPromptTemplate } from "../../prompts/chat.js";
import { RunnableSequence } from "../../runnables/base.js";
import { RunnablePassthrough } from "../../runnables/passthrough.js";
import { FakeStreamingLLM } from "../../utils/testing/index.js";
import { JsonOutputParser } from "../json.js";

async function acc(iter: AsyncGenerator<object>): Promise<object[]> {
const acc = [];
for await (const chunk of iter) {
acc.push(chunk);
}
return acc;
}

async function* streamChunks(chunks: string[]): AsyncGenerator<string> {
for (const chunk of chunks) {
yield chunk;
await new Promise<void>((resolve) => {
void setTimeout(() => {
resolve();
}, 0);
});
}
}

const STREAMED_TOKENS = `
{
Expand Down Expand Up @@ -244,13 +263,64 @@ const EXPECTED_STREAMED_JSON_DIFF = [
[{ op: "replace", path: "/audience/1", value: "So funny" }],
];

async function acc(iter: AsyncGenerator<object>): Promise<object[]> {
const acc = [];
for await (const chunk of iter) {
acc.push(chunk);
const MARKDOWN_STREAM_TEST_CASES = [
{
name: "Markdown with split code block",
input: ['```json\n{"', 'countries": [{"n', 'ame": "China"}]}', "\n```"],
expected: [{ countries: [{ name: "China" }] }],
},
{
name: "Markdown without json identifier, split",
input: ['```\n{"', 'key": "val', '"}\n```'],
expected: [{ key: "val" }],
},
{
name: "Ignores text after closing markdown backticks",
input: ["```json\n", '{ "data": 123 }', "\n```", " Some extra text"],
expected: [{ data: 123 }],
},
];

describe("Markdown Streaming Scenarios", () => {
for (const testCase of MARKDOWN_STREAM_TEST_CASES) {
test(testCase.name, async () => {
const parser = new JsonOutputParser();
const result = await acc(
parser.transform(streamChunks(testCase.input), {})
);
expect(result).toEqual(testCase.expected);
});
}
return acc;
}
});

test("Handles markdown with text around code block", async () => {
const input = [
"Explanation:\n```json\n{",
'"answer": 42',
"}\n```\nConclusion",
];

const expected = [{}, { answer: 42 }];

const parser = new JsonOutputParser();
const result = await acc(parser.transform(streamChunks(input), {}));
expect(result).toEqual(expected);
});

test("Handles multiple code blocks in single chunk", async () => {
const input = ['```json\n{"a":1}\n```\n```json\n{"b":2}\n```'];

const parser = new JsonOutputParser();
const result = await acc(
parser.transform(
(async function* () {
yield input[0];
})(),
{}
)
);
expect(result).toEqual([{ a: 1 }]);
});

test("JSONOutputParser parses streamed JSON", async () => {
async function* generator() {
Expand Down
25 changes: 21 additions & 4 deletions langchain-core/src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
export function parseJsonMarkdown(s: string, parser = parsePartialJson) {
// eslint-disable-next-line no-param-reassign
s = s.trim();
const match = /```(json)?(.*)```/s.exec(s);
if (!match) {

const firstFenceIndex = s.indexOf("```");
if (firstFenceIndex === -1) {
return parser(s);
} else {
return parser(match[2]);
}

let contentAfterFence = s.substring(firstFenceIndex + 3);

if (contentAfterFence.startsWith("json\n")) {
contentAfterFence = contentAfterFence.substring(5);
} else if (contentAfterFence.startsWith("json")) {
contentAfterFence = contentAfterFence.substring(4);
} else if (contentAfterFence.startsWith("\n")) {
contentAfterFence = contentAfterFence.substring(1);
}

const closingFenceIndex = contentAfterFence.indexOf("```");
let finalContent = contentAfterFence;
if (closingFenceIndex !== -1) {
finalContent = contentAfterFence.substring(0, closingFenceIndex);
}

return parser(finalContent.trim());
}

// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/core/llm/utils/parse_partial_json.py
Expand Down
Loading