Back to Blog
Blog

Introducing Real-Time AI Text Generation with the New Streaming Option

Cosmic's avatar

Cosmic

March 28, 2025

Hero image

We're excited to announce the addition of streaming capabilities to our AI text generation feature in the Cosmic API. This powerful enhancement allows your applications to receive AI-generated content in real-time, creating more dynamic and responsive user experiences. This feature is available in the Cosmic API and Cosmic JavaScript SDK v1.5.0.

What's New

Our AI text generation endpoint now supports a new stream parameter that enables real-time streaming of AI responses as they're being generated, rather than waiting for the complete response.

Key Benefits

  • Improved User Experience: Display AI-generated content as it's created, similar to how popular AI chatbots work
  • Faster Perceived Response Times: Users see content appearing immediately rather than waiting for the full response
  • Better Feedback Loop: Implement typing indicators or progressive rendering for a more natural interaction

How to Use Streaming

Simply add stream: true to your generateText request:

const stream = await cosmic.ai.generateText({ prompt: "Write an article about coffee", stream: true // Enable streaming });

We've also added a simplified helper method:

const stream = await cosmic.ai.stream({ prompt: "Write an article about coffee", }); stream.on('text', (text) => { console.log(text); }); stream.on('end', (data) => { console.log('Final response data:', data); });

Processing the Stream

The stream response can be handled in two flexible ways:

Event-Based Approach

let fullResponse = ''; stream.on('text', (text) => { fullResponse += text; // Update UI in real-time as text arrives console.log(text); }); stream.on('end', () => { // Stream complete console.log('Final response:', fullResponse); });

Using For-Await Loops

let fullResponse = ''; for await (const chunk of stream) { if (chunk.text) { fullResponse += chunk.text; // Update UI with new text console.log(chunk.text); } } console.log('Complete:', fullResponse);

REST API Support

Streaming is also available via direct API calls:

curl https://workers.cosmicjs.com/v3/buckets/${BUCKET_SLUG}/ai/text \ -H 'Content-Type: application/json' \ -H "Authorization: Bearer ${BUCKET_WRITE_KEY}" \ -d '{"prompt":"Tell me about coffee mugs","stream":true}' \ --no-buffer

Get Started

This feature is available now in our latest JavaScript SDK version. Update your dependencies to start using AI text streaming today. Learn more in the Cosmic documentation.

We look forward to see how you'll use this new capability to create more engaging, responsive applications.

Hero image