|
| 1 | +# Handling Fix Results for Streaming in Guardrails |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes how Guardrails handles fix results for streaming, addressing the challenges that arise from the new streaming architecture implemented to reduce latency and cost of validation. |
| 6 | + |
| 7 | +## Fix Actions |
| 8 | + |
| 9 | +Fix actions are a feature in Guardrails that allow you to specify an action to take when a particular validator fails. Some validators support a "FIX" on_fail action, which programmatically corrects faulty LLM output. |
| 10 | + |
| 11 | +### Examples |
| 12 | + |
| 13 | +1. Detect PII Validator: |
| 14 | + |
| 15 | + ``` |
| 16 | + Input: John lives in San Francisco. |
| 17 | + Output: <PERSON> lives in <LOCATION> |
| 18 | + ``` |
| 19 | + |
| 20 | +2. Lowercase Validator: |
| 21 | + ``` |
| 22 | + Input: JOHN lives IN san FRANCISCO. |
| 23 | + Output: john lives in san francisco. |
| 24 | + ``` |
| 25 | + |
| 26 | +In non-streaming scenarios, fix results from one validator can be piped into subsequent validators. |
| 27 | + |
| 28 | +## Challenges with Streaming |
| 29 | + |
| 30 | +The new streaming architecture allows validators to specify how much context to accumulate before running validation. However, this poses challenges for fix actions: |
| 31 | + |
| 32 | +1. Validators cannot run in sequence due to different chunk thresholds. |
| 33 | +2. Each validator accumulates chunks independently. |
| 34 | +3. Validators are unaware of fixes applied by other validators. |
| 35 | + |
| 36 | +## Solution: Merging Algorithm |
| 37 | + |
| 38 | +To address these challenges, Guardrails implements the following solution: |
| 39 | + |
| 40 | +1. Wait for all validators to accumulate enough chunks to validate and output a fix value. |
| 41 | +2. Run a merging algorithm on the fixes from all validators. |
| 42 | + |
| 43 | +### Merging Process |
| 44 | + |
| 45 | +The merging algorithm combines fix outputs from multiple validators. For example: |
| 46 | + |
| 47 | +``` |
| 48 | +LLM Output: JOE is FUNNY and LIVES in NEW york |
| 49 | +PII Fix Output: <PERSON> is FUNNY and lives in <LOCATION> |
| 50 | +Lowercase Fix: joe is funny and lives in new york |
| 51 | +Merged Output: <PERSON> is funny and lives in <LOCATION> |
| 52 | +``` |
| 53 | + |
| 54 | +### Implementation Details |
| 55 | + |
| 56 | +- The merging algorithm is a modified version of the `three-merge` package. |
| 57 | +- It uses Google's `diff-match-patch` algorithm under the hood. |
| 58 | + |
| 59 | +## Limitations and Edge Cases |
| 60 | + |
| 61 | +While the merging algorithm works well for most cases, there are some limitations: |
| 62 | + |
| 63 | +- Edge cases may occur when replacement ranges overlap between multiple validators. |
| 64 | +- In rare instances, the merging algorithm might produce unexpected results. |
| 65 | + |
| 66 | +## Reporting Issues |
| 67 | + |
| 68 | +If you encounter any bugs related to stream fixes, please: |
| 69 | + |
| 70 | +1. File an issue on the [Guardrails GitHub repository](https://github.com/guardrails-ai/guardrails). |
| 71 | +2. Mention @nichwch in the issue description. |
0 commit comments