Skip to content

Commit

Permalink
fix: printSchema in StructuredOutputParser failing when using Zod (#787)
Browse files Browse the repository at this point in the history
* fix: printSchema failing when using Zod

* Fix lint

* Fix error message

---------

Co-authored-by: Nuno Campos <nuno@boringbits.io>
  • Loading branch information
justindra and nfcampos authored Apr 14, 2023
1 parent 91d90ad commit a6f1055
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions langchain/src/output_parsers/structured.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,53 @@ import { BaseOutputParser, OutputParserException } from "../schema/index.js";

function printSchema(schema: z.ZodTypeAny, depth = 0): string {
if (
schema instanceof z.ZodString &&
schema._def.checks.some((check) => check.kind === "datetime")
(schema instanceof z.ZodString || schema._def.typeName === "ZodString") &&
(schema as z.ZodString)._def.checks.some(
(check) => check.kind === "datetime"
)
) {
return "datetime";
}
if (schema instanceof z.ZodString) {
if (schema instanceof z.ZodString || schema._def.typeName === "ZodString") {
return "string";
}
if (schema instanceof z.ZodNumber) {
if (schema instanceof z.ZodNumber || schema._def.typeName === "ZodNumber") {
return "number";
}
if (schema instanceof z.ZodBoolean) {
if (schema instanceof z.ZodBoolean || schema._def.typeName === "ZodBoolean") {
return "boolean";
}
if (schema instanceof z.ZodDate) {
if (schema instanceof z.ZodDate || schema._def.typeName === "ZodDate") {
return "date";
}
if (schema instanceof z.ZodNullable) {
if (
schema instanceof z.ZodNullable ||
schema._def.typeName === "ZodNullable"
) {
return `${printSchema(schema._def.innerType, depth)} // Nullable`;
}
if (schema instanceof z.ZodTransformer) {
if (
schema instanceof z.ZodTransformer ||
schema._def.typeName === "ZodTransformer"
) {
return `${printSchema(schema._def.schema, depth)}`;
}
if (schema instanceof z.ZodOptional) {
if (
schema instanceof z.ZodOptional ||
schema._def.typeName === "ZodOptional"
) {
return `${printSchema(schema._def.innerType, depth)} // Optional`;
}
if (schema instanceof z.ZodArray) {
if (schema instanceof z.ZodArray || schema._def.typeName === "ZodArray") {
return `${printSchema(schema._def.type, depth)}[]`;
}
if (schema instanceof z.ZodObject) {
if (schema instanceof z.ZodObject || schema._def.typeName === "ZodObject") {
const indent = "\t".repeat(depth);
const indentIn = "\t".repeat(depth + 1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { shape } = schema as z.ZodObject<any>;
return `{${schema._def.description ? ` // ${schema._def.description}` : ""}
${Object.entries(schema.shape)
${Object.entries(shape)
.map(
([key, value]) =>
`${indentIn}"${key}": ${printSchema(value as z.ZodTypeAny, depth + 1)}${
Expand All @@ -51,7 +64,7 @@ ${Object.entries(schema.shape)
${indent}}`;
}

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

export class StructuredOutputParser<
Expand Down

1 comment on commit a6f1055

@vercel
Copy link

@vercel vercel bot commented on a6f1055 Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.