next.js - Issue with streamText in OpenRouter AI SDK on Vercel (AI_APICallError: Not Found) - Stack Overflow

I’m using OpenRouter AI SDK in my Vercel project to implement AI-powered chat streaming. While textGene

I’m using OpenRouter AI SDK in my Vercel project to implement AI-powered chat streaming. While textGenerate works fine, streamText fails with the following error:

Error from the streamText: AI_APICallError: Not Found

Here’s the relevant part of my API route:


import {
  type Message,
  createDataStreamResponse,
  smoothStream,
  streamText,
} from 'ai';

import { auth } from '@/app/(auth)/auth';
import { myProvider, openrouterProvider, MODEL_IDS } from '@/lib/ai/models';
import { systemPrompt } from '@/lib/ai/prompts';
import {
  deleteChatById,
  getChatById,
  saveChat,
  saveMessages,
} from '@/lib/db/queries';
import {
  generateUUID,
  getMostRecentUserMessage,
  sanitizeResponseMessages,
} from '@/lib/utils';

export async function POST(request: Request) {
  const {
    id,
    messages,
    selectedChatModel,
  }: { id: string; messages: Array<Message>; selectedChatModel: string } =
    await request.json();

  const modelId = MODEL_IDS[selectedChatModel] || MODEL_IDS['chat-model-small'];

  const session = await auth();

  if (!session?.user?.id) {
    return new Response('Unauthorized', { status: 401 });
  }

  const userMessage = getMostRecentUserMessage(messages);
  if (!userMessage) {
    return new Response('No user message found', { status: 400 });
  }

  await saveMessages({
    messages: [{ ...userMessage, createdAt: new Date(), chatId: id }],
  });

  return createDataStreamResponse({
    execute: async (dataStream) => {
      const result = streamText({
        model: openrouterProvider.chat(modelId),
        system: systemPrompt({ selectedChatModel }),
        messages,
        onError({ error }) {
          console.error('Error from the streamText: ' + error);
        },
        experimental_transform: smoothStream({ chunking: 'word' }),
        experimental_generateMessageId: generateUUID,
      });

      await result.consumeStream();
      result.mergeIntoDataStream(dataStream, { sendReasoning: true });
    },
    onError: (e) => {
      return 'Oops, an error occurred! Error: ' + e;
    },
  });
}

What I Tried

1.  Confirmed textGenerate works – The issue only happens with streamText.
2.  Checked model ID – The model ID is correctly retrieved from MODEL_IDS.
3.  Verified OpenRouter API – The OpenRouter API key is set up correctly, and other API calls work.
4.  Checked OpenRouter documentation – There’s no clear mention of additional setup for streamText.

Expected Behavior

streamText should return a streaming response like textGenerate, but instead, it returns AI_APICallError: Not Found.

Question

Has anyone encountered this issue with OpenRouter’s streamText? Could this be a missing configuration or an issue with OpenRouter’s API? Any help would be appreciated!

I’m using OpenRouter AI SDK in my Vercel project to implement AI-powered chat streaming. While textGenerate works fine, streamText fails with the following error:

Error from the streamText: AI_APICallError: Not Found

Here’s the relevant part of my API route:


import {
  type Message,
  createDataStreamResponse,
  smoothStream,
  streamText,
} from 'ai';

import { auth } from '@/app/(auth)/auth';
import { myProvider, openrouterProvider, MODEL_IDS } from '@/lib/ai/models';
import { systemPrompt } from '@/lib/ai/prompts';
import {
  deleteChatById,
  getChatById,
  saveChat,
  saveMessages,
} from '@/lib/db/queries';
import {
  generateUUID,
  getMostRecentUserMessage,
  sanitizeResponseMessages,
} from '@/lib/utils';

export async function POST(request: Request) {
  const {
    id,
    messages,
    selectedChatModel,
  }: { id: string; messages: Array<Message>; selectedChatModel: string } =
    await request.json();

  const modelId = MODEL_IDS[selectedChatModel] || MODEL_IDS['chat-model-small'];

  const session = await auth();

  if (!session?.user?.id) {
    return new Response('Unauthorized', { status: 401 });
  }

  const userMessage = getMostRecentUserMessage(messages);
  if (!userMessage) {
    return new Response('No user message found', { status: 400 });
  }

  await saveMessages({
    messages: [{ ...userMessage, createdAt: new Date(), chatId: id }],
  });

  return createDataStreamResponse({
    execute: async (dataStream) => {
      const result = streamText({
        model: openrouterProvider.chat(modelId),
        system: systemPrompt({ selectedChatModel }),
        messages,
        onError({ error }) {
          console.error('Error from the streamText: ' + error);
        },
        experimental_transform: smoothStream({ chunking: 'word' }),
        experimental_generateMessageId: generateUUID,
      });

      await result.consumeStream();
      result.mergeIntoDataStream(dataStream, { sendReasoning: true });
    },
    onError: (e) => {
      return 'Oops, an error occurred! Error: ' + e;
    },
  });
}

What I Tried

1.  Confirmed textGenerate works – The issue only happens with streamText.
2.  Checked model ID – The model ID is correctly retrieved from MODEL_IDS.
3.  Verified OpenRouter API – The OpenRouter API key is set up correctly, and other API calls work.
4.  Checked OpenRouter documentation – There’s no clear mention of additional setup for streamText.

Expected Behavior

streamText should return a streaming response like textGenerate, but instead, it returns AI_APICallError: Not Found.

Question

Has anyone encountered this issue with OpenRouter’s streamText? Could this be a missing configuration or an issue with OpenRouter’s API? Any help would be appreciated!

Share Improve this question edited Mar 4 at 7:07 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Mar 4 at 0:01 NewbieMFNewbieMF 236 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

how about that

const result = streamText({
    model: openrouterProvider.chat(modelId),
    system: systemPrompt({ selectedChatModel }),
    messages,
    onError({ error }) {
      console.error('Error from the streamText: ' + error);
     },
    experimental_transform: smoothStream({ chunking: 'word' }),
    experimental_generateMessageId: generateUUID,
  })

return result.toDataStreamResponse({
    sendReasoning: true,
    getErrorMessage: (e) => {
      return 'Oops, an error occurred! Error: ' + e
    },
  })

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745067051a4609317.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信