Weird bug from chat.completions.create() when called through vercel SDK


I’m getting a weird bug from chat.completions.create() when called through vercel SDK

Here is my server side code (stream-chat.js)

// Import OpenAI from the 'openai' package as per the documentation
import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

// Hardcoded OpenAI API client configuration for testing
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY, // Ensure to replace with your actual API key

// IMPORTANT! Set the runtime to edge
export const runtime = 'edge';

// Handler function for the API route
export default async function handler(req, res) {
  console.log('Handler invoked with method:', req.method);
  if (req.method === 'POST') {
    console.log('Handling POST request');
    const { messages } = req.body; // Get messages from the request body
    console.log('Message received from client:', messages); // Log the message received from client
    try {
      console.log('Creating chat completion');
      const response = await{
        model: 'gpt-3.5-turbo',
        stream: true,
        messages: messages,
      console.log('Response from OpenAI:', response); // Log the response from OpenAI
      for await (const part of stream) {

      console.log('Chat completion created, streaming response');
      const stream = OpenAIStream(response);
      console.log('Streaming response:', stream); // Log the streaming response

      // Send the stream as the response
      res.status(200).send(new StreamingTextResponse(stream));
      console.log('Response sent');
    } catch (error) {
      console.error('Error encountered:', error); // Log detailed error
      res.status(500).json({ error: error.message });
  } else {
    console.log('Non-POST request received, method:', req.method);
    res.setHeader('Allow', ['POST']);
    res.status(405).end(`Method ${req.method} Not Allowed`);

I got 200 OK in vercel but the logs say

Handler invoked with method: POST
Handling POST request
Message received from client: [ { role: ‘user’, content: ‘Is AI good or bad to Humanity?’ } ]
Creating chat completion
Response from OpenAI: Stream {
iterator: [AsyncGeneratorFunction: iterator],
controller: AbortController { signal: AbortSignal { aborted: false } }
Chat completion created, streaming response
Streaming response: ReadableStream { locked: false, state: ‘readable’, supportsBYOB: false }
Response sent

I intentionally gave a wrong API key to check if the issue is pre-authentication or post-authentication. It gave same error even with the wrong API key, so it looks like the issue is pre-authentication.

Does anyone know what this error is and how to fix it? :pray:

Thanks a ton!