Repository navigation
feat: add per-tool CEL expression rail #2430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aedcd48
feat: add per-tool CEL expression rail
tanushriya910 0b6203c
fix: fail closed on any CEL expression failure and address review com…
tanushriya910 5bf42f1
docs: note the server's current tools restriction
tanushriya910 9990c64
fix: address CEL review comments
tanushriya910 d1ca6a8
fix(cel): fail closed when a tool has no CEL expressions
tanushriya910 89e2305
docs(cel): describe fail-closed behavior for tools without expressions
tanushriya910 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| --- | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| title: "Common Expression Language (CEL)" | ||
| sidebar-title: "Common Expression Language (CEL)" | ||
| description: "Reference for the per-tool rail that blocks tool calls or tool results matching Common Expression Language (CEL) expressions you write per tool." | ||
| content: | ||
| type: "reference" | ||
| --- | ||
|
|
||
| The CEL rail checks a tool call's arguments, or a tool result's content, against [Common Expression Language (CEL)](https://cel.dev) expressions that you write per tool. CEL is a small, side-effect-free expression language used for policy rules in systems such as Kubernetes admission policies and Envoy. Each expression evaluates to `true` when the tool is being used in a way you want to block. | ||
|
|
||
| Unlike [Regex Detection](/configure-guardrails/guardrail-catalog/community/regex), which matches text patterns, CEL expressions address the structured fields of a tool call directly, so one rule can combine several arguments. Unlike [Tool Safety Check](/configure-guardrails/guardrail-catalog/tool-safety-check), the check is deterministic and calls no model. It is scoped per tool through the same `rails.tool_output.per_tool` and `rails.tool_input.per_tool` dispatch mechanism as both. | ||
|
|
||
| ## Installation | ||
|
|
||
| The CEL rail requires the optional `cel` extra, which installs the [`common-expression-language`](https://pypi.org/project/common-expression-language/) package and is available on Python 3.11 and later: | ||
|
|
||
| ```bash | ||
| pip install "nemoguardrails[cel]" | ||
| ``` | ||
|
|
||
| If the extra is not installed, a configuration that uses the CEL rail fails to load with an error naming the extra. | ||
|
|
||
| ## Configure the Rail | ||
|
|
||
| To use the CEL rail, you should: | ||
|
|
||
| 1. Write the expressions for each tool in the `rails.config.cel` section of the `config.yml` file, under `tool_output` to check tool calls and `tool_input` to check tool results: | ||
|
|
||
| ```yaml | ||
| rails: | ||
| config: | ||
| cel: | ||
| tool_output: | ||
| run_shell: | ||
| expressions: | ||
| - 'args.command.matches("(?i)\\brm\\s+-[a-z]*(r[a-z]*f|f[a-z]*r)")' | ||
| write_file: | ||
| expressions: | ||
| - 'args.path.startsWith("/etc/") || args.path.startsWith("/root/")' | ||
| - 'has(args.mode) && args.mode == "append" && args.path.endsWith(".bashrc")' | ||
| tool_input: | ||
| run_shell: | ||
| expressions: | ||
| - 'type(result) == string && result.contains("BEGIN RSA PRIVATE KEY")' | ||
| ``` | ||
|
|
||
| 2. Scope the check to the tool in the `tool_output` and `tool_input` rails sections of the `config.yml` file: | ||
|
|
||
| ```yaml | ||
| rails: | ||
| tool_output: | ||
| per_tool: | ||
| run_shell: | ||
| - "cel check tool output" | ||
| write_file: | ||
| - "cel check tool output" | ||
| tool_input: | ||
| per_tool: | ||
| run_shell: | ||
| - "cel check tool input" | ||
| ``` | ||
|
|
||
| The `cel check tool output` flow evaluates the tool's `tool_output` expressions against each call to the tool, and the `cel check tool input` flow evaluates its `tool_input` expressions against each result the tool returns. A tool without a CEL flow under `per_tool` is not checked by the rail, but a tool whose CEL flow has no expressions configured for its direction fails closed. | ||
|
|
||
| 3. Declare the tool in the main model's `parameters.tools`, or per request: in `options.llm_params.tools` with the Python API, or in the `tools` field of a chat completions request to the server, which the server currently accepts only for non-streaming requests on a configuration with `passthrough: true`. `cel check tool output` blocks a call to an undeclared tool before any expression runs. Refer to [Tool Calling](/configure-guardrails/guardrail-catalog/tool-calling) for the full declaration reference. | ||
|
|
||
| The `cel check tool output` and `cel check tool input` rails execute the [`evaluate_tool_output_cel`](https://github.com/NVIDIA-NeMo/Guardrails/blob/develop/nemoguardrails/library/cel/actions.py) and [`evaluate_tool_input_cel`](https://github.com/NVIDIA-NeMo/Guardrails/blob/develop/nemoguardrails/library/cel/actions.py) actions respectively. | ||
|
|
||
| ## Expression Variables | ||
|
|
||
| Each expression can read the following variables: | ||
|
|
||
| | Flow | Variable | Value | | ||
| |---|---|---| | ||
| | `cel check tool output` | `args` | The tool call's arguments, as a map | | ||
| | | `tool` | The tool's name | | ||
| | `cel check tool input` | `result` | The tool result's content, exactly as the tool returned it: a string, a list of content blocks, or `null` | | ||
| | | `args` | The arguments of the tool call that the result answers | | ||
| | | `tool` | The tool's name | | ||
|
|
||
| Nested arguments are available at any depth, for example `args.options.recursive` or `args.files.exists(f, f.path.startsWith("/etc"))`. | ||
|
|
||
| A tool result that contains JSON text is still a string, so check it with string functions such as `contains()` and `matches()`. | ||
|
|
||
| ## How Expressions Are Evaluated | ||
|
|
||
| The rail evaluates every expression configured for the tool: | ||
|
|
||
| - If any expression evaluates to `true`, the rail blocks the tool call or result. The block lists the expressions that matched in its `matched_expressions` metadata. The metadata never includes the checked arguments or content. | ||
| - If no expression evaluates to `true`, the rail allows the tool call or result. | ||
| - If any expression fails to evaluate, for example because it reads an argument the call did not include, or it returns a value that is not a boolean, the rail fails closed, even if another expression matched. The tool call or result is blocked and reported as a rail failure rather than a policy decision, and the logged error lists each expression that failed with its error type. | ||
| - If the tool has no expressions configured for the direction its flow runs in, the rail also fails closed, since there is nothing to validate the tool call or result against. | ||
|
|
||
| ## Write Expressions | ||
|
|
||
| Expressions use [CEL syntax](https://github.com/google/cel-spec/blob/master/doc/langdef.md), including comparisons, `&&` and `||`, the `in` operator, the `exists()` and `all()` macros, and string functions such as `contains()`, `startsWith()`, `endsWith()`, and `matches()`, which uses RE2 regular expression syntax. The rail also provides the cel-go extension functions, such as `lowerAscii()`, `split()`, and `lists.range()`. | ||
|
|
||
| When you write expressions, keep the following in mind: | ||
|
|
||
| - Guard optional arguments with `has()`, for example `has(args.mode) && args.mode == "append"`. Without the guard, a call that omits the argument fails to evaluate. | ||
| - If a tool can return either a string or a list of content blocks, check the type first, for example `type(result) == string && result.contains("password")`. | ||
| - Each expression must evaluate to a boolean. An expression such as `args.command` that returns a string fails to evaluate. | ||
|
|
||
| The rail checks each expression's syntax when the configuration loads, so a syntax error fails the load. Everything else is checked when an expression is evaluated: the CEL library the rail uses resolves variable and function names only at evaluation, and `args` and `result` hold the values of each tool call or result, so whether a field exists or has the expected type is known only then. A mistake found at evaluation makes the rail fail closed, so test your expressions against representative tool calls and results before deploying them. | ||
|
|
||
| ## Complete Example | ||
|
|
||
| The following configuration blocks recursive forced deletes in `run_shell`, writes to `/etc` or `/root` and appends to `.bashrc` through `write_file`, and any `run_shell` result that contains a private key: | ||
|
tanushriya910 marked this conversation as resolved.
|
||
|
|
||
| <Warning> | ||
|
|
||
| The expressions on this page illustrate how the rail works; they are not complete security policies. Write and test expressions for the actual threats your tools face. | ||
|
|
||
| </Warning> | ||
|
|
||
| ```yaml | ||
| models: | ||
| - type: main | ||
| engine: nim | ||
| model: meta/llama-3.3-70b-instruct | ||
|
|
||
| rails: | ||
| config: | ||
| cel: | ||
| tool_output: | ||
| run_shell: | ||
| expressions: | ||
| - 'args.command.matches("(?i)\\brm\\s+-[a-z]*(r[a-z]*f|f[a-z]*r)")' | ||
| write_file: | ||
| expressions: | ||
| - 'args.path.startsWith("/etc/") || args.path.startsWith("/root/")' | ||
| - 'has(args.mode) && args.mode == "append" && args.path.endsWith(".bashrc")' | ||
| tool_input: | ||
| run_shell: | ||
| expressions: | ||
| - 'type(result) == string && result.contains("BEGIN RSA PRIVATE KEY")' | ||
| tool_output: | ||
| per_tool: | ||
| run_shell: | ||
| - "cel check tool output" | ||
| write_file: | ||
| - "cel check tool output" | ||
| tool_input: | ||
| per_tool: | ||
| run_shell: | ||
| - "cel check tool input" | ||
| ``` | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| <Note> | ||
|
|
||
| This configuration doesn't declare `run_shell` or `write_file`. Declare them in the main model's `parameters.tools`, or per request: in `options.llm_params.tools` with the Python API, or in the `tools` field of a chat completions request to the server, which the server currently accepts only for non-streaming requests on a configuration with `passthrough: true`. Otherwise `cel check tool output` blocks every call to them before any expression runs. Refer to [Tool Calling](/configure-guardrails/guardrail-catalog/tool-calling) for the declaration format. | ||
|
|
||
| </Note> | ||
|
|
||
| ## Schema Validation | ||
|
|
||
| `cel check tool output` runs behind `@tool_output_validation`: a call to a tool the model has not declared, or whose arguments violate the tool's declared JSON schema, is blocked before any expression is evaluated. `cel check tool input` has no such gate, matching regex detection's asymmetry between the two directions. | ||
|
|
||
| ## Engine Support | ||
|
|
||
| | Flow | LLMRails | IORails | | ||
| |---|:---:|:---:| | ||
| | `cel check tool output` | ✗ | ✓ | | ||
| | `cel check tool input` | ✗ | ✓ | | ||
|
|
||
| Per-tool checks are an `IORails`-only capability by design. For the full per-rail matrix, refer to [Rail Engine Support](/reference/rail-engine-support). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.