I’m working with Cohere’s streaming API through Vercel’s AI SDK in my Next.js application. The streaming connection works but I keep getting empty content in the assistant’s response messages.
What I expect: The AI should generate text content that shows up in the stream
What happens: Stream connects successfully but message content stays empty
My API key works fine for regular non-streaming requests. Here’s my implementation in the API route:
import { createDataStreamResponse, streamText } from 'ai'
import { cohere } from '@ai-sdk/cohere'
export const runtime = 'edge'
export async function POST(request) {
try {
const { messageHistory } = await request.json()
const response = createDataStreamResponse({
execute: async (stream) => {
stream.writeData('starting stream\n')
try {
const textStream = streamText({
model: cohere('command-r-plus-08-2024'),
messages: messageHistory,
temperature: 0.5,
})
textStream.mergeIntoDataStream(stream)
textStream.toDataStreamResponse()
textStream.toDataStream()
stream.writeData('\nstream finished')
} catch (err) {
console.error('Stream error:', err)
stream.writeData(`Failed: ${err}\n`)
}
},
onError: err => {
return err instanceof Error ? err.message : String(err)
}
})
return response
} catch (err) {
console.log('API error:', err)
throw new Error('Stream processing failed')
}
}
On the frontend, my message array shows this structure where the assistant response has no content:
messageHistory = [
{ id: "abc123", role: "user", content: "Hello there" },
{
id: "def456",
role: "assistant",
content: "", // This stays empty!
parts: [],
createdAt: new Date()
}
]
Any ideas what might be causing the empty content issue?