Skip to content

Support JSON Schema in responseFormat #143

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

Open
hi019 opened this issue Feb 4, 2025 · 1 comment
Open

Support JSON Schema in responseFormat #143

hi019 opened this issue Feb 4, 2025 · 1 comment

Comments

@hi019
Copy link

hi019 commented Feb 4, 2025

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)

@Hamnivore
Copy link

Structured response seems to be working on openai now, but not on gemini models

const tokenjs = new TokenJS();

async function test_structured_response(provider, model) {
  const response = await tokenjs.chat.completions.create({
    provider: provider,
    model: model,
    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",
            },
          },
        },
      },
    },
  });

  console.log(
    provider, 
    model,
    `\nresponse: ${response.choices[0].message.content}`
  );
}

test_structured_response("openai", "gpt-4o-mini"); // outputs {"name":"John Doe"}
test_structured_response("gemini", "gemini-2.0-flash-001"); // outputs  John Doe

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