dexaai/openai-fetch

Request with a Request object that has already been used (Next.js 13)

Closed this issue · 9 comments

Capture 2023-01-09 - 000687

Seeing the following error when trying to load an Open AI completion from a Next.js 13 page (server component).

import createOpenAiClient from '~/lib/openai';

async function getData() {
  const data = await createOpenAiClient().createCompletion({
    prompt: `List some cool objects in a room`,
    model: 'text-curie-001',
    temperature: 0,
    frequency_penalty: 0.1,
    max_tokens: 256,
  });

  console.log(JSON.stringify(data.response, null, 2));

  return data.completion;
}

export default async function Page() {
  const response = await getData();

To create the client, I originally used the code cited in the README. However, after seeing this error, I tried to create the client on-demand:

import { OpenAIClient } from 'openai-fetch';

function createOpenAiClient() {
  return new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY });
}

export default createOpenAiClient;

But to no avail.

I noticed you're using ky. A cursory search yielded a seemingly related issue: sindresorhus/ky#479

Thanks for the client btw... extremely well done hierarchy, types, docs. Exactly what I was hoping to find.

Thanks for the detailed issue. The Ky issue you linked to is something that should be fixed with this library in general, but I'm not sure it will fix your issue because it should only run when the OpenAI request fails.

I don't have time to create a repro right now to verify. Can you test this (#5) change with your app?

The other possibility is that the new Next.js request caching could be interfering with the request.

The other possibility is that the new Next.js request caching could be interfering with the request.

You're likely right on the money here. Vercel is extending fetch, which I'm sure has all kinds of awful edge cases.

#5 (comment)

Vercel's custom fetch causes other errors when mixed with ky btw vercel/next.js#41531

Not sure what to do w/ this.

And there you have it... Vercel will likely fix this eventually; could take months.

Ky lets you pass a user defined fetch function: https://github.com/sindresorhus/ky#fetch

If I exposed that, could you pass in a fetch that Vercel isn't screwing with? I don't want the hassle of people trying to use this in non-fetch envs, but I could add it undocumented.

Yes, it would be nice to have that escape hatch–however temporary.

Vercel's best interest is to resolve this, and I bet they will. Half their product focus is DX. They're aware of how a misbehaving enhanced fetch implementation could do damage.

I agree. It might be rocky for the next few months as the Node ecosystem moves from node-fetch to undici so it makes sense to have a little more control.

I've added it to my list, but probably won't get to it for at least a week. Should be relatively simple and I can merge and release if you open a PR.

I would love to see this added as well.