Cohere AI SDK streaming response returns blank content field when using Vercel AI SDK

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?

I’ve encountered this issue before, and it seems the root cause lies in how you’re managing the textStream. When you attempt to use both mergeIntoDataStream(stream) and toDataStreamResponse() at the same time, it leads to a race condition, effectively consuming the stream multiple times which results in empty messages. To resolve this, simply use mergeIntoDataStream(stream) without the additional method calls. Your revised API route should look as follows:

const textStream = streamText({
  model: cohere('command-r-plus-08-2024'),
  messages: messageHistory,
  temperature: 0.5,
})

textStream.mergeIntoDataStream(stream)

Avoid invoking toDataStreamResponse() or toDataStream() as this can lead to further complications. Implementing this change resolved my similar issues with Cohere streaming, ensuring that responses are correctly populated.

Had this exact problem last month integrating Cohere streaming. The issue isn’t just multiple stream calls - it’s how you’re handling response creation.

You’re creating createDataStreamResponse then trying to merge a streamText response into it. This conflicts with Cohere’s expected format.

Try this instead:

export async function POST(request) {
  const { messageHistory } = await request.json()
  
  const result = await streamText({
    model: cohere('command-r-plus-08-2024'),
    messages: messageHistory,
    temperature: 0.5,
  })
  
  return result.toDataStreamResponse()
}

Skip the createDataStreamResponse wrapper. Let streamText handle response creation directly.

I hit the same empty content issue because Cohere’s streaming format needs a specific response structure. Wrapping it in additional stream handlers breaks this.

This simplified version should populate your assistant messages properly. Let the AI SDK handle stream formatting instead of trying to customize it.