LLMs and Generative AI › The LLM Landscape › Day 244
Hands-on lab — Day 244: Capabilities, Limits, and Hallucination
- ← Back to the Day 244 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/llms-and-generative-ai/day-244-capabilities-limits-and-hallucination/
Commands
Setup
pip install -r requirements/requirements.txt Run
python3 examples/capabilities_limits_and_hallucination_lib.py Test
./tests/run_tests.sh File tree
conftest.py examples/capabilities_limits_and_hallucination_lib.py examples/test_capabilities_limits_and_hallucination_lib.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/starter-run.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/capabilities_limits_and_hallucination_lib.py starter/test_capabilities_limits_and_hallucination_lib.py tests/run_tests.sh tests/test_capabilities_limits_and_hallucination_lib.py troubleshooting.md
Lab README
Lab: Day 244 -- Capabilities, Limits, and Hallucination
Lesson
Day number: 244 of 365. Course: Course06-SS01 (LLMs and Generative AI - Working with LLMs). Topic: Capabilities, Cognitive Limits, Hallucination, and Grounding.
Purpose
Build and test an automated grounded factuality verifier and Self-Consistency majority voting engine in Python. Extract numerical and named entities, verify citation grounding against source documents, and implement consensus voting.
Learning objectives
- Distinguish intrinsic confabulation from ungrounded extrinsic extrapolation.
- Implement lexical and numerical citation verification algorithms.
- Build Self-Consistency consensus majority voting over multiple reasoning paths.
- Analyze long-context retrieval degradation in deep documents.
Prerequisites
- Day 243 (Tokens, Context Windows, and Sampling).
- Python 3.11+ with PyTorch and NumPy.
Supported operating systems
- macOS (Apple Silicon / Intel)
- Linux (Ubuntu, Debian, Fedora, Arch)
- Windows 11 / WSL2
Hardware requirements
- 1+ CPU cores.
- 1 GB RAM.
- 100 MB disk space.
Required software
- Python 3.11 or newer.
- pip package manager.
- virtualenv or venv module.
Free and open-source options
Python and standard regex/collections modules are free and open source.
Installation
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements/requirements.txt
File structure
starter/capabilities_limits_and_hallucination_lib.py: Student scaffold file.examples/capabilities_limits_and_hallucination_lib.py: Complete reference implementation.tests/test_capabilities_limits_and_hallucination_lib.py: Pytest automated validation suite.expected-output/: Verified output logs and baseline values.
How to run
Execute the reference demonstration script:
python3 examples/capabilities_limits_and_hallucination_lib.py
What the commands do
- Evaluates grounded vs hallucinated claims against a reference context.
- Aggregates multi-path reasoning outputs via majority voting.
- Runs unit test assertions.
Expected output
Hallucination Demo: True Claim Grounded = True, False Claim Grounded = False, Consensus = 42
Validation steps
- Verify
verify_claimreturnsTruefor factually grounded sentences. - Confirm
verify_claimflags ungrounded numerical claims asFalse. - Confirm
self_consistency_voteextracts the consensus majority winner. - Ensure all unit test assertions pass.
Tests
Run the test runner script:
./tests/run_tests.sh
Cleanup
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type d -name ".pytest_cache" -exec rm -rf {} +
Troubleshooting
- False negative on grounding: Ensure case differences are normalized to lowercase before matching.
Security notes
All grounding verification checks execute locally in memory.
Extension exercises
- Implement a token-level predictive entropy calculation heuristic.
- Build an automated citation matcher verifying exact paragraph spans.
Navigation
- Lesson title: Capabilities, Limits, and Hallucination
- Day number: 244 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-244-capabilities-limits-and-hallucination
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-244-capabilities-limits-and-hallucinationwhen the site is running.
Expected output
FIELDS.md
# Expected Output Fields: Day 244
- `True Claim Grounded`: Boolean indicating factually grounded verification.
- `False Claim Grounded`: Boolean indicating rejection of ungrounded claim.
- `Consensus`: Majority vote winner across reasoning paths.
examples-run.txt
Hallucination Demo: True Claim Grounded = True, False Claim Grounded = False, Consensus = 42
measured-values.txt
True Claim Grounded: True
False Claim Grounded: False
Consensus: 42
starter-run.txt
Starter scaffold executed. Ready for student implementation.
test-run.txt
============================= test session starts ==============================
collected 3 items
tests/test_capabilities_limits_and_hallucination_lib.py::test_grounded_verification PASSED [ 33%]
tests/test_capabilities_limits_and_hallucination_lib.py::test_self_consistency_majority_vote PASSED [ 66%]
tests/test_capabilities_limits_and_hallucination_lib.py::test_empty_candidates PASSED [100%]
============================== 3 passed in 0.41s ===============================
Source files
conftest.py (585 bytes)
"""Make this lab importable when the suite runs.
pytest puts the test file's own directory on sys.path, not the lab root, so a
test that does `from examples.<module> import ...` -- or imports a module that
lives in examples/ directly -- fails without this. Putting it here rather than
in run_tests.sh means a bare `pytest` from the lab root works too.
"""
import os
import sys
LAB = os.path.dirname(os.path.abspath(__file__))
for candidate in (LAB, os.path.join(LAB, "examples")):
if os.path.isdir(candidate) and candidate not in sys.path:
sys.path.insert(0, candidate)
examples/capabilities_limits_and_hallucination_lib.py (1692 bytes)
import re
from typing import List, Dict, Any
from collections import Counter
class GroundedVerifier:
def __init__(self, context: str):
self.context = context.lower()
def verify_claim(self, claim: str) -> bool:
claim_clean = claim.strip()
numbers = re.findall(r'\b\d+(?:\.\d+)?\b', claim_clean)
words = [w.lower() for w in re.findall(r'\b[A-Za-z]{4,}\b', claim_clean)]
# All numbers must be in context
for num in numbers:
if num not in self.context:
return False
# Lexical overlap
if not words:
return True
matched = [w for w in words if w in self.context]
return (len(matched) / len(words)) >= 0.60
def self_consistency_vote(candidates: List[str]) -> str:
if not candidates:
return ""
counts = Counter(candidates)
most_common = counts.most_common(1)[0][0]
return most_common
def run_hallucination_demo():
context = "In fiscal year 2024, Acme Corp reported total revenue of $150 million, representing 25% growth year over year."
verifier = GroundedVerifier(context)
claim_true = "Acme Corp reported revenue of $150 million with 25% growth."
claim_false = "Acme Corp reported profit of $999 billion."
res_true = verifier.verify_claim(claim_true)
res_false = verifier.verify_claim(claim_false)
paths = ["42", "42", "10", "42", "42"]
consensus = self_consistency_vote(paths)
print(f"Hallucination Demo: True Claim Grounded = {res_true}, False Claim Grounded = {res_false}, Consensus = {consensus}")
return res_true, res_false, consensus
if __name__ == "__main__":
run_hallucination_demo()
examples/test_capabilities_limits_and_hallucination_lib.py (785 bytes)
import pytest
from examples.capabilities_limits_and_hallucination_lib import (
GroundedVerifier,
self_consistency_vote
)
def test_grounded_verification():
context = "Tesla produced 1.85 million vehicles in 2023, delivering 1.81 million to customers."
verifier = GroundedVerifier(context)
# Valid claim
assert verifier.verify_claim("Tesla produced 1.85 million vehicles in 2023.") is True
# Missing number hallucination
assert verifier.verify_claim("Tesla produced 5.00 million vehicles in 2023.") is False
def test_self_consistency_majority_vote():
votes = ["Option A", "Option B", "Option A", "Option A", "Option C"]
assert self_consistency_vote(votes) == "Option A"
def test_empty_candidates():
assert self_consistency_vote([]) == ""
metadata.yml (450 bytes)
lesson_id: D244
day: 244
kind: lab
languages:
- python
setup_commands:
- 'pip install -r requirements/requirements.txt'
run_commands:
- 'python3 examples/capabilities_limits_and_hallucination_lib.py'
test_commands:
- './tests/run_tests.sh'
cleanup_commands:
- 'find . -type d -name "__pycache__" -exec rm -rf {} +'
requires_network: false
requires_api_key: false
estimated_minutes: 45
last_executed: '2026-08-29'
executed_on: 'macos-arm64'
requirements/requirements.txt (41 bytes)
torch>=2.2.0
numpy>=1.26.0
pytest>=8.0.0
starter/capabilities_limits_and_hallucination_lib.py (446 bytes)
import re
from typing import List, Dict, Any
from collections import Counter
class GroundedVerifier:
def __init__(self, context: str):
self.context = context.lower()
def verify_claim(self, claim: str) -> bool:
# TODO: Return True if all numbers in claim are in context and word overlap >= 0.6
pass
def self_consistency_vote(candidates: List[str]) -> str:
# TODO: Return the majority vote candidate
pass
starter/test_capabilities_limits_and_hallucination_lib.py (785 bytes)
import pytest
from examples.capabilities_limits_and_hallucination_lib import (
GroundedVerifier,
self_consistency_vote
)
def test_grounded_verification():
context = "Tesla produced 1.85 million vehicles in 2023, delivering 1.81 million to customers."
verifier = GroundedVerifier(context)
# Valid claim
assert verifier.verify_claim("Tesla produced 1.85 million vehicles in 2023.") is True
# Missing number hallucination
assert verifier.verify_claim("Tesla produced 5.00 million vehicles in 2023.") is False
def test_self_consistency_majority_vote():
votes = ["Option A", "Option B", "Option A", "Option A", "Option C"]
assert self_consistency_vote(votes) == "Option A"
def test_empty_candidates():
assert self_consistency_vote([]) == ""
tests/run_tests.sh (227 bytes)
#!/usr/bin/env bash
set -euo pipefail
echo "========================================"
echo "Running Day 244 Lab Test Suite"
echo "========================================"
pytest tests/ -v
echo "All tests passed successfully."
tests/test_capabilities_limits_and_hallucination_lib.py (785 bytes)
import pytest
from examples.capabilities_limits_and_hallucination_lib import (
GroundedVerifier,
self_consistency_vote
)
def test_grounded_verification():
context = "Tesla produced 1.85 million vehicles in 2023, delivering 1.81 million to customers."
verifier = GroundedVerifier(context)
# Valid claim
assert verifier.verify_claim("Tesla produced 1.85 million vehicles in 2023.") is True
# Missing number hallucination
assert verifier.verify_claim("Tesla produced 5.00 million vehicles in 2023.") is False
def test_self_consistency_majority_vote():
votes = ["Option A", "Option B", "Option A", "Option A", "Option C"]
assert self_consistency_vote(votes) == "Option A"
def test_empty_candidates():
assert self_consistency_vote([]) == ""
Troubleshooting
Troubleshooting: Day 244 - Capabilities, Limits, and Hallucination
Common Issues
- Regex number parsing failure:
- Cause: Splitting floating point numbers at decimal points.
- Fix: Use
r'\b\d+(?:\.\d+)?\b'to capture whole floats.
Security notes
Security & Privacy: Day 244 - Capabilities, Limits, and Hallucination
Security Guidance
- All verification rules execute locally without external network calls.