# RAG: answer or abstain

> Before a retrieval pipeline answers, check whether the passages it found answer the question; answer when decisio is sure, and say so when they do not.

Source: https://opendecisio.com/docs/use-cases/rag-gate/

The script sends the [question](https://opendecisio.com/docs/glossary/#question) and the passage as one text and acts only on a [certified answer](https://opendecisio.com/docs/glossary/#certified-answer):

 

[Figure: The model judges the input; your code chooses the action. Input: A question and passage. Question: Does the passage answer the question?. Typed answer: An answerability key and probabilities. Your code: Answer from the passage, explain what is missing or ask for review..]

 

## TL;DR

 

Save the [complete caller](https://opendecisio.com/examples/recipes/rag-gate.py) as `rag-gate.py` and run it with Python 3.12 or later. Set `DECISIO_BASE_URL` to your server, or leave the local default. Until you register and certify this question, the caller sends answers to review.

 python

```
import json
import os
import urllib.error
import urllib.request

QUESTION = {
    "type": "choice",
    "instructions": "Does the passage answer the question?",
    "criteria": {
        "yes": "The passage answers the question",
        "partly": "It answers only part of it",
        "no": "It does not answer it"
    }
}
BASE_URL = os.environ.get("DECISIO_BASE_URL", "http://127.0.0.1:8000").rstrip("/")
TIMEOUT = 15
REVIEW = "to a larger model or a person"

def route(text):
    request = urllib.request.Request(
        BASE_URL + "/v1/systemone",
        data=json.dumps({"state": text, "questions": {"answered": QUESTION}}).encode(),
        headers={"Content-Type": "application/json"},
    )
    try:
        with urllib.request.urlopen(request, timeout=TIMEOUT) as response:
            answer = json.load(response)["answers"]["answered"]
        if not isinstance(answer, dict) or answer.get("type") != QUESTION["type"]:
            return REVIEW
        if not (answer.get("choice") in QUESTION["criteria"]):
            return REVIEW
        # A gold certificate is about correctness; agreement alone is insufficient.
        if (answer.get("certified") is True
                and answer.get("certificate_kind") == "gold"
                and isinstance(answer.get("certificate"), str)
                and answer["certificate"]):
            return {"yes": "answer from the passage", "partly": "answer, and say what is missing", "no": "say it is not in the documents"}[answer["choice"]]
        return "to a larger model or a person"
    except (urllib.error.URLError, TimeoutError, OSError, ValueError, KeyError, TypeError):
        return REVIEW

if __name__ == "__main__":
    print(route("Question: How long is the warranty?\nPassage: Every product comes with a two-year warranty from the date of purchase."))
```

 Output

```
to a larger model or a person
```

 

## Is it a good fit?

 

Good fit

- A retrieval step that returns passages, and questions your users ask again and again.
- Labelled pairs of a question and a passage, to certify from.

Not a good fit

- Writing the answer itself: decisio reads and decides, it does not write.
- Questions that need reasoning across many documents at once.

 

## 1 Write the question

 

One `choice` question about the text: does the passage answer the question, partly, or not. Put the user’s question and the passage in the text, as the script does.

 

## 2 Teach it and certify it

 

Teach the question from labelled pairs, and certify it at your [error target](https://opendecisio.com/docs/glossary/#error-target).

 

Send at least 118 labelled question and passage pairs. The server holds back half to certify, and a 5% certificate needs 59 of those. More examples certify a larger share.

 

Prepare `rag-gate-labels.jsonl` with one JSON object per line, containing `state` and `answer`. Use real inputs and labels chosen by a person; each answer is one of the question’s [option](https://opendecisio.com/docs/glossary/#option) keys. The code below builds complete requests using the same question as the caller.

 python

```
import json
import os
import urllib.request

QUESTION = {
    "type": "choice",
    "instructions": "Does the passage answer the question?",
    "criteria": {
        "yes": "The passage answers the question",
        "partly": "It answers only part of it",
        "no": "It does not answer it"
    }
}
# One JSON object per line: state and the answer chosen by a person.
with open("rag-gate-labels.jsonl", encoding="utf-8") as source:
    rows = [json.loads(line) for line in source if line.strip()]
examples = [
    {"request": {"state": row["state"], "questions": {"answered": QUESTION}},
     "answer": row["answer"]}
    for row in rows
]
body = {"id": "rag-gate", "examples": examples, "certify": {"alpha": 0.05}}
base_url = os.environ.get("DECISIO_BASE_URL", "http://127.0.0.1:8000").rstrip("/")
request = urllib.request.Request(
    base_url + "/v1/tasks", data=json.dumps(body).encode(),
    headers={"Content-Type": "application/json"},
)
# Registration reads all your examples; allow more time than an ordinary decision.
with urllib.request.urlopen(request, timeout=300) as response:
    record = json.load(response)
print(json.dumps(record.get("certificate"), indent=2))
```

 

Needs the reader’s own human-labelled JSONL file.

 

Save the [registration script](https://opendecisio.com/examples/recipes/rag-gate-register.py) beside the labels file and run it with Python. Registration replaces a [task](https://opendecisio.com/docs/glossary/#task) of the same id. Check the returned certificate: a `null` [threshold](https://opendecisio.com/docs/glossary/#threshold) means no answer can be certified.

 

[Certify answers](https://opendecisio.com/docs/tasks/certify/#2-teach-the-task-with-a-certificate) runs this call on a server, with its answer. To let the server decline on its own, add a “can’t tell” option and an [abstention](https://opendecisio.com/docs/glossary/#abstain) threshold ([Certify answers](https://opendecisio.com/docs/tasks/certify/#let-the-server-abstain)).

 

## 3 Route on the certificate

 

The caller acts only when `certified` is `true` and `certificate_kind` is `gold`, with a certificate id. A learned table’s `agreement` certificate measures agreement with the model, so it goes to review in this recipe.

 

- A certified `yes` answers from the passage; `partly` answers with the missing part stated; `no` says it is not in the documents.
- Everything else goes to a larger model or a person.
- When your documents or your users’ questions change, certification is suspended until you certify again ([Watch for drift](https://opendecisio.com/docs/tasks/drift/)).

 

## What it did

 

- **82.7%** right on SciFact, does the evidence support the claim, and **88.7%** on BoolQ, yes or no over a passage, on Gemma 4 31B, untaught, in decisio's 1,400-item suite: one RTX PRO 6000, vLLM 0.30.0, 2026-10-04 (its evaluation card, section 7.2).
- Those measure the reading, not a whole retrieval pipeline: no end-to-end run of this gate is published.

 

 

## Limits

 

- An HTTP error, timeout or invalid response goes to the review path, like an uncertified answer ([Handle review and service failures](https://opendecisio.com/docs/use-cases/#handle-review-and-service-failures)).
- Keep the question and options stable after registration, and certify again after changing the task or [serving setup](https://opendecisio.com/docs/glossary/#serving-setup) ([Keep a certificate valid](https://opendecisio.com/docs/production/#keep-a-certificate-valid)).

 

## What next

 

- [Certify answers](https://opendecisio.com/docs/tasks/certify/): the promise, and abstention.
- [Scale](https://opendecisio.com/docs/scale/): long passages, and many questions about one text.
- [Use cases](https://opendecisio.com/docs/use-cases/): the other recipes.
