Skip to content

Support JSON Schema in responseFormat #143

Open
@hi019

Description

@hi019

It would be great if Token.js could support type: "json_schema" in response_format:

import { TokenJS } from "token.js";

const t = new TokenJS();

await o.chat.completions.create({
  provider: "openai",
  model: "gpt-4o-mini",
  messages: [
    {
      role: "user",
      content: "Extract the name from the following text: 'John Doe'",
    },
  ],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "test",
      schema: {
        type: "object",
        properties: {
          name: {
            type: "string",
            description: "The name of the person",
          },
        },
      },
    },
  },
});

This works with the native client:

import { OpenAI} from "openai"

const o = new OpenAI()

await o.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [
    {
      role: "user",
      content: "Extract the name from the following text: 'John Doe'",
    },
  ],
  response_format: {
    type: "json_schema", // Error, can only be "json_object" or "text"
    json_schema: {
      name: "test",
      schema: {
        type: "object",
        properties: {
          name: {
            type: "string",
            description: "The name of the person",
          },
        },
      },
    },
  },
});

Docs: https://platform.openai.com/docs/guides/structured-outputs?lang=javascript

It looks like the json_schema type doesn't exist on the currently depended on openai version (4.52.2), so we probably need to update to the latest (4.82.2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions