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

Feedback for “Getting Started” - Failed to convert the response to stream. #472

Closed
JanKups opened this issue Aug 18, 2023 · 6 comments
Closed

Comments

@JanKups
Copy link

JanKups commented Aug 18, 2023

Next: 13.4.18
OpenAI: 4.0.0

Basic route from /getting-started docs:

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';
 
// Create an OpenAI API client (that's edge friendly!)
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
 
// Set the runtime to edge for best performance
export const runtime = 'edge';
 
export async function POST(req: Request) {
  const { prompt } = await req.json();
 
  // Ask OpenAI for a streaming completion given the prompt
  const response = await openai.completions.create({
    model: 'text-davinci-003',
    stream: true,
    temperature: 0.6,
    max_tokens: 300,
    prompt: `Create three slogans for a business with unique features.
 
Business: Bookstore with cats
Slogans: "Purr-fect Pages", "Books and Whiskers", "Novels and Nuzzles"
Business: Gym with rock climbing
Slogans: "Peak Performance", "Reach New Heights", "Climb Your Way Fit"
Business: ${prompt}
Slogans:`,
  });
  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);
  // Respond with the stream
  return new StreamingTextResponse(stream);
}

Causing the following error:
Failed to convert the response to stream. Received status code: undefined

TypeScript errors for OpenAIStream(response):

Argument of type 'Stream<Completion>' is not assignable to parameter of type 'Response'.
  Type 'Stream<Completion>' is missing the following properties from type 'Response': headers, ok, redirected, status, and 11 more.ts(2345)
@MaxLeiter
Copy link
Member

Thank you, this should be fixed now. But all I did was adjust the types and it worked, so I'm unsure about your error. Are you on the most up-to-date ai and openai?

@Crayon-ShinChan
Copy link

Crayon-ShinChan commented Sep 28, 2023

"next": "13.4.19",
"openai": "^4.10.0",
"ai": "^2.2.13",

Met the similar problem from this get start, this line const stream = OpenAIStream(response);

Argument of type 'Stream<Completion>' is not assignable to parameter of type 'Response | AsyncIterableOpenAIStreamReturnTypes'.
  Type 'Stream<Completion>' is not assignable to type 'AsyncIterable<Completion>'.
    The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types.
      Type 'Promise<IteratorResult<import("/Users/leonz/Github/positive-diary/node_modules/openai/resources/completions").Completion, any>>' is not assignable to type 'Promise<IteratorResult<Completion, any>>'.
 ...
              ```

@bigxalx
Copy link

bigxalx commented Sep 28, 2023

I get the same error, using the exact same versions of the packages. Maybe this issue should be re-opened.

@MaxLeiter MaxLeiter reopened this Sep 28, 2023
@thewebalchemist
Copy link

"next": "13.4.19", "openai": "^4.10.0", "ai": "^2.2.13",

Met the similar problem from this get start, this line const stream = OpenAIStream(response);

Argument of type 'Stream<Completion>' is not assignable to parameter of type 'Response | AsyncIterableOpenAIStreamReturnTypes'.
  Type 'Stream<Completion>' is not assignable to type 'AsyncIterable<Completion>'.
    The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types.
      Type 'Promise<IteratorResult<import("/Users/leonz/Github/positive-diary/node_modules/openai/resources/completions").Completion, any>>' is not assignable to type 'Promise<IteratorResult<Completion, any>>'.
 ...
              ```

Hello, did you manage to get over the error?

@Crayon-ShinChan
Copy link

"next": "13.4.19", "openai": "^4.10.0", "ai": "^2.2.13",
Met the similar problem from this get start, this line const stream = OpenAIStream(response);

Argument of type 'Stream<Completion>' is not assignable to parameter of type 'Response | AsyncIterableOpenAIStreamReturnTypes'.
  Type 'Stream<Completion>' is not assignable to type 'AsyncIterable<Completion>'.
    The types returned by '[Symbol.asyncIterator]().next(...)' are incompatible between these types.
      Type 'Promise<IteratorResult<import("/Users/leonz/Github/positive-diary/node_modules/openai/resources/completions").Completion, any>>' is not assignable to type 'Promise<IteratorResult<Completion, any>>'.
 ...
              ```

Hello, did you manage to get over the error?

I used openai.chat.completions.create rather than openai.completions.create then problem solved. you can see the example here https://sdk.vercel.ai/docs/guides/providers/openai

@bigxalx
Copy link

bigxalx commented Sep 29, 2023 via email

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

5 participants