Skip to main content

Prompt-Knowledge Validation Guide

Overview​

The Briefcase SDK provides a 3-layer validation engine that detects broken references between prompts and knowledge bases before LLM execution. This prevents runtime errors and ensures prompts reference valid, current knowledge.

Quick Start​

Installation​

from briefcase.validation import PromptValidationEngine
from briefcase.integrations.lakefs import VersionedClient

Basic Usage​

# Initialize lakeFS client
lakefs = VersionedClient(
repository="knowledge-base",
branch="main",
lakefs_endpoint="https://briefcasebrain.us-east-1.lakefscloud.io/api/v1",
lakefs_access_key="your-key",
lakefs_secret_key="your-secret"
)

# Create validation engine
validator = PromptValidationEngine(
versioned_client=lakefs,
repository="knowledge-base",
mode="strict"
)

# Validate a prompt
prompt = "Follow policies/medicare.pdf for evaluation"
report = validator.validate(prompt)

if report.status == "passed":
# Safe to execute
response = llm.complete(prompt)
else:
# Handle validation errors
for error in report.errors:
print(f"Error: {error.message}")
print(f"Fix: {error.remediation}")

Architecture​

3-Layer Validation​

  1. Layer 1: Syntax & Reference Extraction (<2ms)

    • Extracts knowledge base references using regex patterns
    • Supports: document IDs, policy names, section numbers, file paths
    • Example: policy: "Medicare 2024" → extracted
  2. Layer 2: Resolution Validation (<5ms per reference)

    • Resolves references against lakeFS
    • Checks file existence and versions
    • Fast index-based lookups
  3. Layer 3: Semantic Validation (100-500ms, optional)

    • LLM-based content checking
    • Detects structural changes (e.g., section renumbered)
    • Schema drift detection

Validation Modes​

ModeBehaviorUse Case
strictFail on errors, warn on warningsProduction, Compliance
tolerantOnly critical errors failDevelopment
warn_onlyNever fail, just logTesting, Monitoring

Error Types​

404 - Reference Not Found​

Problem: File doesn't exist in knowledge base Fix: Check file path or restore missing file

409 - Version Mismatch​

Problem: Prompt references old version Fix: Update to current version or pin specific version

410 - Reference Gone​

Problem: Content was deprecated/removed Fix: Update prompt to reference current content

Reference Patterns​

The validator recognizes these reference patterns:

# Document IDs
"doc_id: abc123"

# Policy names
'policy: "Medicare Coverage 2024"'

# Section references
"Section 4.2.3"

# File paths
"policies/medicare.pdf"

# Versioned files
"policies/medicare.pdf@abc123def"

Best Practices​

  1. Always validate in production - Prevent runtime errors
  2. Use strict mode for compliance - Healthcare, finance, legal
  3. Monitor validation metrics - Track failure rates via OTel
  4. Review remediation suggestions - Automated fix hints
  5. Keep knowledge base indexed - Faster resolution

Performance​

  • Syntax extraction: <2ms per prompt
  • Resolution: <5ms per reference
  • Full validation (10 refs): <100ms
  • Memory overhead: <10MB

Tip: Disable semantic validation for latency-sensitive applications.

Examples​

See examples/validation/ for complete examples:

  • basic_validation.py - Simple validation workflow
  • validation_modes.py - Different validation modes
  • semantic_validation.py - With LLM-based checking
  • auto_validation.py - Automatic validation before LLM calls

Integration with OpenTelemetry​

All validation operations are automatically instrumented with OpenTelemetry:

# Telemetry attributes captured:
- validation.status
- validation.mode
- validation.reference.count
- validation.error.count
- validation.resolution.time_ms
- validation.lakefs.commit

Query validation failures in your OTel backend:

validation.status = "failed" AND
engagement_id = "acme-healthcare"

Advanced: Semantic Validation​

Enable LLM-based semantic checking:

from openai import OpenAI

llm = OpenAI(api_key="your-key")

validator = PromptValidationEngine(
versioned_client=lakefs,
repository="kb",
mode="strict",
enable_semantic=True, # Enable semantic layer
llm_client=llm
)

The semantic layer detects:

  • Section renumbering
  • Content restructuring
  • Schema changes
  • Structural drift

Troubleshooting​

False Positives​

If validation fails incorrectly:

  1. Check knowledge base index is up-to-date
  2. Verify lakeFS connection
  3. Review reference extraction patterns

Performance Issues​

If validation is slow:

  1. Disable semantic validation
  2. Optimize knowledge base index
  3. Use tolerant mode in development

API Reference​

PromptValidationEngine​

class PromptValidationEngine:
def __init__(
self,
versioned_client,
repository: str,
branch: str = "main",
mode: str = "strict",
enable_semantic: bool = False,
llm_client=None
)

def validate(self, prompt: str) -> ValidationReport

ValidationReport​

@dataclass
class ValidationReport:
status: str # "passed" | "failed" | "warning"
errors: List[ValidationError]
warnings: List[ValidationError]
references_checked: int
validation_time_ms: float
lakefs_commit: str

ValidationError​

@dataclass
class ValidationError:
code: ValidationErrorCode # 404, 409, 410, etc.
message: str
reference: str
severity: str # "error" | "warning"
layer: str # "syntax" | "resolution" | "semantic"
remediation: Optional[str]

Support​

For issues or questions:


Version: 2.1.19 Last Updated: 2026-02-06