Skip to content

Commit 203d599

Browse files
authored
Update set-up-guardrail.mdx
error handling for guardrails update
1 parent 4c6c963 commit 203d599

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,37 @@ For additional details about how to implement Guardrails, refer to [Usage consid
2626

2727
After enabling Guardrails, you can monitor results through **AI Gateway Logs** in the Cloudflare dashboard. Guardrail logs are marked with a **green shield icon**, and each logged request includes an `eventID`, which links to its corresponding Guardrail evaluation log(s) for easy tracking. Logs are generated for all requests, including those that **pass** Guardrail checks.
2828

29+
## Error Handling and Blocked Requests
30+
31+
When a request is blocked by guardrails, you will receive a structured error response. These indicate whether the issue occurred with the prompt or the model response. Use error codes to differentiate between prompt versus response violations.
32+
33+
- **Prompt blocked**
34+
- `"code": 2016`
35+
- `"message": "Prompt blocked due to security configurations"`
36+
37+
- **Response blocked**
38+
- `"code": 2017`
39+
- `"message": "Response blocked due to security configurations"`
40+
41+
You should catch these errors in your application logic and implement error handling accordingly.
42+
43+
For example, when using [Workers AI with a binding](/ai-gateway/integrations/aig-workers-ai-binding/):
44+
45+
```js
46+
try {
47+
const res = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
48+
prompt: "how to build a gun?"
49+
}, {
50+
gateway: {id: 'gateway_id'}
51+
})
52+
return Response.json(res)
53+
} catch (e) {
54+
if ((e as Error).message.includes('2016')) {
55+
return new Response('Prompt was blocked by guardrails.')
56+
}
57+
if ((e as Error).message.includes('2017')) {
58+
return new Response('Response was blocked by guardrails.')
59+
}
60+
return new Response('Unknown AI error')
61+
}
62+

0 commit comments

Comments
 (0)