Principle:Guardrails ai Guardrails Guard Streaming Configuration
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Validation |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
A configuration principle for preparing Guards with streaming-aware validators that define custom chunk boundary detection strategies.
Description
Guard Streaming Configuration addresses the challenge of validating LLM output that arrives incrementally as a stream of small text chunks. The same Guard.use() method is used for attaching validators, but for streaming scenarios, validators should implement a _chunking_function that defines how accumulated text is split into validation-ready segments.
The default chunking strategy uses sentence boundary detection, but validators can override this to validate at word, paragraph, or custom boundaries. This configuration step uses the same Guard.use() API but with awareness that validators will operate in streaming mode.
Usage
Apply this when setting up a Guard that will be called with stream=True. Ensure validators either support the default sentence-based chunking or implement custom _chunking_function methods appropriate for their validation logic.
Theoretical Basis
Streaming validation requires a buffering strategy:
- Chunk Accumulation: LLM tokens are accumulated into a buffer
- Boundary Detection: The validator's _chunking_function checks if enough text has accumulated
- Segment Extraction: When a boundary is found, the ready segment is extracted for validation
- Remainder Carry: Incomplete text is carried to the next accumulation cycle