> For the complete documentation index, see [llms.txt](https://docs.mozilla.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mozilla.ai/any-guardrail/api-reference/index/hallucination/lettuce-detect.md).

# LettuceDetect

Token/span-level RAG hallucination detector.

Wraps the `lettucedetect` library to flag spans of an answer that are not supported by the provided context. Token classification over the (context, question, answer) triple lets it exploit ModernBERT's long context window, so full RAG contexts fit in a single pass. The supported checkpoints are English (`-en-v1`).

Expected inputs: `validate(input_text, context=..., question=...)` treats `input_text` as the *answer* to check; `context` (a string or list of strings) is the grounding text and is semantically required — omitting it raises `ValueError`; `question` is optional. Single answer per call (no batching).

`GuardrailOutput` mapping: `spans` mark hallucinated text (character `start` / `end` offsets into the answer, the offending `text`, `label="hallucination"`, and a confidence `score`); `valid` is `True` when no hallucinated span is found; the top-level `score` is the maximum span confidence (higher = riskier, `None` when nothing was flagged); `categories` holds a single `hallucination` entry with `triggered=True` when any span was found. `usage` records the model ID and latency.

For more information, see:

* [lettucedect-base-modernbert-en-v1](https://huggingface.co/KRLabsOrg/lettucedect-base-modernbert-en-v1) (default).
* [lettucedect-large-modernbert-en-v1](https://huggingface.co/KRLabsOrg/lettucedect-large-modernbert-en-v1).
* [LettuceDetect on GitHub](https://github.com/KRLabsOrg/LettuceDetect).
* [LettuceDetect: A Hallucination Detection Framework for RAG Applications (arXiv:2502.17125)](https://arxiv.org/abs/2502.17125).

Raises: ImportError: When the `lettucedetect` extra is not installed.

## Supported Models

* `KRLabsOrg/lettucedect-base-modernbert-en-v1`
* `KRLabsOrg/lettucedect-large-modernbert-en-v1`

## Constructor

| Parameter  | Type  | Required | Default         | Description                                                                                                                                                                                           |
| ---------- | ----- | -------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model_id` | \`str | None\`   | No              | `None`                                                                                                                                                                                                |
| `method`   | `str` | No       | `"transformer"` | Detection method forwarded to `lettucedetect`'s `HallucinationDetector` constructor. Defaults to `"transformer"`, the encoder token-classification method the supported checkpoints were trained for. |

Initialize the LettuceDetect guardrail.

## validate

Detect hallucinated spans in `input_text` (the answer) against `context`.

**Parameters**

| Parameter    | Type  | Required   | Default | Description                             |
| ------------ | ----- | ---------- | ------- | --------------------------------------- |
| `input_text` | `str` | Yes        | —       | The answer to check for hallucinations. |
| `context`    | \`str | list\[str] | None\`  | No                                      |
| `question`   | \`str | None\`     | No      | `None`                                  |

**Returns:** `GuardrailOutput`

## Benchmarks

### General Judge

| Dataset (rev)              | Metric           | Threshold    | Value    | Harness                | Source                          | Contam. |
| -------------------------- | ---------------- | ------------ | -------- | ---------------------- | ------------------------------- | ------- |
| judgebench (unspecified)   | choice\_accuracy | native-valid | 0.508772 | router\_judge\@c06603a | measured:router\_judge\@c06603a |         |
| llmbar (unspecified)       | choice\_accuracy | native-valid | 0.62807  | router\_judge\@c06603a | measured:router\_judge\@c06603a |         |
| rewardbench2 (unspecified) | choice\_accuracy | native-valid | 0.319298 | router\_judge\@c06603a | measured:router\_judge\@c06603a |         |

### Hallucination

| Dataset (rev)          | Metric | Threshold    | Value | Harness                 | Source                           | Contam. |
| ---------------------- | ------ | ------------ | ----- | ----------------------- | -------------------------------- | ------- |
| halueval (unspecified) | f1     | native-valid | 0.672 | guardrail-bench+ag0.7.4 | measured:guardrail-bench+ag0.7.4 |         |

## License

* **Vendor:** KRLabs
* **Default license:** `mit` (of the default model/service)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mozilla.ai/any-guardrail/api-reference/index/hallucination/lettuce-detect.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
