Create a processing rule
A processing rule is a Python script that picks which step processes each file a Knowledge receives. It decides from the file's name, type, content, source or any other condition you can write in code. The rule only routes the file, and the step it picks does the work.
A Knowledge uses processing rules when its Default workflow is the Classic pipeline. If the Knowledge runs a workflow graph instead, the graph decides what happens to each file and the rule isn't used.
Before you begin
- Role: Create and Update on the Workflows row of your role. See Manage roles and users.
- The steps the rule routes to. Each must exist and be switched on. See Create a workflow.
Create the rule
- Go to Settings → Workflows and select the Processing rules tab.
- Select New, then From scratch. On an empty page, select Add new. Import JSON loads a rule exported from another environment instead.
- Replace Untitled processing rule with a name, for example
Route by file type. - Write the rule's code in the editor. The next section has an example.
- Select Save.
Processing rules have no switch: a rule runs whenever a Knowledge or one of its sources uses it. The rule's ⋮ menu holds Download (the .py file), Export (JSON), and Delete. Deleting a rule keeps the runs that used it.
Write the rule code
The rule returns the name of the step to run, exactly as it appears in the Steps list. This rule sends PDF files to one step, Word files to another, and skips everything else:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rational-client",
# ]
# ///
from rational_client.core import File
from rational_client.utils import run
def router(resource: File, options: dict):
name = (resource.name or "").lower()
if name.endswith(".pdf"):
return "PDF extraction"
if name.endswith((".doc", ".docx")):
return "Word extraction"
return None
run(router)
run(router)callsrouterwith the file and the rule's options.- Return a step name to run that step on the file. The step must exist and be switched on.
- Return
Noneto leave the file without a step. Nothing is extracted from it. - To also pass options to the step, return a dictionary instead:
{"workflow": "PDF extraction", "options": {...}}.
Use the rule in a Knowledge
- Go to Settings → Knowledge, open the Knowledge, and select the Sources tab.
- Leave Default workflow set to Classic pipeline.
- In Default processing rule, select your rule.
- Select Save.
To route one attached source differently, open that source's ⋮ menu, select Data processing settings, and pick its Processing rule. A source's own rule takes precedence over the Knowledge's default.
The rule runs on files added after you save. To apply it to the files already in the Knowledge, select Reprocess knowledge in the Knowledge's Options menu. See Add and manage content in a Knowledge.
Next steps
- Create a workflow to write the steps a rule routes to.
- Import, export, and set timeouts to move rules between environments.
- Workflows and processing rules for how rules, steps, and workflows fit together.