Skip to main content
Version: Dev

Streaming Guardrail Behavior

An enforcing output guardrail must decide before unsafe content reaches a streaming caller. AISIX therefore holds some or all upstream frames, evaluates the available text, and releases only content that clears the enforcing chain.

Hold-back changes time to first token and memory use, and the delivery pattern varies by endpoint. Guardrail Content Coverage defines which fields are available for inspection; Guardrail Failure Handling explains what happens when inspection cannot finish.

Streaming Output​

Enforcing output guardrails use full hold-back or windowed inspection. The selected pattern determines when clean content can reach the caller:

PatternBehavior
Full hold-backAISIX buffers the complete response, evaluates it, and then releases or refuses it.
Windowed inspectionAISIX collects a window, evaluates it, and releases clean windows incrementally on routes that support incremental release.

Holding content prevents a blocked or unmasked span from reaching the caller before the decision. It also lets AISIX detect matches that cross upstream chunks.

A monitor-only output chain never holds content back. AISIX relays the stream live, scans the complete generated output after the stream ends, and records observations without a size cutoff. This includes streamed tool-call arguments, Responses streams, and streamed transcription text. If any enforcing output guardrail shares the chain, the enforcing policy determines hold-back behavior.

Configure Buffer Limits​

Buffer limits cap the amount of generated content AISIX holds before it must refuse the response or release it without further output scanning. Guardrail kinds that expose these settings use the following defaults:

{
"max_buffer_bytes": 262144,
"on_buffer_exceeded": "fail_closed"
}

In AISIX Enterprise or AISIX Cloud, configure these values inside the guardrail's config object. In a format-v2 resources file, configure them on the guardrail entry.

SettingBehavior
max_buffer_bytesMaximum model-generated content held for inspection.
on_buffer_exceeded: fail_closedRefuse an oversized response.
on_buffer_exceeded: fail_openRelease the held content unscanned and unmasked, then continue without output scanning.

Use fail-open overflow only when availability is more important than strict response enforcement. Guardrail kinds without configurable buffer fields use the built-in limit and fail closed.

What Counts Toward the Limit​

Buffer accounting follows generated content rather than transport framing. The content limit includes assistant text, reasoning, and tool-call arguments, but not SSE or JSON framing. Equivalent content therefore reaches the limit at the same point across Chat Completions, Messages, Responses, and recognized passthrough streams. For an unrecognized passthrough SSE stream, the complete data: payload of each frame counts.

Frames with no generated content—such as pings, keep-alives, and empty deltas—do not count as content but still use memory. AISIX also caps raw held bytes at 128 times max_buffer_bytes, which is 32 MiB with the default content limit.

Generated image bytes count toward neither limit. A held stream that contains images can therefore use additional memory.

How a Chain Selects a Limit​

When several full-buffer guardrails share an output chain, AISIX folds their limits and failure policies into one decision. The smallest configured max_buffer_bytes applies, and overflow fails closed when any guardrail that contributes to that limit fails closed.

Under fail-open overflow, AISIX records the bypass reason output_buffer_exceeded. Under fail-closed overflow, it records blocked_buffer_exceeded and names the guardrail whose limit the chain reached.

Window Mode on Messages and Responses​

Window mode can release clean content incrementally only on routes that support partial delivery. Azure Text Moderation, Alibaba Cloud Content Moderation, Alibaba Cloud AI Guardrails, and custom scripts default to this mode.

RouteWindow-Mode Delivery
Chat Completions and passthrough streamsAISIX can release each window after it scans clean.
Messages and ResponsesAISIX must hold the complete response whenever the chain can block or mask, then deliver only after the final scan.

Messages and Responses callers therefore receive the first byte later than with token-by-token relay. A monitor-only chain still relays live and scans once at the end.

When every enforcing output guardrail uses window mode, the smallest window-mode buffer limit applies and overflow fails closed unless every contributing guardrail fails open.

When any output guardrail uses buffer_full, only the full-buffer guardrails determine the chain limit and overflow policy. Window-mode settings in the same chain neither tighten nor loosen that limit.

On Chat Completions, the same folded window limit applies to streamed tool-call names and arguments collected for scanning.

Frames AISIX Cannot Scan​

While an enforcing output guardrail holds a stream, AISIX must decode each frame before it can scan or mask the content. Without an enforcing guardrail, Messages and Responses streams relay upstream frames without this hold-back processing.

AISIX resolves each buffered SSE frame before scanning or masking it. A payload is readable when it is one JSON document, empty, or the [DONE] sentinel.

Upstream FrameBehavior
Several data: lines in one frameAISIX joins them with a newline according to the SSE specification, then scans the complete payload.
Final frame without a terminatorAISIX processes and delivers it when the payload is readable or has no content to scan.
Terminated frame that is not one JSON documentAISIX scans the raw text for blocking matches. If it passes, AISIX drops the frame because masking cannot be applied safely.
Final unterminated frame with an unreadable payloadAISIX drops the frame before guardrail evaluation.
Comment, keepalive, empty payload, or [DONE]AISIX preserves it because it carries no content to scan.

When AISIX drops content, it logs the hook, reason, and number of removed frames and bytes. If removal leaves no scannable content, AISIX refuses the response with unscannable_body. A Messages stream also uses this refusal when more than 1 MiB arrives without a frame terminator.

These held-stream refusals ignore the ordinary guardrail failure policy. See Failures While Holding a Stream and Guardrail Refusals.

Buffering Does Not Prove Every Byte Was Scanned

AISIX can preserve a valid JSON event whose schema it does not model. The frame parses, but AISIX extracts no text from it, so the bytes reach the caller unchanged. Hold-back guardrails enforce recognized response text; they do not certify every relayed byte.

Neighboring Endpoint Behavior​

Other streaming endpoints use their own decoding and relay paths, so unreadable frames do not produce one universal outcome:

EndpointUnreadable Stream Behavior
Chat CompletionsRebuilds outgoing chunks from decoded events. Content the decoder cannot read is dropped. A configured streaming-read budget can instead trigger failover and an eventual error.
Messages and ResponsesFollow the upstream response type. A plain JSON response to a streaming request uses the buffered scan and masking path.
Passthrough routesRecognized streams scan their protocol carriers. An opaque SSE stream scans only a bare JSON string. Passthrough guardrails never rewrite the response, so allowed content is relayed unchanged.