Encountering Error while content stream from OpenAI API

Using expo tsx file, trying to fetch data directly from user side.

Problem accurs when i try to get the data back

Error:

Possible unhandeled promise rejection

TypeError: Object is not async iterable

Request for Stream

const Response = await openai.chat.completions.create({
    model: "gpt-3.5-turbo-0125",
    stream:true,
    messages: updatedChat
  });

  let fullResponse = "";

  for await (const chunk of Response) {
    process.stdout.write(chunk.choices[0]?.delta?.content || "");
  }

Async function

const handleUserInput = async ()=>{

    let updatedChat =[
        ...chat,
        {
            role:"user",
            content: userInput,
        },
    ];
    setLoading(true);


  const Response = await openai.chat.completions.create({
    model: "gpt-3.5-turbo-0125",
    stream:true,
    messages: updatedChat
  });

  let fullResponse = "";

  for await (const chunk of Response) {
    process.stdout.write(chunk.choices[0]?.delta?.content || "");
  }

//   const modelResponse = Response.choices[0].message.content

   


    // try{
      
        
    //     if(modelResponse){
    //         const updatedChatWithModel = [
    //             ...updatedChat,
    //             {
    //                 role:"assistant",
    //                 content:modelResponse,

    //             },
    //         ];
    //         setChat(updatedChatWithModel);
    //         setUserInput("");
    //     }

        
    // }catch(error){
    //     console.log(error)
    // }finally{
    //     setLoading(false)
    // }

};

Trying to get the stream data back using tsx file directly from client side code.
Encountered an error:

Possible unhandeled promise rejection

TypeError: Object is not async iterable

Any news on the topic? Iā€™m encountering the same issue with assistants, also using expo.

I added in a Node js based backend and stopped trying to get results directly from the client side code.

You can accomplish this on both the client & server by using the Streams API. (optionally) Use the back-end to set a transform for the stream (mainly filter out the client-useless data) and then pass the Reader object down to then consume and stream on the client-side.

1 Like