-
Notifications
You must be signed in to change notification settings - Fork 916
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
Zod => JSONSchema conversion creates references to unknown definitions #978
Comments
This might be the same issue as the second issue discussed in #970, but that issue is marked as closed and this is still happening to me. Hopefully the above example serves as a good test case! |
Thanks for the bug report, I'm investigating. |
@airhorns this should be fixed in |
Spectacular, thanks so much! |
Hm, I'm not sure why but I'm still seeing an error on v4.55.4. This is my exact test case: test("test", async () => {
const metadata = z.nullable(
z.object({
foo: z.string(),
})
);
const fieldA = z.object({
type: z.literal("string"),
name: z.string(),
metadata,
});
const fieldB = z.object({
type: z.literal("number"),
metadata,
});
const model = z.object({
name: z.string(),
fields: z.array(z.union([fieldA, fieldB])),
});
const completion = await openai.beta.chat.completions.parse({
model: "gpt-4o-2024-08-06",
messages: [
{
role: "system",
content: "You are a helpful assistant. Generate a data model according to the user's instructions.",
},
{ role: "user", content: "create a todo app data model" },
],
tools: [zodFunction({ name: "query", parameters: model })],
});
expect(completion.choices[0].message.tool_calls[0].function.parsed_arguments).toMatchInlineSnapshot();
}); And I checked and double checked that I am using pnpm why -r openai
[email protected] /Users/airhorns/Code/gadget/packages/data-science
dependencies:
@langchain/core 0.2.23
└─┬ langsmith 0.1.41
├─┬ langchain 0.2.15 peer
│ └─┬ @langchain/openai 0.2.6
│ └── openai 4.55.4
└── openai 4.55.4 peer
@langchain/langgraph 0.0.33
└─┬ @langchain/core 0.2.23
└─┬ langsmith 0.1.41
├─┬ langchain 0.2.15 peer
│ └─┬ @langchain/openai 0.2.6
│ └── openai 4.55.4
└── openai 4.55.4 peer
@langchain/openai 0.2.6
├─┬ @langchain/core 0.2.23
│ └─┬ langsmith 0.1.41
│ └── openai 4.55.4 peer
└── openai 4.55.4
api link:../api
└─┬ data-science link:
├─┬ @langchain/core 0.2.23
│ └─┬ langsmith 0.1.41
│ ├─┬ langchain 0.2.15 peer
│ │ └─┬ @langchain/openai 0.2.6
│ │ └── openai 4.55.4
│ └── openai 4.55.4 peer
└─┬ @langchain/langgraph 0.0.33
└─┬ @langchain/core 0.2.23
└─┬ langsmith 0.1.41
├─┬ langchain 0.2.15 peer
│ └─┬ @langchain/openai 0.2.6
│ └── openai 4.55.4
└── openai 4.55.4 peer Could it be that I'm using zod for function parsing and the newly added test uses it as a response format? |
hmm sorry about that, I'll look into it.
I think that'd be unlikely, they use the same function for generating the JSON schema 🤔 |
Is there a reason that the $ref is used in the schema? I tested using the schema without ref ($refStrategy: 'none'), it passes the check. And in the OpenAI playground, the schema that used ref and without ref seems to cost same input tokens. Also when I test using the 'z.literal' (or 'const' in json schema) it does not always match it. But when I change it to enum, it can always match it. Edit: Checked the doc showing the example of the $ref with recursive here. Sorry I missed it previously. |
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
When compiling a zod schema with multiple references to the same nullable object, the compiled JSONSchema refers to definitions that don't exist. This is using the latest version of the openai client with structured output support. I believe the issue comes from an extracted definition trying to reference an inner extracted definition again -- see the example below.
To Reproduce
Here's an example zod schema and function call which triggers the issue:
When run, I get this error:
I ninja'd into the source and console.log'd the generated JSON schema, here's what comes out:
Code snippets
No response
OS
macOS
Node version
v22.2.0
Library version
4.55.3
The text was updated successfully, but these errors were encountered: