From 203d59928c82c3f7213942fd4d6444ed99d71341 Mon Sep 17 00:00:00 2001 From: Kathy <153706637+kathayl@users.noreply.github.com> Date: Fri, 2 May 2025 09:55:47 -0700 Subject: [PATCH 1/2] Update set-up-guardrail.mdx error handling for guardrails update --- .../guardrails/set-up-guardrail.mdx | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx b/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx index f2e187ccba23b78..930b59c27598aa9 100644 --- a/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx +++ b/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx @@ -26,3 +26,37 @@ For additional details about how to implement Guardrails, refer to [Usage consid 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. +## Error Handling and Blocked Requests + +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. + +- **Prompt blocked** + - `"code": 2016` + - `"message": "Prompt blocked due to security configurations"` + +- **Response blocked** + - `"code": 2017` + - `"message": "Response blocked due to security configurations"` + +You should catch these errors in your application logic and implement error handling accordingly. + +For example, when using [Workers AI with a binding](/ai-gateway/integrations/aig-workers-ai-binding/): + +```js +try { + const res = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', { + prompt: "how to build a gun?" + }, { + gateway: {id: 'gateway_id'} + }) + return Response.json(res) +} catch (e) { + if ((e as Error).message.includes('2016')) { + return new Response('Prompt was blocked by guardrails.') + } + if ((e as Error).message.includes('2017')) { + return new Response('Response was blocked by guardrails.') + } + return new Response('Unknown AI error') +} + From ce5d0e26b2fba5675f8e74a266745915091ac800 Mon Sep 17 00:00:00 2001 From: daisyfaithauma Date: Mon, 5 May 2025 20:26:53 +0100 Subject: [PATCH 2/2] Update set-up-guardrail.mdx --- src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx b/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx index 930b59c27598aa9..ca721bace853af6 100644 --- a/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx +++ b/src/content/docs/ai-gateway/guardrails/set-up-guardrail.mdx @@ -22,11 +22,11 @@ Add Guardrails to any gateway to start evaluating and potentially modifying resp For additional details about how to implement Guardrails, refer to [Usage considerations](/ai-gateway/guardrails/usage-considerations/). ::: -## Viewing Guardrail Results in Logs +## Viewing Guardrail results in Logs 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. -## Error Handling and Blocked Requests +## Error handling and blocked requests 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.