Handle streaming output guardrails for client-side decisions
Learn how to consume sync output guardrail results from streaming responses and implement client-side retry, fallback, and error handling logic.
When you enable output guardrails on streaming requests, Portkey accumulates the full response after the stream completes, runs your guardrail checks, and sends the results as an additional SSE chunk. Unlike non-streaming requests, no server-side action is taken — retry, fallback, and deny are not triggered automatically. The results are informational, giving you full control to handle them on the client side.This guide walks through capturing those results and building client-side handling patterns like retry, fallback, and content filtering.
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 result chunks are not visible.
Add your output guardrail to the config using afterRequestHooks (or output_guardrails in a saved config). Set stream: true and disable strict OpenAI compliance.
Each guardrail in after_request_hooks returns a verdict (boolean) and an array of checks. A guardrail’s overall verdict is true only when all its checks pass.
def evaluate_guardrail_results(guardrail_results): """Returns True if all guardrails passed, False otherwise.""" if not guardrail_results: return True # No results means guardrails didn't run for guardrail in guardrail_results: if not guardrail.get("verdict"): # At least one guardrail failed failed_checks = [ check["id"] for check in guardrail.get("checks", []) if not check.get("verdict") ] print(f"Guardrail '{guardrail['id']}' failed. " f"Failed checks: {failed_checks}") return False return True
The hook_results chunk for output guardrails looks like this:
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-compliance to false to receive the hook_results chunk. This header defaults to true.
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 /messages endpoint: Hook results cannot be accessed through the Anthropic SDK. Use cURL or raw HTTP requests for that endpoint.