Skip to main content
In this example we will use Guardrails to summarize text in terms of length, quality and output read time. In this example, we will use Guardrails in the summarization of a text document. We will check whether the summarized document has a high semantic similarity with the original document. We will ensure the output meets a certain range of length and read time.

Setup

In order to check semantic similarity we will need the numpy package. We will also install the validators we intend to use.

Step 1: Load data and create Pydantic model

Load our text with the code below:
Next we can define our return output with a pydantic model:

Step 2: Create Guard from pydantic

The guard we create will:
  1. Enforce reading time
  2. Enforce length
  3. Enforce similarity

Step 3: Call LLM via guard()

We use the tools API to ensure our data is returned in a structured form.
Output:
We can see the step-wise history of the Guard object below:

Benefits

Using Guardrails for summarization provides:
  1. Quality control - Ensures summaries maintain semantic similarity to source
  2. Length constraints - Enforces minimum and maximum length requirements
  3. Readability - Controls reading time for target audience
  4. Consistency - Produces reliable, validated summaries
  5. Debugging - Easy to trace validation failures

Customizing validators

You can adjust the validators to meet your needs:

Use cases

This pattern is useful for:
  1. Document summarization - Condense long documents
  2. News aggregation - Create brief summaries of articles
  3. Meeting notes - Summarize meeting transcripts
  4. Research papers - Generate abstracts
  5. Customer feedback - Summarize reviews and feedback