Skip to content
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

Marviel/fix structured output parser #770

Merged
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
13 changes: 11 additions & 2 deletions langchain/src/output_parsers/structured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ function printSchema(schema: z.ZodTypeAny, depth = 0): string {
if (schema instanceof z.ZodDate) {
return "date";
}
if (schema instanceof z.ZodNullable) {
return `${printSchema(schema._def.innerType, depth)} // Nullable`;
}
if (schema instanceof z.ZodTransformer) {
return `${printSchema(schema._def.schema, depth)}`;
}
if (schema instanceof z.ZodOptional) {
return `${printSchema(schema._def.innerType, depth)} // Optional`;
}
Expand All @@ -44,7 +50,8 @@ ${Object.entries(schema.shape)
.join("\n")}
${indent}}`;
}
throw new Error(`Unsupported type: ${schema._def.innerType}`);

throw new Error(`Unsupported type: ${schema._def.innerType.typeName}`);
}

export class StructuredOutputParser<
Expand Down Expand Up @@ -79,6 +86,8 @@ export class StructuredOutputParser<
\`\`\`json
${printSchema(this.schema)}
\`\`\`

Including the leading and trailing "\`\`\`json" and "\`\`\`"
`;
}

Expand All @@ -88,7 +97,7 @@ ${printSchema(this.schema)}
return this.schema.parse(JSON.parse(json));
} catch (e) {
throw new OutputParserException(
`Failed to parse. Text: ${text}. Error: ${e}`
`Failed to parse. Text: "${text}". Error: ${e}`
);
}
}
Expand Down
11 changes: 10 additions & 1 deletion langchain/src/output_parsers/tests/structured.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect } from "@jest/globals";
import { z } from "zod";

import { expect, test } from "@jest/globals";

import { StructuredOutputParser } from "../structured.js";

test("StructuredOutputParser.fromNamesAndDescriptions", async () => {
Expand All @@ -20,6 +21,8 @@ test("StructuredOutputParser.fromNamesAndDescriptions", async () => {
"url": string // A link to the resource
}
\`\`\`

Including the leading and trailing "\`\`\`json" and "\`\`\`"
"
`);
});
Expand All @@ -41,6 +44,8 @@ test("StructuredOutputParser.fromZodSchema", async () => {
"url": string // A link to the resource
}
\`\`\`

Including the leading and trailing "\`\`\`json" and "\`\`\`"
"
`);
});
Expand Down Expand Up @@ -73,6 +78,8 @@ test("StructuredOutputParser.fromZodSchema", async () => {
"sources": string[] // sources used to answer the question, should be websites.
}
\`\`\`

Including the leading and trailing "\`\`\`json" and "\`\`\`"
"
`);
});
Expand Down Expand Up @@ -136,6 +143,8 @@ test("StructuredOutputParser.fromZodSchema", async () => {
}[]
}
\`\`\`

Including the leading and trailing "\`\`\`json" and "\`\`\`"
"
`);
});