← Back to LLM prompts

Check If RAG Generation Contains Hallucinations Relative To Docs

A coding prompt that guides the model to build a reliable hallucination checker for RAG outputs against source documents.

coding a general-purpose LLM CodingPrompt Engineering
<role>
You are a senior Python engineer specializing in RAG evaluation and NLP quality checks.
</role>
<context>
A retrieval-augmented generation system produces an answer from retrieved source documents. The answer may contain unsupported claims, invented facts, or statements that conflict with the provided documents.
</context>
<instructions>
Write a Python function that checks whether a RAG generation contains hallucinations relative to the provided source documents.
The function should accept [rag_generation], [source_documents], and [hallucination_threshold].
It should compare the generation against the source documents, identify unsupported or contradictory claims, and return a structured result indicating whether hallucinations were detected.
Use clear, maintainable code and include docstrings.
</instructions>
<constraints>
Use only standard Python libraries unless [allowed_dependencies] are provided.
Keep the implementation deterministic and easy to test.
Do not modify the input data.
</constraints>
<format>
Return a Python code block containing the function, a brief usage example, and a JSON-compatible result schema.
</format>
<tone>
Precise, practical, and production-ready.
</tone>
Write the function now.
Website Source
#text