Skip to main content

Streaming Overview

The NeMo Guardrails server supports streaming responses using Server-Sent Events (SSE). When streaming is enabled, the server sends partial message deltas as they are generated, allowing for real-time response display.

Enabling Streaming

To enable streaming, set stream: true in your request:

Streaming with Output Rails

When output rails are configured, you need to enable streaming support in your guardrails configuration:
config.yml

Configuration Options

boolean
default:"false"
required
Enables streaming mode for output rails.
integer
default:"200"
The number of tokens in each processing chunk. This is the size of the token block on which output rails are applied.
integer
default:"50"
The number of tokens carried over from the previous chunk to provide context for continuity in processing.
boolean
default:"true"
If true, token chunks are streamed immediately before output rails are applied. If false, chunks are buffered and streamed only after rails check.

Streaming Response Format

Streaming responses use Server-Sent Events (SSE) format. Each chunk is sent as a data: line:

Chunk Structure

string
Unique identifier for the streaming response.
string
Always “chat.completion.chunk”.
integer
Unix timestamp.
string
The model being used.
array
integer
The choice index (always 0).
object
string
The content delta (token chunk).
string
Present only in the first chunk, always “assistant”.
string | null
Null during streaming, set to “stop”, “length”, or “content_filter” in the final chunk.

Error Handling in Streaming

If an error occurs during streaming, an error chunk is sent:
The stream is then terminated with data: [DONE].

Streaming with Rails Applied

When output rails are enabled, the streaming behavior depends on the configuration:

Stream-First Mode (Default)

With stream_first: true, tokens are streamed immediately and output rails are applied in parallel:
  1. LLM generates tokens
  2. Tokens are immediately streamed to client
  3. Output rails process chunks in parallel
  4. If rails detect an issue, streaming is aborted with an ABORT event

Buffer-First Mode

With stream_first: false, chunks are buffered and only streamed after passing rails:
  1. LLM generates tokens
  2. Tokens are buffered into chunks
  3. Output rails process each chunk
  4. Only approved chunks are streamed to client

Performance Considerations

Chunk Size

Larger chunk sizes:
  • Reduce the number of rail checks
  • Lower latency for rail processing
  • Higher time-to-first-token
Smaller chunk sizes:
  • More frequent rail checks
  • Higher rail processing overhead
  • Lower time-to-first-token

Context Size

The context_size parameter ensures continuity between chunks:
This helps rails detect issues that span chunk boundaries.

Advanced Streaming Example

Troubleshooting

StreamingNotSupportedError

If you get this error, enable streaming in your config:
config.yml

Slow Streaming

If streaming is slow:
  1. Increase chunk_size to reduce rail processing overhead
  2. Use stream_first: true to stream immediately
  3. Optimize your output rail flows

Incomplete Responses

If responses are cut off:
  1. Check for ABORT events in the stream
  2. Review output rail logs
  3. Adjust max_tokens parameter