-
Notifications
You must be signed in to change notification settings - Fork 863
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
Difficulty in importing { zodResponseFormat } from https://deno.land #972
Comments
@samreid when you say the types don't work, do you get an error or is it resolved to |
Thanks for the detailed report and sorry this isn't working in deno right now. Will investigate. |
This looks fixed in // deno run --allow-all src/main-structured.ts
import OpenAI from 'https://deno.land/x/[email protected]/mod.ts';
import { z } from 'https://deno.land/x/[email protected]/mod.ts';
import { zodResponseFormat } from 'https://deno.land/x/[email protected]/helpers/zod.ts';
const openai = new OpenAI();
console.log( openai.hello ); // checking for a type error here
const WriteFile = z.object( {
op: z.literal( 'writeFile' ),
writeFilename: z.string(),
contents: z.string()
} );
const ReadFile = z.object( {
op: z.literal( 'readFile' ),
readFilename: z.string()
} );
// Define an anyOf schema using Zod
const FileOperation = z.object( {
selectedAction: z.union( [ ReadFile, WriteFile ] )
} );
const x = zodResponseFormat( FileOperation, 'mySchema' )
console.log( JSON.stringify( x, null, 2 ) );
// Use the anyOf schema in response_format
const completion = await openai.beta.chat.completions.parse( {
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'Choose the next command to accomplish the goal.' },
{ role: 'user', content: 'Write a haiku' }
],
response_format: zodResponseFormat( FileOperation, 'mySchema' )
} );
const event = completion.choices[ 0 ].message.parsed;
console.log( event ); I saw that this line: const x = zodResponseFormat( FileOperation, 'mySchema' ) triggers an error:
But that sounds like a separate issue. Nice work everyone. Closing. |
@samreid Did you manage to resolve the other error? I have the same issue.
|
I did not investigate it further, but I did commit this type assertion: // this any speeds up type checking by about 20 seconds
response_format: ( zodResponseFormat as any )( totalSchema, 'commandSchema' ) |
does this work for you? type Schema = z.infer<typeof totalSchema>; |
Facing this same problem here, where also type checking gets very laggy. @samreid solution with casting to @RobertCraigie This works independent of that type Schema = z.infer<typeof totalSchema>; |
@KevinKreps thanks, is any additional information provided in the error message? e.g. a stack trace? |
The best I can think of right now is running
|
thanks @KevinKreps! can you / someone in this thread file an issue with Deno? I don't think we're doing anything too weird here so I suspect there's some bug with the deno typescript implementation. unfortunately it's unlikely we're going to be able to prioritise investigating this in the short term, so any PR would be really appreciated :) |
Confirm this is a feature request for the Node library and not the underlying OpenAI API.
Describe the feature or improvement you're requesting
In testing with structured outputs,
import { zodResponseFormat } from 'npm:openai/helpers/zod';
works correctly at runtime (with --unstable, but does not provide types), butimport { zodResponseFormat } from 'https://deno.land/x/[email protected]/helpers/zod.ts';
yields runtime resolution errors. I do not have enough expertise to know whether I'm doing something wrong or whether there is a problem in the deno.land for the helpers/zod. Please advise.Additional context
In testing with the structured outputs, this works correctly (using npm: import resolution):
However, this does not yield type information and must be run with --unstable. The openai-node documentation says we can import from deno like so:
However, when trying to import all imports from deno.land like so:
We obtain this error at runtime:
I do not have enough expertise to know whether I'm doing something wrong or whether there is a problem in the deno.land for the helpers/zod. Please advise.
The text was updated successfully, but these errors were encountered: