Skip to main content

Setup

Install and import the necessary validators

1. For structured JSON output

Define the prompt and output schema

Create the Guard object

Example 1: No streaming
By default, the stream parameter is set to False
/Users/dtam/dev/guardrails/guardrails/validator_service/_init_.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.
warnings.warn(
Screenshot2026 02 02222805 Screenshot2026 02 02222826
Example 2: Streaming
Set the stream parameter to True
Screenshot2026 02 02223222 Screenshot2026 02 02223245 As you can see here, the outputs in both examples match. The only difference is that, in the streaming example, the outputs are returned as soon as they are received and validated by Guardrails. In the non-streaming example, the outputs are returned only after the entire request has been processed by the API. In other words, when streaming is enabled, the API returns the outputs as soon as they are ready, rather than waiting for the entire request to be processed.

2. For unstructured text output

Define the prompt and Guard object with validators

Example 1: No streaming

By default, the stream parameter is set to False
/Users/dtam/dev/guardrails/guardrails/validator_service/_init_.py:85: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.
warnings.warn(

Example 2: With streaming

Set the stream parameter to True
Screenshot2026 02 02223937 As you can see, the outputs in both examples match. The only difference is that, in the streaming example, the outputs are returned as soon as they are received and validated by Guardrails. In the non-streaming example, the outputs are returned only after the entire request has been processed by the API. In other words, when streaming is enabled, the API returns the outputs as soon as they are ready, rather than waiting for the entire request to be processed.