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

fix: printSchema in StructuredOutputParser failing when using Zod #787

Merged
merged 3 commits into from
Apr 14, 2023

Conversation

justindra
Copy link
Contributor

When using Zod with the StructuredOutputParser, it always fails as the instanceof checks never passes as instanceof only works when the class used is the exact same one as the one being compared to. This issue occurs when dealing with monorepos where the same package may have multiple instances.

  const parser = StructuredOutputParser.fromZodSchema(
    z.object({
      name: z.string().describe('The name'),
      artists: z
        .array(z.string())
        .describe('An array of artists'),
    })
  );

The error received is:

Error: Cannot read properties of undefined (reading 'typeName')

So using the schema._def.typeName is a lot more reliable in performing these checks.

@vercel
Copy link

vercel bot commented Apr 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Apr 14, 2023 11:05am

@justindra justindra changed the title fix: printSchema failing when using Zod fix: printSchema in StructuredOutputParser failing when using Zod Apr 14, 2023
@nfcampos
Copy link
Collaborator

Thanks @justindra agreed, this is a good fix, I think we should just remove the instanceof checks in favour of this?

@nfcampos nfcampos added the lgtm PRs that are ready to be merged as-is label Apr 14, 2023
@justindra
Copy link
Contributor Author

justindra commented Apr 14, 2023

@nfcampos yea, I think we can remove the instanceof checks. I'll send through another PR for it.

Update: See #797

@justindra justindra deleted the fix-print-schema-zod branch April 14, 2023 15:56
RohitMidha23 pushed a commit to RohitMidha23/langchainjs that referenced this pull request Apr 18, 2023
…ngchain-ai#787)

* fix: printSchema failing when using Zod

* Fix lint

* Fix error message

---------

Co-authored-by: Nuno Campos <nuno@boringbits.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm PRs that are ready to be merged as-is
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants