# DataRobot Moderations library

> DataRobot Moderations library - How to install and use the DataRobot Moderations library to apply
> guard-based moderation to LLM prompts and responses.

This Markdown file sits beside the HTML page at the same path (with a `.md` suffix). It summarizes the topic and lists links for tools and LLM context.

Companion generated at `2026-06-10T05:26:01.506169+00:00` (UTC).

## Primary page

- [DataRobot Moderations library](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md): Full documentation for this topic (Markdown sidecar).

## Sections on this page

- [Architecture](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#architecture): In-page section heading.
- [How to build it](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#how-to-build-it): In-page section heading.
- [How to use it](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#how-to-use-it): In-page section heading.
- [Optional extras](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#optional-extras): In-page section heading.
- [Example: task-adherence guard backed by a DataRobot LLM deployment](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#example-task-adherence-guard-backed-by-a-datarobot-llm-deployment): In-page section heading.
- [Transient dependencies and build compatibility](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#transient-dependencies-and-build-compatibility): In-page section heading.
- [Standalone Python API](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#standalone-python-api): In-page section heading.
- [Using the CLI](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#using-the-cli): In-page section heading.
- [Usage with DRUM](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/index.html.md#usage-with-drum): In-page section heading.

## Related documentation

- [Developer documentation](https://docs.datarobot.com/en/docs/api/index.html.md): Linked from this page.
- [Code-first tools](https://docs.datarobot.com/en/docs/api/code-first-tools/index.html.md): Linked from this page.
- [Moderations guardrails](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/moderations-guardrails.html.md): Linked from this page.
- [Using the Moderations CLI](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/using-moderations-with-the-cli.html.md): Linked from this page.
- [Moderations in structured custom models](https://docs.datarobot.com/en/docs/api/code-first-tools/drum/structured-custom-models.html.md#moderations): Linked from this page.

## Documentation content

The [DataRobot Moderations library](https://pypi.org/project/datarobot-moderations/) applies guard-based moderation to LLM prompts and responses based on your guard configuration. You define guards in YAML (or programmatically), pass prompts and responses through a pipeline, and receive structured results that indicate whether content was blocked, replaced, or allowed with metrics attached.

| Section | Description |
| --- | --- |
| Moderations guardrails | Configure guards in YAML, choose guard types and LLM backends, and use the Python API. |
| Using the Moderations CLI | Install, authenticate, and run the dr-moderation CLI. |

For each evaluation, the library can report:

- Whether the prompt should be blocked.
- Whether the completion should be blocked.
- Metric values from model and out-of-the-box guards.
- Whether the prompt or response was modified by a modifier guard.

## Architecture

The library wraps the typical LLM prediction flow. It first runs prescore guards that evaluate prompts and enforce moderation when necessary. Prompts that pass prescore checks are forwarded to the LLM for completion. The library then evaluates those completions with postscore guards and enforces intervention as needed.

## How to build it

The repository uses `poetry` to manage the build process and a wheel can be built using:

```
make clean
make
```

## How to use it

You can install a generated or downloaded wheel file with `pip`; dependencies are installed automatically.

```
pip install datarobot-moderations
```

### Optional extras

The base install covers token-count, ROUGE-1, cost, and NeMo guards.
Heavier or cloud-specific dependencies are opt-in:

| Extra | What it enables |
| --- | --- |
| datarobot-sdk | DataRobot model guards, DataRobot LLM evaluator type |
| llm-eval | Faithfulness, Task Adherence, Agent Goal Accuracy, Guideline Adherence guards |
| nemo | NeMo Guardrails colang-based flow guard |
| nemo-evaluator | NeMo live-evaluation microservice guard |
| nvidia | NVIDIA NIM / ChatNVIDIA LLM support |
| vertex | Google Cloud Vertex AI LLM support |
| bedrock | AWS Bedrock LLM support |
| all | Every optional dependency at once |

The library opts out of deepeval telemetry by default.

#### Example: task-adherence guard backed by a DataRobot LLM deployment

```
pip install 'datarobot-moderations[llm-eval,datarobot-sdk]'
```

### Transient dependencies and build compatibility

Installing `[all]` (or the `nemo` / `llm-eval` extras individually) pulls in packages that `nemoguardrails` and `deepeval` declare as runtime dependencies but that this library never uses at runtime:

| Package | Pulled in by | Problem |
| --- | --- | --- |
| annoy | nemoguardrails | Requires a C++ compiler; breaks restricted build environments such as Kaniko |
| fastembed / onnxruntime | nemoguardrails | Heavy ML runtimes, hundreds of MB |
| fastapi / starlette / uvicorn | nemoguardrails | Web server stack, only used by nemoguardrails' built-in server |
| watchdog / prompt-toolkit / typer | nemoguardrails, deepeval | Dev-server and CLI tools |
| pyfiglet / wheel | deepeval | CLI banner / build artifact mis-declared as a runtime dep |

To exclude them, add the following to your own project's `pyproject.toml` (these overrides are not inherited from this library):

```
[tool.uv]
override-dependencies = [
    "annoy; sys_platform == 'never'",
    "fastembed; sys_platform == 'never'",
    "onnxruntime; sys_platform == 'never'",
    "fastapi; sys_platform == 'never'",
    "starlette; sys_platform == 'never'",
    "uvicorn; sys_platform == 'never'",
    "watchdog; sys_platform == 'never'",
    "prompt-toolkit; sys_platform == 'never'",
    "typer; sys_platform == 'never'",
    "pyfiglet; sys_platform == 'never'",
    "wheel; sys_platform == 'never'",
]
```

## Standalone Python API

Create a `ModerationPipeline` from a YAML file, a plain dict, or a Pydantic config object, then evaluate prompts, responses, or a full prescore → LLM → postscore pipeline. Each method has an async counterpart.

For constructor options, method parameters, return types, DataFrame schemas, streaming details, and environment variables, see [Moderations guardrails](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/moderations-guardrails.html.md#using-the-config-in-python).

## Using the CLI

The package ships a `dr-moderation` CLI so you can manage guards without writing Python code. Commands include `evaluate`, `add-guard`, `agent a2a connect`, and `serve` (JSON-RPC over stdio or WebSocket).

For installation, authentication, command reference, YAML schema differences, and exit codes, see [Using Moderations with the CLI](https://docs.datarobot.com/en/docs/api/code-first-tools/moderations-library/using-moderations-with-the-cli.html.md).

## Usage with DRUM

The library wraps [DRUM's](https://github.com/datarobot/datarobot-user-models) `score` method for prescore and postscore guards. With DRUM, run your custom model using `drum score` to use moderation features.

Install DRUM along with the optional extras required for your guards. If you are unsure which guards are in use, install `[all]`:

```
pip install datarobot-drum 'datarobot-moderations[all]'
drum score --verbose --logging-level info --code-dir ./ --input ./input.csv --target-type textgeneration --runtime-params-file values.yaml
```

For DRUM-specific guard configuration and the `chat()` hook response format, see [Moderations in structured custom models](https://docs.datarobot.com/en/docs/api/code-first-tools/drum/structured-custom-models.html.md#moderations).
