Skip to content

Commit 30d8e85

Browse files
docs: document LLM-as-judge guardrail middleware [AL-469] (#978)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent d6b9227 commit 30d8e85

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

docs/guardrails.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ from uipath_langchain.guardrails import (
4545
UiPathDeterministicGuardrailMiddleware,
4646
UiPathHarmfulContentMiddleware,
4747
UiPathIntellectualPropertyMiddleware,
48+
UiPathLLMAsJudgeMiddleware,
4849
UiPathPIIDetectionMiddleware,
4950
UiPathUserPromptAttacksMiddleware,
5051
PIIDetectionEntity,
@@ -67,9 +68,10 @@ from uipath.core.guardrails import GuardrailScope
6768
| `UiPathHarmfulContentMiddleware` | AGENT, LLM, TOOL | PRE / POST / PRE_AND_POST | `entities`, `tools`, `stage` |
6869
| `UiPathUserPromptAttacksMiddleware` | LLM only | PRE only ||
6970
| `UiPathIntellectualPropertyMiddleware` | AGENT, LLM only | POST only | `entities` |
71+
| `UiPathLLMAsJudgeMiddleware` | AGENT, LLM, TOOL | PRE / POST / PRE_AND_POST | `guardrail_text`, `model`, `threshold`, `positive_examples`, `negative_examples`, `tools`, `stage` |
7072
| `UiPathDeterministicGuardrailMiddleware` | TOOL only | PRE / POST / PRE_AND_POST | `tools`, `rules`, `stage` |
7173

72-
TOOL scope for `UiPathPIIDetectionMiddleware` and `UiPathHarmfulContentMiddleware` requires passing `tools=[...]` to restrict `wrap_tool_call` hooks to specific tools.
74+
TOOL scope for `UiPathPIIDetectionMiddleware`, `UiPathHarmfulContentMiddleware`, and `UiPathLLMAsJudgeMiddleware` requires passing `tools=[...]` to restrict `wrap_tool_call` hooks to specific tools.
7375

7476
All classes share these common parameters:
7577

@@ -83,6 +85,7 @@ Additional parameters per class:
8385
- **`UiPathPIIDetectionMiddleware`** / **`UiPathHarmfulContentMiddleware`**: `entities` (list of entity configs), `tools` (restrict TOOL-scope hooks to specific tools), `stage`.
8486
- **`UiPathUserPromptAttacksMiddleware`**: no extra parameters.
8587
- **`UiPathIntellectualPropertyMiddleware`**: `entities` (list of `IntellectualPropertyEntityType` values).
88+
- **`UiPathLLMAsJudgeMiddleware`**: `guardrail_text` (required — the plain-language rule the judge evaluates against, ≤ 4000 chars), `model` (required — judge model id, e.g. `"gpt-4o-2024-08-06"`; must be a model your governance policy allows for the LLM-as-judge guardrail — LLM Gateway enforces the permitted list), `threshold` (`0` strictest … `6` most lenient, default `2`), `positive_examples` / `negative_examples` (optional calibration payloads — ≤ 2 each, ≤ 1000 chars each), `tools` (required only when `GuardrailScope.TOOL` is used), `stage`. See the [core guardrails documentation](https://uipath.github.io/uipath-python/core/guardrails/#llm-as-judge) for the full parameter reference.
8689
- **`UiPathDeterministicGuardrailMiddleware`**: `tools` (required — list of tools to guard), `rules` (list of lambda functions), `stage`.
8790

8891
### Full example
@@ -98,6 +101,7 @@ from uipath_langchain.guardrails import (
98101
UiPathDeterministicGuardrailMiddleware,
99102
UiPathHarmfulContentMiddleware,
100103
UiPathIntellectualPropertyMiddleware,
104+
UiPathLLMAsJudgeMiddleware,
101105
UiPathPIIDetectionMiddleware,
102106
UiPathUserPromptAttacksMiddleware,
103107
PIIDetectionEntity,
@@ -168,6 +172,18 @@ agent = create_agent(
168172
action=LogAction(severity_level=LoggingSeverityLevel.WARNING),
169173
entities=[IntellectualPropertyEntityType.TEXT],
170174
),
175+
# LLM-as-judge: block responses that violate a plain-language rule (POST)
176+
*UiPathLLMAsJudgeMiddleware(
177+
name="No financial advice",
178+
scopes=[GuardrailScope.AGENT],
179+
action=BlockAction(),
180+
guardrail_text=(
181+
"The response must remain professional and must not contain "
182+
"financial or investment advice."
183+
),
184+
model="gpt-4o-2024-08-06",
185+
stage=GuardrailExecutionStage.POST,
186+
),
171187
# Deterministic: block tool input longer than 1000 chars
172188
*UiPathDeterministicGuardrailMiddleware(
173189
tools=[analyze_text],
@@ -391,6 +407,33 @@ def create_my_agent():
391407
agent = create_my_agent()
392408
```
393409

410+
### LLM-as-judge
411+
412+
Use `LLMAsJudgeValidator` to check content against a plain-language rule via a judge LLM. Scope is inferred from the decorated target (here, the agent factory → AGENT scope). See the [core guardrails documentation](https://uipath.github.io/uipath-python/core/guardrails/#llm-as-judge) for the full parameter reference (`threshold`, `positive_examples`, `negative_examples`, and their limits).
413+
414+
```python
415+
from langchain.agents import create_agent
416+
from uipath_langchain.guardrails import guardrail, LLMAsJudgeValidator, BlockAction, GuardrailExecutionStage
417+
418+
@guardrail(
419+
validator=LLMAsJudgeValidator(
420+
guardrail_text=(
421+
"The response must remain professional and must not contain "
422+
"financial or investment advice."
423+
),
424+
model="gpt-4o-2024-08-06",
425+
threshold=2,
426+
),
427+
action=BlockAction(),
428+
name="No financial advice",
429+
stage=GuardrailExecutionStage.POST,
430+
)
431+
def create_my_agent():
432+
return create_agent(model=llm, tools=[analyze_text], system_prompt="...")
433+
434+
agent = create_my_agent()
435+
```
436+
394437
### LangGraph node
395438

396439
```python

0 commit comments

Comments
 (0)