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

Support Valibot for Structured Outputs

Open fabian-hiller opened this issue 1 year ago • 8 comments

Confirm this is a feature request for the Node library and not the underlying OpenAI API.

  • [X] This is a feature request for the Node library

Describe the feature or improvement you're requesting

Hi, I am the creator of Valibot and would like to ask if you are interested in supporting Valibot for Structured Outputs besides Zod. If so, I would be happy to help with implementation and documentation.

Valibot is a newer schema validation library for TypeScript that I developed about a year ago as part of my bachelor thesis. Since then, the project has grown a lot and is slowly becoming a popular alternative to Zod. Among our partners are two universities as well as companies like Vercel, Netlify and DigitalOcean. I expect to release our v1 RC in the next 2 weeks. This might be the perfect time to integrate it into the OpenAI Node library.

Feel free to check out our website: https://valibot.dev/

Additional context

We are currently in the process of implementing an official toJsonSchema function for Valibot.

fabian-hiller avatar Aug 29 '24 13:08 fabian-hiller

Hey @fabian-hiller, yes of course structured outputs helpers for Valibot is a great idea that we'd be happy to support!

How far along are you with an official toJsonSchema function?

RobertCraigie avatar Sep 03 '24 19:09 RobertCraigie

I expect to finish the implementation of our official toJsonSchema function next week. Should I follow the implementation for Zod in this repository and create a PR with similar code for Valibot? Have you implemented a custom conversion from Zod to JSON Schema instead of using a community package because Structured Outputs does not support the full JSON Schema spec?

fabian-hiller avatar Sep 04 '24 07:09 fabian-hiller

I'm not sure if we'd want to go with the exact same implementation to be honest.

The motivation for using our own zod-to-json-schema were:

  • so we could massage the output to be Structured Outputs compatible
  • to avoid adding a new dependency
  • minor dx improvement that users don't have to install zod-to-json-schema themselves

I don't think we'd want to vendor our own json-schema converter for valibot just to keep the SDK from getting too big. Instead we'd likely go with something more similar to our Python SDK where we have a function that ensures a JSON schema is Structured Outputs compatible, instead of baking that into the JSON schema conversion library itelf.

How does that sound to you?

RobertCraigie avatar Sep 04 '24 12:09 RobertCraigie

For us as a project, it would be an honor and short-term marketing advantage to be part of this library, but otherwise I fully agree that OpenAI should not be forced to maintain an adapter for every schema library.

Should such a function check every schema passed to response_format? Should there be an option to skip this check? Should I create a PR or do you prefer to create this function yourself?

fabian-hiller avatar Sep 05 '24 17:09 fabian-hiller

We will probably release the first version of our toJsonSchema function tomorrow and would be ready to proceed with the integration into OpenAI's SDK.

fabian-hiller avatar Sep 13 '24 01:09 fabian-hiller

v0.1.0 of our official toJsonSchema function is now available:

  • https://github.com/fabian-hiller/valibot/tree/main/packages/to-json-schema
  • https://www.npmjs.com/package/@valibot/to-json-schema
  • https://jsr.io/@valibot/[email protected]

fabian-hiller avatar Sep 13 '24 18:09 fabian-hiller

Awesome! I think the best approach here would be for you to publish a @valibot/openai package (or something like that) and then we'd be more than happy to update our documentation to reference it!

The trickiest part will be ensuring that the generated json schemas are compatible with strict mode so I'd be open to considering adding a helper function to our package for achieving that so it can be reused by other libraries (python version for reference). However it's unlikely we'd be able to prioritise adding that ourselves anytime soon but a PR would be more than welcome if you're up for it :)

RobertCraigie avatar Sep 26 '24 22:09 RobertCraigie

Thanks for getting back to me! I have an alternative idea. I am working with Colin (creator of Zod) and other schema library authors to define a standard interface for schema libraries. This way libraries like the OpenAI Node SDK can reduce their implementation effort and prevent or minimize vendor lock-in. We will be adding more documentation and examples to the Standard Schema repository soon. Below is some pseudocode implementation code to give you a concrete idea:

import type { StandardSchema, InferOutput } from "@standard-schema/spec";
import { toJsonSchema as valibotToJsonSchema } from "@valibot/to-schema-schema";
import type { JSONSchema7 } from "json-schema";
import { zodToJsonSchema } from "zod-to-json-schema";

interface NormalizedScheme<T extends StandardSchema> {
  schema: JSONSchema7;
  validate: (value: unknown) => Promise<InferOutput<T>>;
}

function processSchema<T extends StandardSchema>(schema: T): NormalizedScheme<T> {
  let jsonSchema: JSONSchema7 | undefined;
  if (schema["~vendor"] === "zod") {
    jsonSchema = zodToJsonSchema(schema);
  } else if (schema["~vendor"] === "valibot") {
    jsonSchema = valibotToJsonSchema(schema);
  } else {
    throw new Error("Unsupported schema vendor");
  }
  return {
    schema: jsonSchema,
    validate: async (value) => {
      const result = await schema["~validate"]({ value });
      if (result.issues) {
        throw new Error("Schema validation failed");
      }
      return result.value;
    },
  };
}

fabian-hiller avatar Oct 01 '24 19:10 fabian-hiller

Any updates on this?

vladfaust avatar Nov 05 '25 11:11 vladfaust

The Standard Schema team is currently working on a Standard JSON Schema spec. We will share more details soon. It would be great if OpenAI could integrate it in the SDK.

fabian-hiller avatar Nov 05 '25 19:11 fabian-hiller