Prerequisites
- A Portkey account with an API key
- An output guardrail created in the Portkey dashboard (e.g., sentence count, word count, regex match, or any custom check)
- The guardrail’s ID (visible after saving it in the dashboard)
To receive
hook_results in streaming chunks, you must set the x-portkey-strict-open-ai-compliance header to false. Without this, guardrail is executed but the result chunks are not sent to be compliant with the spec.How streaming output guardrails work
- You send a streaming request with output guardrails configured
- Portkey streams the LLM response chunks to you normally
- After the final
[DONE]chunk, Portkey sends one additional chunk containinghook_resultswith theafter_request_hooksarray - Your client can inspect the verdict and decide what to do — retry, fall back to another model, filter the response, or surface a warning
Step 1: Configure the request
Add your output guardrail to the config usingafterRequestHooks (or output_guardrails in a saved config). Set stream: true and disable strict OpenAI compliance.
Step 2: Capture guardrail results from the stream
Iterate over the stream, collect content chunks, and extracthook_results from the final chunk.
Step 3: Inspect the verdict
Each guardrail inafter_request_hooks returns a verdict (boolean) and an array of checks. A guardrail’s overall verdict is true only when all its checks pass.
hook_results chunk for output guardrails looks like this:
Client-side handling patterns
Since streaming output guardrails are informational only, you implement the handling logic yourself. Here are common patterns:Pattern 1: Client-side retry
Re-send the request when the guardrail fails, optionally with a modified prompt to steer the model toward compliant output.Pattern 2: Client-side fallback to another model
If the primary model’s output fails guardrails, fall back to a different model.Pattern 3: Conditional content filtering
Show or hide the streamed response based on specific check results.Pattern 4: Logging and observability
Log guardrail outcomes for monitoring without blocking the response.Combining patterns
You can chain these patterns together for robust handling:Important considerations
- Output guardrails on streaming are informational only. Portkey does not trigger server-side retry, fallback, or deny for output guardrails in streaming mode. You must handle these on the client side.
- Set
x-portkey-strict-open-ai-compliancetofalseto receive thehook_resultschunk. This header defaults totrue. - Async guardrails do not return results in the response. If your guardrail is configured with
async=true, results appear only in Portkey logs, not in the stream. - The full response is evaluated. Portkey assembles all stream chunks and runs checks on the complete text, so partial-stream checks are not supported.
- Anthropic
/messagesendpoint: Hook results cannot be accessed through the Anthropic SDK. Use cURL or raw HTTP requests for that endpoint.
Next steps
- Guardrails overview — Full reference for configuring guardrail checks and actions
- List of guardrail checks — All available built-in and partner checks
- Bring your own guardrails — Integrate custom guardrail logic via webhooks

