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

[BUG] Importing a schema object in script() lead to a 400 error #922

Open
waltoss opened this issue Dec 7, 2024 · 6 comments
Open

[BUG] Importing a schema object in script() lead to a 400 error #922

waltoss opened this issue Dec 7, 2024 · 6 comments

Comments

@waltoss
Copy link
Contributor

waltoss commented Dec 7, 2024

The following GenAIScript configuration leads to a 400 Error when making a request to OpenAI:

import { dialog } from "./schema-magnolia.mts";

script({
  model: "openai:gpt-4o",
  system: ["system.schema"],
  responseSchema: dialog,
  responseType: "json_schema",
});

defSchema("DIALOG_SCHEMA", dialog);

$`
Generate a dialog for Magnolia CMS respecting DIALOG_SCHEMA
`;

The OpenAI API returns the following error:

- Invalid schema for response_format 'result': In context=('properties', '0'), 'default' is not permitted.

The reason is that responseSchema is equal to the string value "dialog" instead of the imported js object

Image

This is due to parsePromptScriptMeta turning the code inside script({...}) into JSON

Image

Do you have a lead to fix that ?

@waltoss waltoss changed the title Can't import a schema [BUG] Importing a schema object in script() lead to a 400 error Dec 7, 2024
Copy link

github-actions bot commented Dec 7, 2024

{
  "feedback": "The issue appears to be due to how the `script()` function is handling the `responseSchema`. The current implementation seems to be converting the schema into a string rather than keeping it as an object. To resolve this, you may need to adjust the part of the code that parses and converts the prompt script metadata to ensure that the `responseSchema` is preserved as an object instead of a string. This will allow OpenAI to correctly interpret the schema when generating the response."
}

generated by issue-reviewer

@pelikhan
Copy link
Member

pelikhan commented Dec 7, 2024

Ha yes, this is a hack to speed up parsing of the script to extract the metadata to avoid loading and executing the script.

I will bring back running the script as a bailout scenario.

@pelikhan
Copy link
Member

pelikhan commented Dec 7, 2024

For now you have to inline it

@pelikhan
Copy link
Member

pelikhan commented Dec 7, 2024

Actually since it parsed it correctly, we should try to test the properties are a string (unresolved variables) and remove them. We don't need the schema at this point. I'll get a fix later.

@waltoss
Copy link
Contributor Author

waltoss commented Dec 8, 2024

Yes I inlined it like this

import { dialog } from "./zod-magnolia.mts";
import { zodResponseFormat } from "openai/helpers/zod";

const schema = zodResponseFormat(dialog, "dialog").json_schema
  .schema as unknown as JSONSchemaObject;

const result = await runPrompt(
  (_) => {
    _.defSchema("DIALOG_SCHEMA", schema, { format: "json" });
    _.$`Generate a dialog for Magnolia CMS respecting DIALOG_SCHEMA. This dialog allows to add an article`;
  },
  {
    model: "openai:gpt-4o",
    responseType: "json_schema",
    responseSchema: schema,
  }
);

By the way, what do you think about a small helper around zod -> schema ?

@pelikhan
Copy link
Member

pelikhan commented Dec 8, 2024

Yes that would be great. Could be added to the already existing schema validation helpers.

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

2 participants