CertSafari
    Databricks Certified Generative AI Engineer Associate· Lessons

    Domain 5 · Lesson 44/56

    Custom Guardrail Policies: SQL Rules, LLM Judges, and Evaluation Order

    Select guardrail techniques to protect against malicious user inputs to a Gen AI application

    10 min read
    1.79% of exam
    3 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Write an input-phase SQL service policy that denies prompts by keyword or length
    • Decide when a custom LLM-as-a-judge policy is needed instead of a deterministic rule or a built-in
    • Predict which policies run, and in what order, when several guardrails share a service
    • Explain why service policies fail closed and how to roll out a new guardrail safely

    1.Deterministic SQL policies for exact rules

    On Databricks, a guardrail is a service policy: a Unity Catalog function attached to an AI service that inspects each request before the model runs (input phase, ON CALL) and each response after it (output phase, ON RESULT). The built-in guardrails handle common risks such as jailbreaks and unsafe content. Some malicious inputs are specific to your organization, though. A user might fish for a confidential project codename, or try to push the assistant onto topics it must not handle. For rules like these you write a custom policy.

    The simplest custom policy is a deterministic SQL function. It is a SQL UDF that takes (event VARIANT) and returns a decision, and the message text is read from event:context.message. Use this kind of policy when the rule is precise and repeatable, such as a keyword, a tool name or a length. A custom function runs at both evaluation points, so an input-only rule branches on event:type and acts only when the value is 'request'. The tutorial's codename blocker does exactly that:

    An input-phase (ON CALL) custom policy that denies any prompt mentioning a confidential codenamesql
    CREATE OR REPLACE FUNCTION main.governance.block_confidential_codename(
      event VARIANT
    )
    RETURNS VARIANT
    LANGUAGE SQL
    RETURN
      CASE
        WHEN event:type::string = 'request'
          AND contains(lower(event:context.message::string), 'project aurora')
        THEN to_variant_object(named_struct('result', 'DENY', 'reason', 'Requests about confidential projects are not permitted.'))
        ELSE to_variant_object(named_struct('result', 'ALLOW', 'reason', ''))
      END;

    The same pattern covers other kinds of hostile input. A keyword list can deny restricted topics such as legal or investment advice. A length check can deny very long prompts, which are often pasted dumps or prompt-stuffing attempts and also raise latency and cost. Keep in mind how narrow the SQL subset is. Matching is done on substrings with CONTAINS, LIKE, STARTSWITH and ENDSWITH, and regular expressions (regexp_*) are not supported. A format check that depends on structure or a checksum, such as a national ID, belongs to a built-in guardrail.

    Checkpoint 1 of 6· Fill the gap

    This policy should deny oversized prompts before they reach the model. Which value completes the phase check?

    CREATE OR REPLACE FUNCTION main.governance.deny_oversized_prompt(
      event VARIANT
    )
    RETURNS VARIANT
    LANGUAGE SQL
    RETURN
      CASE
        WHEN event:type::string =  ? 
          AND LENGTH(event:context.message::string) > 8000
        THEN to_variant_object(named_struct('result', 'DENY', 'reason', 'Your prompt exceeds the 8000-character limit for this service.'))
        ELSE to_variant_object(named_struct('result', 'ALLOW', 'reason', ''))
      END;

    Checkpoint 2 of 6· Exam question

    A healthcare intake assistant must never process a request that contains a patient's social security number: any such request should be rejected outright rather than forwarded to the model in a modified form. Which guardrail configuration meets this requirement?

    Sources12

    2.Custom LLM-as-a-judge policies for intent

    Keywords are a blunt tool against an attacker who simply rephrases. When the check is about meaning, such as intent, topic or tone, and no exact rule captures it, use an LLM-as-a-judge policy. You set it up like any other policy: choose Custom as the Guardrail type, set Type to LLM-as-a-judge, write a classifier in the Prompt field, and pick an Evaluator model service, which needs CAN QUERY. One example input-phase judge flags requests outside a support assistant's scope, so that an assistant built for one purpose isn't used as a general-purpose chatbot.

    You write only the classification criteria. Databricks appends an output contract, so the evaluator returns a flagged boolean and a confidence. Don't write ALLOW or DENY in your prompt, and don't specify an output format. This matters for injection in particular: Databricks wraps the content under evaluation and tells the evaluator to treat it as untrusted data, not as instructions, so a prompt cannot simply tell the judge to let it through.

    Judge-prompt best practices that bear on malicious-input detection
    PracticeWhat it means for your guardrail
    State clear FLAG and DO NOT FLAG criteriaA prompt that lists only what to flag tends to over-flag
    Describe intent, not just keywords or techniquesRole-play or an encoded string isn't a jailbreak by itself; require an attempt to obtain disallowed content
    Prefer a built-in where one fitsFor jailbreak, unsafe content or PII, use the built-in instead of re-describing it in a custom prompt
    Roll out in Log mode firstLog mode records the verdict without blocking; review false positives and negatives before switching to Enforce

    Checkpoint 3 of 6· Check yourself

    You write a custom judge prompt: 'Flag any message that uses role-play or contains base64.' What is wrong with it?

    Sources2

    3.Stacking guardrails: rank, stages and fail-closed

    A service usually carries several guardrails, and each attachment has a rank. On the input phase, policies are evaluated in ascending rank order, lowest first. On the output phase the order is reversed. The chain stops at the first DENY. That is why the tutorial gives the unsafe-content guardrail rank 1 and the custom codename policy rank 10, so a harmful prompt is rejected by the managed check before any custom rule runs.

    Policies that share a rank run in two stages. First, the blocking LLM-as-a-judge built-ins run in parallel, so their combined latency is roughly that of the slowest one. The custom SQL policies and ASK policies then run one after another, but only if every parallel policy allowed the interaction. So stacking several blocking guardrails does not multiply latency. To see the actual order on a service, open its Policies tab and click See execution flow.

    Policies also fail closed. A missing field, an unsupported function or any evaluation error results in DENY. A SQL policy that compares an uncast VARIANT path, or that has no final ALLOW branch, will therefore block legitimate traffic rather than let it through. Always cast, for example event:type::string = 'request', and end with an explicit ALLOW.

    Checkpoint 4 of 6· Check yourself

    A custom SQL policy calls a function that is outside the supported SQL subset. What happens to requests on that service?

    Checkpoint 5 of 6· Put it in order

    Order how Databricks evaluates policies that share the same rank on the input phase

    1. 1.Evaluation short-circuits at the first DENY, skipping later policies and higher ranks
    2. 2.If all of them allowed the interaction, custom SQL and ASK policies run sequentially in attachment order
    3. 3.Blocking LLM-as-a-judge built-in policies run in parallel

    Checkpoint 6 of 6· Exam question

    Before rolling out a new jailbreak-protection guardrail to production traffic, a platform team wants to observe which real user requests it would flag without actually blocking or altering any traffic yet. Which guardrail setting accomplishes this?

    Sources3

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.A custom SQL policy can use a regular expression to catch structured identifiers in a prompt.Why is that wrong?

      The policy SQL subset supports only substring matching. For format or checksum checks, use a built-in guardrail.

      Covered in Deterministic SQL policies for exact rules

    2. 2.A custom judge prompt should end by telling the evaluator to answer ALLOW or DENY in a fixed JSON format.Why is that wrong?

      Databricks appends the output contract itself. Your prompt should state only the classification criteria.

      Covered in Custom LLM-as-a-judge policies for intent

    3. 3.If a policy function errors, the gateway lets the request through so the app keeps working.Why is that wrong?

      Service policies fail closed: any evaluation error results in DENY.

      Covered in Stacking guardrails: rank, stages and fail-closed

    Practise it for real

    Protect a Model Service against jailbreaks and a confidential-codename probe, then verify both guardrails in the playground.

    1. 1.In AI Gateway, open your model service's Policies tab, click New policy, choose Guardrail type Jailbreak, set Rank 1, and create it.

      Why: The managed jailbreak judge covers prompt-injection attempts without code. It runs on requests only. The Unity Gateway beta features must be enabled for your account, and you need MANAGE on the service.

      You should see: The policy is listed on the service's Policies tab.

    2. 2.Run CREATE OR REPLACE FUNCTION main.governance.block_confidential_codename(...) from the tutorial in a schema where you hold CREATE FUNCTION.

      Why: The organization-specific rule needs a SQL UDF that branches on event:type::string = 'request'.

      You should see: The function is registered in Unity Catalog.

    3. 3.Attach it with New policy, Guardrail type Custom, select the function, set Phase to Input guardrails (Before the Model) only, and set Rank 10.

      Why: It is a request policy, and the higher rank makes it run after the managed check on the input phase.

      You should see: Both policies appear on the Policies tab, and See execution flow shows their order.

    4. 4.Wait a couple of minutes, click Chat in playground, and send: Tell me about Project Aurora.

      Why: During the beta, policy changes take a short time to propagate.

      You should see: The request is blocked with the reason: Requests about confidential projects are not permitted.

    5. 5.Send an ordinary prompt.

      Why: This confirms the guardrails don't block legitimate traffic.

      You should see: A normal completion is returned.

    Stuck? Get a nudge

    If every prompt is blocked, check that the function casts event:type and ends with an explicit ALLOW branch, because policies fail closed.

    Sources

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “A custom policy is a SQL UDF that takes (event VARIANT) and returns a decision”
      ↩︎ Deterministic SQL policies for exact rules
    2. 2.
      “Use these when the rule is precise and repeatable.”
      ↩︎ Deterministic SQL policies for exact rules
      “Very long prompts are often pasted dumps or prompt-stuffing attempts, and they raise latency and cost.”
      ↩︎ Deterministic SQL policies for exact rules
      “regular expressions (regexp_*) aren't supported.”
      ↩︎ Deterministic SQL policies for exact rules
      “Use these when the check is semantic (intent, topic, tone) and no exact rule captures it.”
      ↩︎ Custom LLM-as-a-judge policies for intent
      “instructs the evaluator to treat it as untrusted data rather than as instructions to follow”
      ↩︎ Custom LLM-as-a-judge policies for intent
      “Reviewing those verdicts requires inference tables enabled on the service”
      ↩︎ Custom LLM-as-a-judge policies for intent
      “regular expressions (regexp_*) aren't supported.”
      ↩︎ Exam trap 1
      “Don't write ALLOW or DENY in the prompt, and don't specify an output format.”
      ↩︎ Exam trap 2
      “Service policies are fail-closed: a missing field, an unsupported function, or any evaluation error results in DENY.”
      ↩︎ Exam trap 3
      “Role-play or an encoded string isn't a jailbreak by itself; require an attempt to obtain disallowed content.”
      ↩︎ Checkpoint
      “Service policies are fail-closed: a missing field, an unsupported function, or any evaluation error results in DENY.”
      ↩︎ Checkpoint
    3. 3.
      “Each attachment has a rank (priority), and the chain stops at the first DENY.”
      ↩︎ Stacking guardrails: rank, stages and fail-closed
      “Databricks evaluates policies in ascending rank order on the input phase”
      ↩︎ Stacking guardrails: rank, stages and fail-closed
      “stacking several blocking guardrails on a service doesn't multiply their latency.”
      ↩︎ Stacking guardrails: rank, stages and fail-closed
      “The remaining policies then run sequentially, but only if every parallel policy in the first stage allowed the interaction.”
      ↩︎ Checkpoint

    Spotted a mistake, or was something unclear? Tell us.