Next.js Discord

Discord Forum

ai-sdk v5 streaming doesn't work

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I was trying to figure out how to do streaming in ai v5 but couldnt understand what am i doing wrong

this is my client side onData doesnt do anything it doesnt log
  const { sendMessage } = useChat({
    onData: (data) => {
      if (data.data?.parts?.[0]?.text) {
        console.log("Message text:", data.data.parts[0].text);
      }
    },
    transport: new DefaultChatTransport({
      api: "/api/project",
    }),
  });


this is my route.ts
import { xai } from "@ai-sdk/xai";
import { streamText } from "ai";
import type { NextRequest } from "next/server";

type Message = {
  id: string;
  role: "user" | "assistant";
  parts: Array<{
    text: string;
  }>;
  createdAt?: Date;
};

export async function POST(req: NextRequest) {
  const { messages } = (await req.json()) as { messages: Message[] };

  const lastUserMessage = messages[messages.length - 1];
  const prompt = String(lastUserMessage?.parts?.[0]?.text);

  if (!prompt) {
    return new Response("No prompt provided", { status: 400 });
  }

  const result = streamText({
    model: xai("grok-3-mini"),
    prompt,
  });

  return result.toTextStreamResponse();
}


in browser network tab I see response is coming but doesn't log anything what I do wrong?

0 Replies