Skip to main content

Why AI APIs Use Streaming

Modern AI APIs stream responses using Server-Sent Events (SSE) instead of waiting for the complete response:

Without Streaming

With Streaming

Benefits:
  • ⚡ Faster perceived response time
  • 📱 Better user experience (see response forming)
  • 🔄 Can cancel long responses early
  • 🛠️ Get tool calls before full response completes

Server-Sent Events (SSE) Format

AI APIs send responses as SSE streams:
Each data: line is a chunk. Combined: “Hello there!”

StreamParser Block

StreamParser extracts text and tool calls from SSE streams:
Output:

Supported Formats

StreamParser supports multiple streaming formats:

Basic Usage

Parse OpenAI Stream

Parse Vercel AI SDK Stream

Extracting Content

Text Only

Combines all text chunks into single string.

Tool Calls Only

Extracts all function/tool calls from stream.

Both Text and Tools

Include Metadata

Metadata includes:

Real-World Examples

1. Test ChatGPT-Style Interface

2. Test Tool Calls in Stream

3. Test Vercel AI SDK Streaming

4. Test Partial Response Quality

Test response quality even if stream is cut short:

Performance Considerations

Response Time

Streaming doesn’t make the total time faster, but improves perceived speed:

Testing Performance

For time-to-first-chunk testing, you’d need custom timing logic (not built-in yet).

Error Handling

Incomplete Streams

Malformed SSE

StreamParser handles common issues:
  • Missing data: prefix
  • Invalid JSON in chunks
  • Incomplete tool call objects

Timeout Handling

Combining with Validation Blocks

StreamParser → ValidateContent → LLMJudge

Streaming vs Non-Streaming

Pros:
  • Better UX (see response forming)
  • Can cancel long responses
  • Get tool calls early
Cons:
  • More complex to parse
  • Harder to debug
  • Can’t easily inspect full response
When to use:
  • User-facing chat interfaces
  • Long-form content generation
  • Real-time feedback needed

Best Practices

Streaming can take longer than traditional requests.
  • Empty streams
  • Incomplete streams (connection drops)
  • Very long responses (10,000+ tokens)
  • Multiple tool calls in one stream
  • Mixed text and tool calls

Debugging Streams

Problem: Empty aiMessage

Check:
  1. Is stream format correct?
  2. Look at raw response.body

Problem: Tool calls not extracted

Check:
  1. Correct format? (sse-openai vs sse-vercel)
  2. Are tool calls in the response?
  3. Check metadata.totalTools

Problem: Parse errors

Common causes:
  • Wrong format specified
  • Malformed JSON in chunks
  • Mixed stream formats
Solution:

Format Details

OpenAI Format (sse-openai)

Vercel AI SDK Format (sse-vercel)

Generic SSE Format (sse)

Next Steps

Multi-Turn Conversations

Test conversational AI flows

StreamParser Reference

Complete StreamParser documentation

Tool Call Validation

Validate AI tool/function calls

AI Chat Example

Full streaming chat test example