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-deno: Type-checking error for zodResponseFormat with Deno and deno.land imports #984

Open
1 task done
hammerlscs opened this issue Aug 12, 2024 · 3 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@hammerlscs
Copy link

hammerlscs commented Aug 12, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Type-checking using "deno check" fails with either

error: TS2589 [ERROR]: Type instantiation is excessively deep and possibly infinite.
response_format: zodResponseFormat(MathResponse, "math_response")

or

Fatal JavaScript out of memory: Reached heap limit

when using deno.land imports for OpenAI and Zod.

To Reproduce

(The example is from https://github.com/openai/openai-node/blob/master/helpers.md#auto-parsing-response-content-with-zod-schemas)

With deno.land imports

main.ts:

import { zodResponseFormat } from "openai/helpers/zod.ts";
import OpenAI from "openai/mod.ts";
import { z } from "zod";

const Step = z.object({
  explanation: z.string(),
  output: z.string(),
});

const MathResponse = z.object({
  steps: z.array(Step),
  final_answer: z.string(),
});

const client = new OpenAI();

const completion = await client.beta.chat.completions.parse({
  model: "gpt-4o-2024-08-06",
  messages: [
    { role: "system", content: "You are a helpful math tutor." },
    { role: "user", content: "solve 8x + 31 = 2" },
  ],
  response_format: zodResponseFormat(MathResponse, "math_response"),
});

console.dir(completion, { depth: 5 });

const message = completion.choices[0]?.message;
if (message?.parsed) {
  console.log(message.parsed.steps);
  console.log(`answer: ${message.parsed.final_answer}`);
}

with deno.json:

{
  "imports": {
    "zod": "https://deno.land/x/[email protected]/mod.ts",
    "openai/": "https://deno.land/x/[email protected]/"
  }
}

Running deno check main.ts fails with:

Check file://main.ts
error: TS2589 [ERROR]: Type instantiation is excessively deep and possibly infinite.
response_format: zodResponseFormat(MathResponse, "math_response"),

With npm imports

main.ts:

import { zodResponseFormat } from "openai/helpers/zod";
import OpenAI from "openai";
import { z } from "zod";

[same code]

with deno.json:

{
  "imports": {
    "zod": "npm:[email protected]",
    "openai": "npm:[email protected]",
    "openai/": "npm:/[email protected]/"
  }
}

Running deno check main.ts with npm imports is successful

OS

macOS

Node version

deno 1.45.5, typescript 5.5.2

Library version

4.55.4

@hammerlscs hammerlscs added the bug Something isn't working label Aug 12, 2024
@alexkates
Copy link

Same bug with the following versions and example

import OpenAI from "https://deno.land/x/[email protected]/mod.ts";
import { zodResponseFormat } from "https://deno.land/x/[email protected]/helpers/zod.ts";
import { z } from "https://deno.land/x/[email protected]/mod.ts";
const WildlifeSchema = z.object({
  commonName: z.string(),
});

image

@RobertCraigie
Copy link
Collaborator

Thanks for the report. It'd be helpful if someone here could help us narrow down the issue, does this also result in a type error for you?

import { ResponseFormatJSONSchema } from 'openai/resources';
import z from 'zod';
import type { infer as zodInfer, ZodType } from 'zod';

export type AutoParseableResponseFormat<ParsedT> = ResponseFormatJSONSchema & {
  __output: ParsedT; // type-level only

  $brand: 'auto-parseable-response-format';
  $parseRaw(content: string): ParsedT;
};

export function zodTest<ZodInput extends ZodType>(
  zodObject: ZodInput,
  name: string,
  props?: Omit<ResponseFormatJSONSchema.JSONSchema, 'schema' | 'strict' | 'name'>,
): AutoParseableResponseFormat<zodInfer<ZodInput>> {
  throw new Error('not implemented');
}

const WildlifeSchema = z.object({
  commonName: z.string(),
});
const fmt = zodTest(WildlifeSchema, 'wildLifeSchema');

@hammerlscs
Copy link
Author

No, Deno can successfully type-check your code. (I had to adjust your first import, import { ResponseFormatJSONSchema } from "openai/resources/shared.ts";)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants