Skip to main content
Fact checking rails help ensure your bot’s responses are grounded in your knowledge base and don’t contain factual inaccuracies.

Overview

The fact checking guardrail validates bot responses against retrieved evidence (relevant chunks from your knowledge base). It:
  • Checks if bot responses are accurate relative to the evidence
  • Prevents the bot from making unsupported claims
  • Works with RAG (Retrieval-Augmented Generation) systems
  • Requires explicit activation per response

Quick Start

1

Enable fact checking in output rails

The fact checking flow is included by default:
config.yml
2

Activate fact checking per message

Set $check_facts = True before generating responses:
flows.co
3

Ensure relevant chunks are available

The fact checker needs $relevant_chunks in the context:

How It Works

The fact checking rail:
  1. Checks if $check_facts is set to True
  2. Retrieves the bot’s response and relevant evidence chunks
  3. Prompts the LLM to assess factual accuracy
  4. Returns a score from 0.0 (inaccurate) to 1.0 (accurate)
  5. Blocks responses with accuracy below 0.5

Configuration

Basic Configuration

config.yml
No additional configuration is needed - the rail is ready to use once included.

Activating Fact Checking

Fact checking must be explicitly activated:
flows.co
If $check_facts is not set to True, the fact checking rail does nothing. This allows you to selectively enable fact checking only when needed.

Evidence Requirements

The fact checker requires $relevant_chunks to be populated:
If no evidence is provided, the fact checker always returns True (allows the response).

Accuracy Threshold

The default threshold is 0.5. Responses with accuracy below this are blocked:
To customize the threshold, you can create a custom action:

Behavior

When a fact check fails (accuracy < 0.5):

With Rails Exceptions

config.yml
Raises FactCheckRailException with a message about the failed check.

Without Rails Exceptions

The bot refuses to respond and aborts the conversation.

Context Variables

The fact checking rail uses: Input:
  • $relevant_chunks - Evidence from knowledge base (list of strings)
  • $bot_message - The bot’s generated response
  • $check_facts - Boolean flag to enable checking
Output:
  • $accuracy - Float from 0.0 to 1.0 representing factual accuracy
  • $check_facts - Reset to False after checking

Custom Flows

Create custom fact checking flows:
flows.co

Integration with RAG

Typical RAG flow with fact checking:
flows.co

Temperature Settings

Fact checking uses the lowest possible temperature for consistency:

Implementation Details

The fact checking flows are defined in:
  • /nemoguardrails/library/self_check/facts/flows.co
  • /nemoguardrails/library/self_check/facts/actions.py
Actions:
  • SelfCheckFactsAction - Performs the fact check using LLM
The action uses the self_check_facts task prompt, which you can customize in prompts.yml.

Custom Task Prompts

Customize the fact checking prompt:
prompts.yml

Best Practices

  1. Enable selectively - Only use fact checking for knowledge-based responses
  2. Provide good evidence - Ensure $relevant_chunks contains relevant, high-quality information
  3. Handle failures gracefully - Consider warning users instead of always blocking
  4. Test threshold - The default 0.5 may need adjustment for your use case
  5. Monitor performance - Fact checking adds latency; consider caching strategies

Limitations

  • Requires evidence chunks to be available
  • Adds latency due to additional LLM call
  • May have false positives/negatives depending on evidence quality
  • Works best with clear, factual content (not opinions or creative responses)

See Also