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

openai[minor]: Add support for json schema response format #6438

Merged
merged 10 commits into from
Aug 16, 2024

Conversation

bracesproul
Copy link
Collaborator

@bracesproul bracesproul commented Aug 7, 2024

port of https://github.com/langchain-ai/langchain/pull/25123/files

Closes #6477
Closes #6479

Requires a release of @langchain/core and bumping min core version of oai

Copy link

vercel bot commented Aug 7, 2024

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

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 16, 2024 7:21pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Visit Preview Aug 16, 2024 7:21pm

@bracesproul bracesproul changed the title [WIP]openai[minor]: Add support for json schema response format openai[minor]: Add support for json schema response format Aug 12, 2024
@bracesproul bracesproul marked this pull request as ready for review August 12, 2024 21:03
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. auto:improvement Medium size change to existing code to handle new use-cases labels Aug 12, 2024
@bracesproul bracesproul merged commit 5eabf67 into main Aug 16, 2024
40 checks passed
@bracesproul bracesproul deleted the brace/openai-res-format-json-schema branch August 16, 2024 19:40
@david8z
Copy link

david8z commented Aug 26, 2024

Hello @bracesproul is there any example on how should this be used?

@david8z
Copy link

david8z commented Aug 26, 2024

Hello @bracesproul is there any example on how should this be used?

Got it working:

const model = new ChatOpenAI({
  model: "gpt-4o-mini",
}).bind({
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "schemaName",
      schema: zodSchema,
    },
  },
});

const parser = StructuredOutputParser.fromZodSchema(zodSchema);
const typedResponse = await prompt.pipe(model).pipe(parser)

@bracesproul
Copy link
Collaborator Author

@david8z sweet! I also have an example here showing how to use it with withStructuredOutput

@nguyenthinhit996
Copy link

Hi @bracesproul , nice to meet you. I'm currently working on LangChain.js with the Gmail Toolkit (specifically GmailCreateDraft), along with LangSmith and OpenAI. I noticed an issue with the function calling when using optional fields defined with Zod. Although fields like cc and bcc are marked as optional in the toolkit, it seems that the function is requiring all fields to be included. After investigating, I found that the zodToJsonSchema function automatically inserts some default values, which causes the resulting JSON schema to not align with the expected requirements.

image
image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
3 participants