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 badges1 Answer
Reset to default 0how 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条)