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

Using Structured Outputs throws body used already for: https://api.openai.com/v1/chat/completions in 4.59.0 #1102

Open
1 task done
jreidgreer opened this issue Sep 26, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@jreidgreer
Copy link

jreidgreer commented Sep 26, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Since 4.59.0, using structured outputs with Zod causes the following error:

TypeError: body used already for: https://api.openai.com/v1/chat/completions
    at Response.consumeBody (/dev/app/node_modules/node-fetch/lib/index.js:344:30)
    at Response.json (/dev/app/node_modules/node-fetch/lib/index.js:269:22)
    at Proxy.defaultParseResponse (/dev/app/node_modules/openai/src/core.ts:68:33)
    at /dev/app/node_modules/openai/src/core.ts:121:42
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at /dev/app/dist/apps/api/main.js:4027:32

To Reproduce

You can reproduce by using the chat completions endpoint with structured outputs. I haven't tried this without Zod.

const completion = await this.client.beta.chat.completions.parse(
  {
    model: "gpt-4o-mini-2024-07-18",
    temperature: 0,
    messages: [
      {
        role: "system",
        content: prompt,
      },
      {
        role: "user",
        content: input,
      },
    ],
    response_format: zodResponseFormat(schema, schemaName),
  },
  { timeout: 60_000 },
);

Using this.client.chat.completions.create with response_format: { type: "json_object" } works just fine.

This is a regression introduced in v4.59.0

Code snippets

No response

OS

macOS

Node version

v20.13.1

Library version

4.59.0

@jreidgreer jreidgreer added the bug Something isn't working label Sep 26, 2024
@jreidgreer
Copy link
Author

Looking at a comparison with v4.58.2 and v4.59.0: v4.58.2...v4.59.0

This seems like the most likely candidate: #1058

@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Sep 26, 2024

Thanks for the report @jreidgreer, I can't reproduce this, could you share more details about how you're using the SDK?

Here's the snippet I used to try to reproduce

import { zodResponseFormat } from 'openai/helpers/zod';
import OpenAI from 'openai/index';
import { z } from 'zod';

const Step = z.object({
  explanation: z.string(),
  output: z.string(),
});

const MathResponse = z.object({
  steps: z.array(Step),
  final_answer: z.string(),
});

const client = new OpenAI();

async function makeRequest() {
  const completion = await client.beta.chat.completions.parse({
    model: 'gpt-4o-2024-08-06',
    messages: [
      { role: 'system', content: 'You are a helpful math tutor.' },
      { role: 'user', content: 'solve 8x + 31 = 2' },
    ],
    response_format: zodResponseFormat(MathResponse, 'math_response'),
  });

  console.dir(completion, { depth: 5 });

  const message = completion.choices[0]?.message;
  if (message?.parsed) {
    console.log(message.parsed.steps);
    console.log(`answer: ${message.parsed.final_answer}`);
  }
}

async function main() {
  await makeRequest();
  await makeRequest();
}

main();

@rattrayalex
Copy link
Collaborator

@jreidgreer can you share a stackblitz or equivalent that reproduces the bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants