guard() to evaluate a guardrails policy before sending content to users or downstream systems.
For a full explanation of guardrail types (PII, moderation, jailbreak, NSFW, URL filter, etc.) and when to use each, see Policies: Guardrails.
Policies: Guardrails
Learn about guardrail types and how to configure them.
guard(policyKeyOrTag, input)
Evaluates a guardrails policy on a string. input is the text to check (e.g. LLM response or user message).
Signature
| Parameter | Type | Required | Description |
|---|---|---|---|
policyKeyOrTag | string | Yes | Policy key (e.g. 'content-guardrail') or tag (e.g. '#safety'). |
input | string | Yes | Text to check (e.g. model output). |
Promise<PolicyEvaluationResult>. See Result shape below.
Throws: InvalidPolicyKeyError, InvalidInputError, APIRequestError, NetworkError, TimeoutError.
Policy key or tag
- Policy key — A single guardrails policy, e.g.
'content-guardrail'. - Tag — All policies with that tag are evaluated. Use the tag name with a
#prefix, e.g.'#safety'.
Examples
Basic usage
By tag
In middleware (Node/Express)
Result shape
The API returns a response withmeta and result; the SDK normalizes this to the PolicyEvaluationResult shape below. guard() returns the same PolicyEvaluationResult as check() and evaluate():
| Field | Type | Description |
|---|---|---|
data.action | 'ALLOW' | 'BLOCK' | 'ESCALATE' | Decision. |
data.reason | string | Human-readable reason. |
isAllowed | boolean | data.action === 'ALLOW'. |
isBlocked | boolean | data.action === 'BLOCK'. |
isEscalated | boolean | data.action === 'ESCALATE'. |
errors | string[] | undefined | Validation errors from the API when present. |
Error handling
| Error class | When it is thrown |
|---|---|
InvalidAPIKeyError | Key missing or doesn’t start with sk_. |
InvalidPolicyKeyError | Policy key/tag empty or invalid. |
InvalidInputError | Empty string input. |
APIRequestError | API returned 4xx/5xx (error.statusCode, error.response). |
NetworkError / TimeoutError | Request failed or timed out. |