AI Hallucinations
You learned about hallucinations earlier in the course. Here we focus on responsible practices for managing hallucinations when deploying AI in real applications.
10 min•By Priygop Team•Updated 2026
Managing Hallucinations Responsibly
- Never use AI for medical, legal, or financial decisions without verified facts and licensed human review
- Always provide users with source citations when possible so they can verify AI claims
- Use RAG (from Module 9) to ground AI responses in verified documents
- Tell users clearly when AI is answering so they know to apply critical judgment
- Set expectations: inform users that AI can make mistakes and they should verify important information
- For high-stakes applications, add a human review step before AI outputs are delivered to end users
Hallucination Prevention in Code
Hallucination Prevention in Code
# Responsible hallucination management in an AI application
def build_responsible_ai_response(question, retrieved_facts=None):
"""
Build an AI response that is transparent about its sources
and acknowledges uncertainty when appropriate.
"""
if retrieved_facts:
# Ground the prompt in verified facts (RAG approach)
system_prompt = """Answer based ONLY on the provided facts.
If the question cannot be answered from the provided facts, say so clearly.
Do not add information beyond what is in the facts.
If you are uncertain, say you are uncertain."""
context = "\n".join([f"Fact: {fact}" for fact in retrieved_facts])
prompt = f"{system_prompt}\n\nFacts:\n{context}\n\nQuestion: {question}"
# Simulated grounded response
response = {
"answer": "Based on the provided information: [answer grounded in facts]",
"sources": retrieved_facts,
"confidence": "high", # Grounded in specific facts
"disclaimer": "This answer is based on specific verified documents."
}
else:
# Without grounding, be transparent about limitations
response = {
"answer": "Based on my training data: [generated answer]",
"sources": None,
"confidence": "medium", # No specific source
"disclaimer": "This answer is based on AI training data and may contain errors. Please verify important information from authoritative sources."
}
print("AI Response:")
print(f"Answer: {response['answer']}")
print(f"Confidence: {response['confidence']}")
print(f"Disclaimer: {response['disclaimer']}")
if response["sources"]:
print(f"Sources: {response['sources']}")
# Example: ungrounded vs grounded response
print("=== Without RAG (ungrounded) ===")
build_responsible_ai_response("What is our return policy?")
print()
print("=== With RAG (grounded) ===")
facts = ["Returns are accepted within 30 days with original receipt.", "Items must be unused and in original packaging."]
build_responsible_ai_response("What is our return policy?", retrieved_facts=facts)Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Key Takeaways
- You learned about hallucinations earlier in the course.
- Never use AI for medical, legal, or financial decisions without verified facts and licensed human review
- Always provide users with source citations when possible so they can verify AI claims
- Use RAG (from Module 9) to ground AI responses in verified documents