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

generateObject: Object literal may only specify known properties, and 'schema' does not exist in type #3201

Open
benjick opened this issue Oct 4, 2024 · 4 comments

Comments

@benjick
Copy link

benjick commented Oct 4, 2024

Description

Hello!

I'm migrating from zod-gpt to use ai instead. It's working in every single case, except one where I do this:

  const functionCall = functionCalls[prompt.functionCall];
  const { object, usage } = await generateObject({
    model: openai(prompt.model),
    schema: functionCall,
    prompt: content,
  });

(reproduction below)

It gives me the following error:

No overload matches this call.
  The last overload gave the following error.
    Object literal may only specify known properties, and 'schema' does not exist in type 'Omit<CallSettings, "stopSequences"> & Prompt & { output: "no-schema"; model: LanguageModelV1; mode?: "json" | undefined; experimental_telemetry?: TelemetrySettings | undefined; experimental_providerMetadata?: LanguageModelV1ProviderMetadata | undefined; _internal?: { ...; } | undefined; }'.ts(2769)

What do I need to do to solve this?

Code example

import { createOpenAI } from "@ai-sdk/openai";
import { generateObject } from "ai";
import { z } from "zod";

const openai = createOpenAI({
  apiKey: process.env.OPENAI_API_KEY!,
  compatibility: "strict", // strict mode, enable when using the OpenAI API
});

export const dynamic = "force-dynamic";

type FunctionCall = "test1" | "test2";

const test1 = z.object({
  type: z.literal("test1").default("test1"),
  test: z.string().describe("test1"),
});

const test2 = z.object({
  type: z.literal("test2").default("test2"),
  test: z.string().describe("test2"),
});

const functionCalls = {
  test1,
  test2,
} satisfies Record<FunctionCall, z.ZodTypeAny>;

export const GET = async () => {
  const prompt: {
    functionCall: FunctionCall;
    model: string;
  } = {
    functionCall: "test1",
    model: "gpt-4o-mini",
  };
  const content = "test";

  const functionCall = functionCalls[prompt.functionCall];
  const { object, usage } = await generateObject({
    model: openai(prompt.model),
    schema: functionCall,
    prompt: content,
  });
  return Response.json({ object, usage });
};

Additional context

I've also tried as const Instead of satisfies and not having anything at all. Same issue

"ai": "3.4.9",

@benjick benjick changed the title Object literal may only specify known properties, and 'schema' does not exist in type 'Omit<CallSettings, "stopSequences"> & Prompt & ... generateObject: Object literal may only specify known properties, and 'schema' does not exist in type Oct 4, 2024
@jnimmo
Copy link

jnimmo commented Oct 9, 2024

Try updating your @ai-sdk/openai package - mine seemed to be stuck at 0.0.36 and this resolved it for me.

@benjick
Copy link
Author

benjick commented Oct 10, 2024

Thanks for the comment, I'm on 0.0.66 though

@jonriegel
Copy link

In the code snippet, the functionCall variable you are passing into generateObject appears to be a z.Object. The versions of the overloaded generateObject function whose argument has a schema property, requires that it be a schema type (e.g. z.Schema<ELEMENT, z.ZodTypeDef, any> | Schema<ELEMENT>). I think that's the reason your call doesn't match any of the overloads.

The error message comes from the attempt to match the one declared last in the library, which doesn't have a schema property on its argument at all.

@benjick
Copy link
Author

benjick commented Oct 11, 2024

@jonriegel thanks for the comment. I looked into the definition files after your reply and I see it too. But I don't know how to translate it to code. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants