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
- Your request is sent to PromptGuard
- PromptGuard scans the input for threats (~150ms)
- If safe, the request is forwarded to the LLM provider
- The LLM provider streams tokens directly back through PromptGuard
- 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.- Python
- Node.js
- cURL
Using the PromptGuard SDK
- Python
- Node.js
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
Whenscan_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.
- Python
- Node.js
- Input is scanned before streaming begins (same as without output scanning)
- Tokens stream to your application in real-time as they arrive
- The SDK accumulates the full response in the background
- After the stream completes, the full response is sent to the Guard API for output scanning
- If the output is flagged, a
PromptGuardBlockedErroris raised after the stream ends