Skip to main content
PromptGuard fully supports streaming responses. Security scanning happens on the input before the request is forwarded, so streaming adds no additional latency to token delivery.

How Streaming Works

  1. Your request is sent to PromptGuard
  2. PromptGuard scans the input for threats (~150ms)
  3. If safe, the request is forwarded to the LLM provider
  4. The LLM provider streams tokens directly back through PromptGuard
  5. Tokens arrive in real-time as they’re generated

Using the OpenAI SDK

The simplest way to stream — works with your existing OpenAI/Anthropic code.

Using the PromptGuard SDK

Server-Sent Events (SSE)

When streaming, the API returns Server-Sent Events. Each event contains a JSON chunk:

Framework Integration

FastAPI (Python)

Express (Node.js)

Next.js (React)

Error Handling During Streaming

Errors during streaming are delivered as SSE events:
Security blocks happen before streaming begins (during input scanning). If a request passes the security check, the stream will complete normally. You won’t receive a mid-stream security block.

Streaming Output Guardrails

When scan_responses (Python) or scanResponses (Node.js) is enabled with auto-instrumentation, PromptGuard also scans the completed output after streaming finishes. The SDK buffers the full response internally and sends it to the Guard API with direction="output" once the stream ends.
How it works:
  1. Input is scanned before streaming begins (same as without output scanning)
  2. Tokens stream to your application in real-time as they arrive
  3. The SDK accumulates the full response in the background
  4. After the stream completes, the full response is sent to the Guard API for output scanning
  5. If the output is flagged, a PromptGuardBlockedError is raised after the stream ends
Because output scanning happens after the full stream is received, your application will have already displayed the tokens to the user by the time a block is triggered. Design your UI to handle post-stream blocks gracefully — for example, by clearing the displayed response or showing a warning banner.

Performance

Streaming is recommended for all user-facing applications. The perceived latency is significantly lower because users see tokens appear in real-time rather than waiting for the full response.