openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

openai-deno: Type-checking error for zodResponseFormat with Deno and deno.land imports

Open ghost opened this issue 1 year ago • 4 comments

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

  • [X] 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

ghost avatar Aug 12 '24 10:08 ghost

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

alexkates avatar Aug 13 '24 10:08 alexkates

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');

RobertCraigie avatar Sep 06 '24 11:09 RobertCraigie

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

ghost avatar Sep 06 '24 11:09 ghost

We're now publishing to JSR, could you try using that package instead and report back if you're still seeing issues?

RobertCraigie avatar Nov 11 '24 10:11 RobertCraigie

I can confirm that this is still an issue when using only packages from JSR

zkdiff avatar Apr 21 '25 06:04 zkdiff

Same issue, using the deno 2.4.2. The file that contains zodResponseFormat is not parsable when openai and zod are installed thru jsr.

when both modules are installed thru npm, I still get a compilation error in the call to zodResponseFormat:

  "dependencies": {
    "@types/web": "^0.0.232",
    "openai": "^5.10.2",
    "zod": "^4.0.10"
  },

Compilation Error: Argument of type

ZodObject<{
  comment: ZodString;
  suggest: ZodObject<{
    source: ZodString;
    suggestions: ZodArray<ZodString>; 
  }, $strip>; 
}, $strip>

is not assignable to parameter of type

ZodType<any, ZodTypeDef, any>

Type

ZodObject<{
  comment: ZodString;
  suggest: ZodObject<{
    source: ZodString;
    suggestions: ZodArray<ZodString>;
  }, $strip>; },
$strip>

is missing the following properties from type

ZodType<any, ZodTypeDef, any>

_type, _parse, _getType, _getOrReturnCtx, and 7 more.deno-ts(2345)

Casting to any with the relevant warning turned off works.

imdfl avatar Jul 28 '25 08:07 imdfl