API Reference¶
ContextGuard Core Specifications
This module defines the fundamental data structures (contracts) that power ContextGuard: - StateSpec: Persistent constraints that filter retrieval and enforce consistency - ClaimSpec: Atomic claims to be verified - Evidence: Retrieved chunks with provenance - Verdict: Per-claim and overall verification results - ReasonCode: Machine-readable explanation codes
These are the "types" of the ContextGuard compiler.
Chunk ¶
Bases: BaseModel
A retrieved chunk of text with full metadata.
This is the universal representation that works across all vector DBs. Adapters convert backend-specific formats to/from Chunk.
Source code in contextguard/core/specs.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
Claim ¶
Bases: BaseModel
An atomic, verifiable claim.
Claims are the "program" that ContextGuard verifies. Each claim should be: - Atomic: one fact per claim - Testable: can be supported or contradicted by evidence - Specific: has clear entities, time, metrics
Source code in contextguard/core/specs.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
generate_id
classmethod
¶
generate_id(text)
Generate stable ID from claim text.
Source code in contextguard/core/specs.py
521 522 523 524 525 | |
ClaimVerdict ¶
Bases: BaseModel
Verdict for a single claim.
Contains: - The claim - Label (SUPPORTED/CONTRADICTED/INSUFFICIENT/MIXED) - Confidence score - Reason codes explaining the verdict - Evidence that led to the verdict
Source code in contextguard/core/specs.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | |
ContextPack ¶
Bases: BaseModel
Safe context pack for LLM generation.
This is the SECONDARY OUTPUT: a curated set of verified facts that can be safely fed to an LLM for generation.
Only includes evidence from SUPPORTED claims.
Source code in contextguard/core/specs.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
to_prompt_text ¶
to_prompt_text(max_tokens=2000)
Convert to text suitable for LLM prompt.
Source code in contextguard/core/specs.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |
EntityRef ¶
Bases: BaseModel
Canonical entity reference.
Entities are the "who" of verification: companies, people, organizations. The entity_id should be a stable canonical identifier (ticker, LEI, internal ID).
Source code in contextguard/core/specs.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
matches_text ¶
matches_text(text)
Check if text mentions this entity (case-insensitive).
Source code in contextguard/core/specs.py
232 233 234 235 236 237 238 239 | |
EvidenceAssessment ¶
Bases: BaseModel
Full assessment of a chunk as evidence for a claim.
Combines: - The chunk itself - Gate decision (why it was accepted/rejected) - Judge scores (support/contradict) - Extracted rationale
Source code in contextguard/core/specs.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
EvidenceRole ¶
Bases: str, Enum
Role of evidence in supporting or contradicting a claim.
Source code in contextguard/core/specs.py
48 49 50 51 52 | |
GateDecision ¶
Bases: BaseModel
Decision from the evidence gating layer.
Every chunk gets a GateDecision explaining: - Was it accepted or rejected? - Why? (reason codes) - Which constraints did it match/violate?
Source code in contextguard/core/specs.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
MergeConflict ¶
Bases: BaseModel
Record of a conflict detected during state merge.
Source code in contextguard/core/specs.py
339 340 341 342 343 344 345 346 347 | |
MergeResult ¶
Bases: BaseModel
Result of merging StateDelta into StateSpec.
Source code in contextguard/core/specs.py
350 351 352 353 354 355 356 357 | |
Provenance ¶
Bases: BaseModel
Complete provenance chain for a piece of evidence.
This is critical for: - Audit trails - Reproducibility - Trust calibration - Citations in reports
Source code in contextguard/core/specs.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
ReasonCode ¶
Bases: str, Enum
Machine-readable reason codes for every decision in the pipeline. These appear in gate decisions, verdicts, and warnings.
Organized by category: - CTXT_: Context/constraint failures (the core problem we're solving) - EVIDENCE_: Evidence quality issues - CLAIM_: Claim formulation issues - SYS_: System/execution issues
Source code in contextguard/core/specs.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
SourcePolicy ¶
Bases: BaseModel
Source filtering policy.
Controls what evidence is admissible based on: - Source type (primary/secondary/tertiary) - Specific domains (allow/block lists) - Recency requirements - Corpus vs web access
Source code in contextguard/core/specs.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
SourceType ¶
Bases: str, Enum
Classification of evidence sources by reliability tier.
Source code in contextguard/core/specs.py
55 56 57 58 59 | |
StateDelta ¶
Bases: BaseModel
Partial state update extracted from new user input.
This is NOT the full state; it's what changed in the current turn. The merge algorithm combines StateDelta with existing StateSpec.
Source code in contextguard/core/specs.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
StateSpec ¶
Bases: BaseModel
The State Contract: persistent constraints that control retrieval and verification.
This is THE core abstraction of ContextGuard. It represents: - WHAT entities we're talking about - WHEN (time constraints) - WHAT metric/topic - HOW to normalize units - WHICH sources are allowed
The StateSpec persists across turns and filters retrieval. A chunk that violates any constraint is rejected with reason codes.
Source code in contextguard/core/specs.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
get_entity_ids ¶
get_entity_ids()
Get all entity IDs for filter construction.
Source code in contextguard/core/specs.py
286 287 288 | |
has_constraints ¶
has_constraints()
Check if any meaningful constraints are set.
Source code in contextguard/core/specs.py
290 291 292 293 294 295 296 297 | |
TimeConstraint ¶
Bases: BaseModel
Time-based constraints for retrieval and verification.
Supports: - Specific year/quarter (fiscal or calendar) - Date ranges - Both can be combined (e.g., Q1 2024 with specific start/end dates)
Source code in contextguard/core/specs.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
is_empty ¶
is_empty()
Check if no time constraints are set.
Source code in contextguard/core/specs.py
147 148 149 150 151 152 153 154 | |
matches ¶
matches(other)
Check if another time constraint is compatible.
Source code in contextguard/core/specs.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
UnitConstraint ¶
Bases: BaseModel
Unit and scale constraints for numeric verification.
Critical for financial data where: - "200" could mean 200, 200K, 200M, or 200B - USD vs EUR matters - Nominal vs real (inflation-adjusted) differs
Source code in contextguard/core/specs.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
VerdictLabel ¶
Bases: str, Enum
Final verdict for a claim or overall report.
Source code in contextguard/core/specs.py
40 41 42 43 44 45 | |
VerdictReport ¶
Bases: BaseModel
The complete verification report.
This is the PRIMARY OUTPUT of ContextGuard: - Overall verdict with confidence - Per-claim verdicts with citations - Warnings and issues - State used for verification
Source code in contextguard/core/specs.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
ContextGuard Retrieval Protocols
This module defines the universal interfaces for retrieval that work across any vector database, search backend, or hybrid system.
Key abstraction: ContextGuard doesn't care HOW you retrieve - it cares WHAT you retrieve and whether it satisfies the current StateSpec.
Protocols defined here: - Retriever: The universal retrieval interface - CanonicalFilters: Backend-agnostic filter specification - Chunk: Universal chunk representation (see core/specs.py)
Adapters in contextguard/adapters/ translate these to specific backends: - LangChain retrievers - LlamaIndex retrievers - Direct pgvector/Qdrant/Chroma/Weaviate calls
AsyncRetriever ¶
Bases: Protocol
Optional async retriever interface.
If implemented, async runners can call asearch directly instead of using thread pools.
Source code in contextguard/retrieve/protocols.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
CanonicalFilters ¶
Bases: BaseModel
Universal filter specification for retrieval.
This is translated by adapters into backend-specific filter syntax (Qdrant filter dict, pgvector WHERE clause, Chroma where, etc.).
Design principle: express filters in domain terms (entity, time, source), not in vector DB terms (metadata.field == value).
Source code in contextguard/retrieve/protocols.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
from_state_spec
classmethod
¶
from_state_spec(state)
Create filters from a StateSpec.
This is the main translation from state contract to retrieval filters.
Source code in contextguard/retrieve/protocols.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
is_empty ¶
is_empty()
Check if no filters are set.
Source code in contextguard/retrieve/protocols.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
to_dict ¶
to_dict()
Convert to dictionary for serialization.
Source code in contextguard/retrieve/protocols.py
93 94 95 | |
FederatedRetriever ¶
Bases: RetrieverBase
Retriever that combines results from multiple backends.
Useful for: - Corpus + web retrieval - Multiple corpora (internal + external) - Hybrid search (vector + BM25)
Source code in contextguard/retrieve/protocols.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
MockRetriever ¶
Bases: RetrieverBase
Mock retriever for testing.
Pre-loaded with chunks that can be searched. Useful for unit tests and demos without a real vector DB.
Source code in contextguard/retrieve/protocols.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
add_chunk ¶
add_chunk(text, source_id='mock_doc', source_type=SourceType.SECONDARY, entity_ids=None, year=None, metadata=None)
Add a chunk to the mock store.
Source code in contextguard/retrieve/protocols.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
Retriever ¶
Bases: Protocol
Protocol for any retrieval backend.
Implementations: - contextguard.adapters.langchain.LangChainRetriever - contextguard.adapters.llamaindex.LlamaIndexRetriever - Direct implementations for pgvector, Qdrant, Chroma, etc.
The only method required is search(). Everything else is optional.
Source code in contextguard/retrieve/protocols.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
search ¶
search(query, *, filters=None, k=10)
Search for chunks matching the query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The search query (natural language) |
required |
filters
|
Optional[CanonicalFilters]
|
Optional filters to apply |
None
|
k
|
int
|
Maximum number of results to return |
10
|
Returns:
| Type | Description |
|---|---|
List[Chunk]
|
List of Chunk objects with provenance |
Source code in contextguard/retrieve/protocols.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
RetrieverBase ¶
Bases: ABC
Base class for retriever implementations.
Provides common functionality like filter translation and logging. Subclasses must implement _search_impl().
Source code in contextguard/retrieve/protocols.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
search ¶
search(query, *, filters=None, k=None)
Public search method with common pre/post processing.
Source code in contextguard/retrieve/protocols.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
ContextGuard Retrieval Planner
This module generates retrieval plans that enforce: 1. Coverage: at least one query per entity × claim combination 2. Counter-evidence: always search for contradictions (anti-confirmation-bias) 3. Constraint injection: queries include state constraints
The planner is the difference between "top-k once" and "systematic evidence gathering."
Key insight: Most RAG systems fail because they retrieve once and hope. ContextGuard retrieves systematically based on what needs to be verified.
QueryType ¶
Bases: str, Enum
Types of retrieval queries.
Source code in contextguard/retrieve/planner.py
32 33 34 35 36 37 | |
RetrievalPlan
dataclass
¶
A complete retrieval plan with ordered steps.
Source code in contextguard/retrieve/planner.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
get_counter_steps ¶
get_counter_steps()
Get counter-evidence query steps.
Source code in contextguard/retrieve/planner.py
106 107 108 | |
get_steps_for_claim ¶
get_steps_for_claim(claim_id)
Get all steps targeting a specific claim.
Source code in contextguard/retrieve/planner.py
98 99 100 | |
get_support_steps ¶
get_support_steps()
Get support query steps.
Source code in contextguard/retrieve/planner.py
102 103 104 | |
RetrievalPlanner ¶
Plans retrieval based on claims and state constraints.
The planner ensures: 1. Every claim gets at least one support query 2. Every claim gets at least one counter query (if enabled) 3. Queries are constrained by StateSpec (entity, time, source policy) 4. Multi-entity claims get per-entity queries
Source code in contextguard/retrieve/planner.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
plan ¶
plan(claims, state, total_k=50, trace=None, trace_parents=None)
Generate a retrieval plan for the given claims and state.
Strategy: 1. For each claim, generate support + counter queries 2. Distribute k across steps based on claim weight 3. Apply state constraints to all queries
Source code in contextguard/retrieve/planner.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
RetrievalStep
dataclass
¶
A single step in the retrieval plan.
Each step is a query with: - The query text - Filters to apply - Query type (support/counter/background) - Target claim (optional)
Source code in contextguard/retrieve/planner.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
estimate_plan_cost ¶
estimate_plan_cost(plan)
Estimate the cost/size of executing a retrieval plan.
Useful for budgeting and planning.
Source code in contextguard/retrieve/planner.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
plan_retrieval ¶
plan_retrieval(claims, state, total_k=50, enable_counter=True, trace=None, trace_parents=None, profile=None)
Convenience function to create a retrieval plan.
Uses default planner settings.
Source code in contextguard/retrieve/planner.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
ContextGuard Evidence Gating
This module implements the evidence gating layer that: 1. Enforces constraint eligibility (hard rejection) 2. Filters noise and boilerplate 3. Enforces diversity (prevents top-k monoculture) 4. Produces reason codes for every decision
Gating is the mechanism that prevents "plausible but wrong" chunks from reaching the verification stage.
Key insight: Similarity ≠ Relevance under constraints. A chunk with 0.95 cosine similarity can be COMPLETELY WRONG if it violates a time or entity constraint.
Design principle: HARD GATES, not soft penalties. Rejected chunks are rejected with reason codes, not downranked. This makes the system explainable and debuggable.
EvidenceGate ¶
The evidence gating layer.
Evaluates each chunk against: 1. StateSpec constraints (entity, time, source policy) 2. Quality filters (length, boilerplate) 3. Diversity requirements (max per source)
Returns GateDecision with reason codes for every chunk.
Source code in contextguard/retrieve/gating.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
gate ¶
gate(chunks, state, trace=None, parents=None)
Gate a list of chunks against the current state.
Returns GatedChunk objects with accept/reject decisions and reason codes.
Source code in contextguard/retrieve/gating.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
GatedChunk
dataclass
¶
A chunk with its gating decision.
Source code in contextguard/retrieve/gating.py
111 112 113 114 115 116 117 118 119 | |
GatingConfig
dataclass
¶
Configuration for the gating layer.
Source code in contextguard/retrieve/gating.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
from_profile
classmethod
¶
from_profile(profile)
Factory presets for different domains.
Source code in contextguard/retrieve/gating.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
explain_rejection ¶
explain_rejection(gated_chunk)
Generate human-readable explanation for a rejection.
Source code in contextguard/retrieve/gating.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
filter_accepted ¶
filter_accepted(gated)
Get only accepted chunks.
Source code in contextguard/retrieve/gating.py
516 517 518 | |
filter_rejected ¶
filter_rejected(gated)
Get only rejected chunks with their decisions.
Source code in contextguard/retrieve/gating.py
521 522 523 | |
gate_chunks ¶
gate_chunks(chunks, state, config=None, trace=None, parents=None)
Convenience function to gate chunks.
Returns list of GatedChunk with accept/reject decisions.
Source code in contextguard/retrieve/gating.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
summarize_gating ¶
summarize_gating(gated)
Summarize gating results.
Useful for debugging and reporting.
Source code in contextguard/retrieve/gating.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
ContextGuard Claim Splitter
This module decomposes text into atomic, verifiable claims.
Each claim should be: - Atomic: one fact per claim - Testable: can be supported or contradicted by evidence - Specific: has clear entities, time, metrics when applicable
The claim splitter is the "parser" of the verification compiler. Bad claim splitting → bad verification.
ClaimSplitter ¶
Bases: ABC
Abstract base for claim splitting implementations.
Source code in contextguard/verify/claim_splitter.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
split
abstractmethod
¶
split(text)
Split text into atomic claims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to decompose |
required |
Returns:
| Type | Description |
|---|---|
List[Claim]
|
List of Claim objects |
Source code in contextguard/verify/claim_splitter.py
68 69 70 71 72 73 74 75 76 77 78 79 | |
LLMClaimSplitter ¶
Bases: ClaimSplitter
LLM-powered claim splitter.
Uses structured prompting to extract atomic claims with facets.
Source code in contextguard/verify/claim_splitter.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
split ¶
split(text)
Split text into claims using LLM.
Source code in contextguard/verify/claim_splitter.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
LLMProvider ¶
Bases: Protocol
Protocol for LLM providers used by claim splitter and judges.
Implementations can wrap OpenAI, Anthropic, local models, etc.
Source code in contextguard/verify/claim_splitter.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
complete_json ¶
complete_json(prompt, schema, temperature=0.0)
Complete a prompt and return structured JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to complete |
required |
schema
|
Dict[str, Any]
|
JSON schema describing expected output |
required |
temperature
|
float
|
Sampling temperature (0 = deterministic) |
0.0
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Parsed JSON response matching schema |
Source code in contextguard/verify/claim_splitter.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
RuleBasedClaimSplitter ¶
Bases: ClaimSplitter
Rule-based claim splitter.
Uses heuristics to split text into claims without LLM. Useful as a fallback or for simple cases.
Source code in contextguard/verify/claim_splitter.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
split ¶
split(text)
Split text into claims using rules.
Source code in contextguard/verify/claim_splitter.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
filter_verifiable ¶
filter_verifiable(claims)
Filter to only verifiable (non-vague, non-subjective) claims.
Source code in contextguard/verify/claim_splitter.py
389 390 391 392 393 394 | |
get_claim_summary ¶
get_claim_summary(claims)
Get summary statistics for a list of claims.
Source code in contextguard/verify/claim_splitter.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
split_claims ¶
split_claims(text, llm=None, max_claims=10)
Convenience function to split text into claims.
Uses LLM if provided, otherwise falls back to rule-based.
Source code in contextguard/verify/claim_splitter.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
ContextGuard Verification Judges
This module implements the claim–evidence scoring logic.
For each (claim, evidence) pair, judges produce: - support_score: [0, 1] - how much the evidence supports the claim - contradict_score: [0, 1] - how much the evidence contradicts the claim - rationale: short explanation of the decision - quality signals: entity/time/metric matches
The judge is the "type checker" of the verification compiler. Bad judge calls → wrong verdicts.
Implementations: - LLMJudge: Uses LLM for semantic understanding - NLIJudge: Uses NLI models (entailment/contradiction) - RuleBasedJudge: Simple heuristics for testing
Judge ¶
Bases: ABC
Abstract base for verification judges.
Source code in contextguard/verify/judges.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
score
abstractmethod
¶
score(claim, evidence, state=None)
Score a single claim against a single piece of evidence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claim
|
Claim
|
The claim to verify |
required |
evidence
|
Chunk
|
The evidence chunk |
required |
state
|
Optional[StateSpec]
|
Optional state constraints (for context) |
None
|
Returns:
| Type | Description |
|---|---|
JudgeResult
|
JudgeResult with scores and rationale |
Source code in contextguard/verify/judges.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
score_batch ¶
score_batch(claim, evidence_list, state=None)
Score a claim against multiple evidence chunks.
Default implementation calls score() for each. Subclasses may override for batch optimization.
Source code in contextguard/verify/judges.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
JudgeResult
dataclass
¶
Result of judging a claim against evidence.
Source code in contextguard/verify/judges.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
get_role ¶
get_role()
Determine the role based on scores.
Source code in contextguard/verify/judges.py
77 78 79 80 81 82 83 84 | |
to_assessment ¶
to_assessment(chunk, gate_decision)
Convert to EvidenceAssessment.
Source code in contextguard/verify/judges.py
86 87 88 89 90 91 92 93 94 95 | |
LLMJudge ¶
Bases: Judge
LLM-powered verification judge.
Uses structured prompting to determine support/contradiction.
Source code in contextguard/verify/judges.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
score ¶
score(claim, evidence, state=None)
Score using LLM.
Source code in contextguard/verify/judges.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
LLMProvider ¶
Bases: Protocol
Lightweight structural interface for LLM providers.
Any object implementing this method is accepted by LLMJudge.
Source code in contextguard/verify/judges.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
LLMProviderBase ¶
Bases: ABC
Abstract base class for LLM providers (OOP-friendly).
Use when you prefer subclassing + overriding to pure duck typing.
Source code in contextguard/verify/judges.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
complete_json
abstractmethod
¶
complete_json(prompt, schema, temperature=0.0)
Return a JSON object (dict) matching the provided schema. Implementations may raise exceptions on failure.
Source code in contextguard/verify/judges.py
127 128 129 130 131 132 133 134 135 136 137 138 | |
NLIJudge ¶
Bases: Judge
NLI-based judge using entailment models.
Uses models like: - roberta-large-mnli - deberta-v3-base-mnli - sentence-transformers NLI models
Requires transformers library.
Source code in contextguard/verify/judges.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
score ¶
score(claim, evidence, state=None)
Score using NLI model.
Source code in contextguard/verify/judges.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
RuleBasedJudge ¶
Bases: Judge
Simple rule-based judge using keyword matching.
Useful for: - Unit tests - Fallback when LLM unavailable - Fast baseline comparisons
Source code in contextguard/verify/judges.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
score ¶
score(claim, evidence, state=None)
Score using keyword matching.
Source code in contextguard/verify/judges.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
best_evidence ¶
best_evidence(results, for_support=True)
Get the best evidence result for support or contradiction.
Source code in contextguard/verify/judges.py
627 628 629 630 631 632 633 634 635 636 637 | |
create_judge ¶
create_judge(judge_type='rule', llm=None, **kwargs)
Factory function to create judges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
judge_type
|
str
|
"rule", "llm", or "nli" |
'rule'
|
llm
|
Optional[LLMProvider]
|
Required for "llm" type |
None
|
**kwargs
|
Additional arguments for specific judge types |
{}
|
Source code in contextguard/verify/judges.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
judge_claim ¶
judge_claim(claim, evidence, judge=None, state=None)
Convenience function to judge a claim against evidence.
Uses RuleBasedJudge if no judge provided.
Source code in contextguard/verify/judges.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
ContextGuard Verdict Aggregation
This module implements the verdict aggregation logic: 1. Per-claim aggregation: combine multiple evidence assessments into a claim verdict 2. Overall aggregation: combine multiple claim verdicts into an overall verdict
The aggregation layer is the "linker" of the verification compiler. It produces the final executable (verdict report).
Key design decisions: - Critical claim contradiction → overall contradiction - Low coverage → lower confidence - Mixed evidence → MIXED or INSUFFICIENT verdict - Weighted claims affect overall verdict proportionally
AggregationConfig
dataclass
¶
Configuration for verdict aggregation.
Source code in contextguard/verify/aggregate.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
ClaimAggregator ¶
Aggregates evidence assessments into a claim verdict.
Strategy: 1. Find best support and contradict scores 2. Calculate coverage (unique sources) 3. Apply decision rules 4. Compute confidence
Source code in contextguard/verify/aggregate.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
aggregate ¶
aggregate(claim, judge_results, accepted_chunks=0, rejected_chunks=0, trace=None, trace_parents=None)
Aggregate judge results into a claim verdict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claim
|
Claim
|
The claim being verified |
required |
judge_results
|
List[JudgeResult]
|
Results from judging claim against evidence |
required |
accepted_chunks
|
int
|
Number of chunks that passed gating |
0
|
rejected_chunks
|
int
|
Number of chunks that failed gating |
0
|
Returns:
| Type | Description |
|---|---|
ClaimVerdict
|
ClaimVerdict with label, confidence, and evidence |
Source code in contextguard/verify/aggregate.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
OverallAggregator ¶
Aggregates claim verdicts into an overall verdict.
Strategy: 1. Weight claims by importance (weight + critical flag) 2. Check for critical contradictions 3. Calculate weighted verdict distribution 4. Apply decision rules
Source code in contextguard/verify/aggregate.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
aggregate ¶
aggregate(claim_verdicts, trace=None, trace_parents=None)
Aggregate claim verdicts into overall verdict.
Returns:
| Type | Description |
|---|---|
Tuple[VerdictLabel, float, List[ReasonCode]]
|
(overall_label, overall_confidence, warnings) |
Source code in contextguard/verify/aggregate.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
aggregate_claim ¶
aggregate_claim(claim, judge_results, config=None, trace=None, trace_parents=None)
Convenience function to aggregate a single claim.
Source code in contextguard/verify/aggregate.py
517 518 519 520 521 522 523 524 525 526 527 528 | |
aggregate_overall ¶
aggregate_overall(claim_verdicts, config=None, trace=None, trace_parents=None)
Convenience function to aggregate overall verdict.
Source code in contextguard/verify/aggregate.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | |
verdict_summary ¶
verdict_summary(claim_verdicts)
Generate a summary of claim verdicts.
Source code in contextguard/verify/aggregate.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
ContextGuard Report Generation
This module generates the final verdict report in multiple formats: - JSON: For programmatic access - Markdown: For human reading - Context Pack: For safe RAG generation
The report is the PRIMARY OUTPUT of ContextGuard.
ReportBuilder ¶
Builds VerdictReport from aggregated results.
Source code in contextguard/verify/report.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
add_claim_verdict ¶
add_claim_verdict(verdict)
Add a claim verdict to the report.
Source code in contextguard/verify/report.py
50 51 52 | |
add_warning ¶
add_warning(warning)
Add a warning to the report.
Source code in contextguard/verify/report.py
54 55 56 57 | |
build ¶
build(overall_label, overall_confidence, *, report_id=None, created_at=None, llm_model=None, llm_prompt_version=None, llm_temperature=None, retrieval_plan=None, seed=None)
Build the final report.
Source code in contextguard/verify/report.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
set_retrieval_stats ¶
set_retrieval_stats(total, accepted, rejected)
Set retrieval statistics.
Source code in contextguard/verify/report.py
59 60 61 62 63 64 65 66 67 68 | |
ReportRenderer ¶
Renders VerdictReport to various formats with a stable schema.
Source code in contextguard/verify/report.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
canonical_dict
classmethod
¶
canonical_dict(report)
Canonical, stable JSON-ready structure.
Source code in contextguard/verify/report.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
to_dict
classmethod
¶
to_dict(report)
Render report as dictionary (canonical schema).
Source code in contextguard/verify/report.py
276 277 278 279 | |
to_html
staticmethod
¶
to_html(report)
Render report as HTML (basic).
Source code in contextguard/verify/report.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
to_json
classmethod
¶
to_json(report, indent=2)
Render report as JSON (canonical schema).
Source code in contextguard/verify/report.py
271 272 273 274 | |
to_markdown
staticmethod
¶
to_markdown(report)
Render report as Markdown with evidence and rejected tables.
Source code in contextguard/verify/report.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
build_report ¶
build_report(thread_id, state, claim_verdicts, overall_label, overall_confidence, warnings=None, retrieval_stats=None)
Convenience function to build a report.
Source code in contextguard/verify/report.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
render_report ¶
render_report(report, format='markdown')
Convenience function to render a report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
report
|
VerdictReport
|
The report to render |
required |
format
|
str
|
"markdown", "json", or "html" |
'markdown'
|
Source code in contextguard/verify/report.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
save_report ¶
save_report(report, filepath, format=None)
Save report to file.
Format is inferred from file extension if not specified.
Source code in contextguard/verify/report.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
LangChain retriever adapter for ContextGuard.
Design (template-method style):
- Wraps any LangChain retriever that returns Document objects.
- Converts each Document into a ContextGuard Chunk (with full Provenance).
- Applies a lightweight post-filter using CanonicalFilters when the backend
cannot apply them natively.
Customization / extension points:
- doc_to_chunk: inject your own mapping (e.g., custom provenance fields,
entity extraction, doc_type normalization).
- Override _lc_search to support bespoke retrieval calls.
- Override _matches_filters to add richer constraints (e.g., language, tags).
This follows the template-method pattern: _search_impl orchestrates; hooks
handle backend-specific behavior.
LangChainRetrieverAdapter ¶
Bases: RetrieverBase
Adapter that makes a LangChain retriever conform to ContextGuard's Retriever.
Typical use
from langchain.retrievers import YourRetriever lc = YourRetriever(...) adapter = LangChainRetrieverAdapter(lc, source_type=SourceType.SECONDARY) chunks = adapter.search("acme 2024 revenue", filters=CanonicalFilters(...), k=5)
Source code in contextguard/adapters/langchain.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
LlamaIndex retriever adapter for ContextGuard.
Design (template-method style):
- Wraps any LlamaIndex retriever/query engine exposing .retrieve(query).
- Converts NodeWithScore results into ContextGuard Chunk with Provenance.
- Applies lightweight post-filtering via CanonicalFilters when the backend
cannot apply them natively.
Customization / extension points:
- node_to_chunk: inject custom mapping (provenance, metadata normalization).
- Override _li_search to support custom retrieval calls.
- Override _matches_filters to enforce richer constraints.
LlamaIndexRetrieverAdapter ¶
Bases: RetrieverBase
Adapter that makes a LlamaIndex retriever conform to ContextGuard's Retriever.
Typical use
li = index.as_retriever() adapter = LlamaIndexRetrieverAdapter(li, source_type=SourceType.PRIMARY) chunks = adapter.search("acme 2024 revenue", filters=CanonicalFilters(...), k=5)
Source code in contextguard/adapters/llamaindex.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Chroma retriever adapter for ContextGuard.
Design (template-method style):
- Wraps a Chroma collection (client or persistent) and uses metadata filters.
- Converts Chroma results into ContextGuard Chunk with full Provenance.
Requirements:
- Optional dependency: chromadb.
- User must supply an embedding function that maps text -> vector.
Customization:
- Override _build_query to change how queries are constructed (e.g., add
n_results logic).
- Override _convert_result to map Chroma documents/metadata to Chunk.
- Override _matches_filters to add richer filtering beyond Chroma metadata.
ChromaRetrieverAdapter ¶
Bases: RetrieverBase
Adapter for Chroma collections.
Usage
import chromadb client = chromadb.Client() collection = client.get_collection("my_collection") adapter = ChromaRetrieverAdapter(collection, embed_fn=my_embed_fn) chunks = adapter.search("acme 2024 revenue", filters=CanonicalFilters(...), k=5)
Source code in contextguard/adapters/chroma.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
Qdrant retriever adapter for ContextGuard.
Design (template-method style):
- Wraps a Qdrant client and collection name.
- Uses an embedding function to convert queries to vectors.
- Translates CanonicalFilters to Qdrant Filter conditions.
Requirements:
- Optional dependency: qdrant-client.
- User supplies embed_fn (text -> List[float]).
Customization:
- Override _build_filter to map more metadata fields.
- Override _convert_point to add richer provenance/metadata mapping.
QdrantRetrieverAdapter ¶
Bases: RetrieverBase
Adapter for Qdrant collections.
Usage
from qdrant_client import QdrantClient client = QdrantClient(url="http://localhost:6333") adapter = QdrantRetrieverAdapter( client=client, collection="my_collection", embed_fn=my_embed_fn, ) chunks = adapter.search("acme 2024 revenue", filters=CanonicalFilters(...), k=5)
Source code in contextguard/adapters/qdrant.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
OpenAI provider for LLMJudge (implements LLMProvider protocol).
Design (strategy pattern):
- Implements the LLMProvider protocol used by LLMJudge.
- Minimal JSON-only call to OpenAI Chat Completions.
- build_messages is overrideable to customize system/user prompts.
Usage
from contextguard.adapters.openai_provider import OpenAIProvider from contextguard import LLMJudge llm = OpenAIProvider(model="gpt-4o-mini") judge = LLMJudge(llm)
Customization:
- Subclass and override build_messages to inject domain/system prompts.
- You can also wrap this provider with your own retry/backoff/debias layer.
Notes:
- Optional dependency: requires openai>=1.0.0.
- Network calls are not retried here; wrap externally if needed.
OpenAIProvider ¶
Bases: LLMProviderBase
Thin wrapper over the OpenAI chat completion API.
Source code in contextguard/adapters/openai_provider.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
complete_json ¶
complete_json(prompt, schema, temperature=0.0)
Returns parsed JSON according to the judge's schema.
Source code in contextguard/adapters/openai_provider.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
Budgeted provider for LLMJudge (decorator over LLMProvider).
Features: - Enforces max prompt length (in characters) and max output tokens before calling the underlying provider. - Optional logging for budget violations.
BudgetedProvider ¶
Bases: LLMProviderBase
Wraps an LLMProvider and enforces prompt/output budgets.
Source code in contextguard/adapters/budgeted_provider.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
Retrying/logging wrapper for LLM providers.
Patterns:
- Decorator/strategy: wraps any LLMProvider and adds retry with exponential
backoff and jitter, plus structured logging.
- Composable: can be stacked with other providers (e.g., OpenAIProvider, your
custom provider).
Usage
base = OpenAIProvider(model="gpt-4o-mini") llm = RetryingProvider(base, max_attempts=3, base_delay=0.5) judge = LLMJudge(llm)
Customization:
- Override _sleep for testability.
- Override _log to integrate with your observability stack.
RetryingProvider ¶
Bases: LLMProviderBase
Wraps an LLMProvider with retry/backoff and logging.
Source code in contextguard/adapters/retrying_provider.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
ContextGuard SQLite Store
Default storage implementation using SQLite for zero-ops deployment.
Features: - Single-file database (works in notebooks, CLI, tests) - In-memory option for testing - Automatic schema creation - Thread-safe for basic use cases
This is the "Simon-ish" choice: simple, inspectable, works everywhere.
SQLiteStore ¶
Bases: Store
SQLite-backed storage for ContextGuard.
Usage
store = SQLiteStore("contextguard.db") store.save_state("thread_1", state) loaded = store.load_state("thread_1")
For in-memory (testing): store = SQLiteStore(":memory:")
Source code in contextguard/stores/sqlite.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
__init__ ¶
__init__(db_path='contextguard.db', create_tables=True)
Initialize SQLite store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
str
|
Path to SQLite database file, or ":memory:" for in-memory |
'contextguard.db'
|
create_tables
|
bool
|
Whether to create tables if they don't exist |
True
|
Source code in contextguard/stores/sqlite.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
add_fact ¶
add_fact(thread_id, fact_text, provenance, confidence, scope=None)
Add a fact to the store.
Source code in contextguard/stores/sqlite.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
close ¶
close()
Close the database connection.
Source code in contextguard/stores/sqlite.py
439 440 441 442 443 | |
delete_fact ¶
delete_fact(fact_id)
Delete a fact by ID.
Source code in contextguard/stores/sqlite.py
326 327 328 329 330 | |
delete_state ¶
delete_state(thread_id)
Delete state for a thread.
Source code in contextguard/stores/sqlite.py
193 194 195 196 197 198 199 200 | |
get_fact ¶
get_fact(fact_id)
Get a fact by ID.
Source code in contextguard/stores/sqlite.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
get_run ¶
get_run(run_id)
Get a run by ID.
Source code in contextguard/stores/sqlite.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
get_stats ¶
get_stats()
Get storage statistics.
Source code in contextguard/stores/sqlite.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
get_trace ¶
get_trace(run_id)
Get the trace graph for a run.
Source code in contextguard/stores/sqlite.py
421 422 423 424 425 426 427 428 429 430 431 432 433 | |
list_runs ¶
list_runs(thread_id=None, limit=100)
List runs, optionally filtered by thread.
Source code in contextguard/stores/sqlite.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
list_threads ¶
list_threads()
List all thread IDs with stored state.
Source code in contextguard/stores/sqlite.py
202 203 204 205 206 | |
load_state ¶
load_state(thread_id)
Load state for a thread.
Source code in contextguard/stores/sqlite.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
query_facts ¶
query_facts(thread_id=None, entity_ids=None, year=None, min_confidence=0.0)
Query facts by filters.
Source code in contextguard/stores/sqlite.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
save_run ¶
save_run(thread_id, report, trace=None, input_content=None)
Save a verification run.
Source code in contextguard/stores/sqlite.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
save_state ¶
save_state(thread_id, state)
Save state for a thread.
Source code in contextguard/stores/sqlite.py
179 180 181 182 183 184 185 186 187 188 189 190 191 | |
vacuum ¶
vacuum()
Reclaim unused space in the database.
Source code in contextguard/stores/sqlite.py
445 446 447 448 | |
create_store ¶
create_store(path='contextguard.db', in_memory=False)
Create a SQLite store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to database file |
'contextguard.db'
|
in_memory
|
bool
|
If True, use in-memory database (ignores path) |
False
|
Source code in contextguard/stores/sqlite.py
482 483 484 485 486 487 488 489 490 491 492 493 494 | |
get_default_store ¶
get_default_store()
Get the default store (contextguard.db in current directory).
Source code in contextguard/stores/sqlite.py
497 498 499 | |
Cloud store adapter (S3-compatible) for ContextGuard.
Design:
- Implements the Store protocol using an S3-compatible bucket.
- Uses JSON blobs for state/fact/run data. Traces are stored as JSON.
- Minimal, dependency-light: requires boto3 only when used.
Customization / extension:
- Override key templates (state_key, fact_key, run_key) to align with
your org’s layout.
- Subclass to add encryption, compression, or metadata tagging.
S3Store ¶
Bases: Store
S3-backed store implementing the Store protocol.
Note: This is a thin adapter; it assumes bucket-level permissions are already in place. Network and AWS credentials are outside this library’s scope.
Source code in contextguard/stores/cloud.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
Async runner for the ContextGuard pipeline (plan → retrieve → gate → judge → aggregate).
Design:
- Uses asyncio to parallelize retrieval across plan steps while keeping the
existing synchronous components unchanged (wrapped via asyncio.to_thread).
- Provides a single entry point async_run_verification that mirrors the
synchronous flow.
Customization / extension points:
- Swap in any Retriever that has a synchronous search; async wrapper handles
concurrency via thread pool. For fully async retrievers, override
_aretrieve to call native async methods.
- Override build_judge to change judge type (LLMJudge/NLI/etc.) or inject
domain-specific judges.
async_run_verification
async
¶
async_run_verification(claims, state, retriever, *, judge=None, total_k=20, trace=None, profile=None, logger=None, instrumentation=None, max_concurrent_tasks=None)
Asynchronous end-to-end verification runner.
Returns:
| Type | Description |
|---|---|
Tuple[VerdictLabel, float, List[ClaimVerdict]]
|
overall_label, overall_confidence, claim_verdicts |
Source code in contextguard/pipeline/async_runner.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
Generation utilities for ContextGuard.
Goal:
- Provide a thin, overrideable way to turn a ContextPack + user prompt into
a guarded answer. This does not replace your main application generation
stack; it is a reference implementation and an integration pattern.
Design:
- Generator protocol: strategy interface for generation.
- LLMGenerator: uses an LLMProvider (same protocol as LLMJudge) to
produce a JSON answer, ensuring structured output and easy parsing.
Customization / extension points:
- Override LLMGenerator.build_prompt to change how context is formatted.
- Override LLMGenerator.build_schema to change required fields or add
safety tags.
- Provide your own Generator implementation (e.g., retrieval-augmented
streaming, guarded pipelines with red-team filters).
Generator ¶
Bases: Protocol
Strategy interface for producing a response from a context pack.
Source code in contextguard/generate/generator.py
30 31 32 33 34 | |
LLMGenerator ¶
Bases: Generator
Reference generator that uses an LLMProvider to produce a JSON answer.
Pattern: - Build a constrained prompt that reminds the model to stay within the context pack. - Request JSON with a small schema to simplify parsing and downstream validation. - Intended to be swapped out or subclassed for domain-specific generation.
Source code in contextguard/generate/generator.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
build_prompt ¶
build_prompt(user_prompt, context_pack)
Build a guarded prompt: - Echo the user request. - Provide the curated facts-first context pack. - Remind the model to refuse answers that cannot be supported.
Source code in contextguard/generate/generator.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
build_schema ¶
build_schema()
JSON schema to enforce structured, machine-readable output. Override to add more fields (e.g., citations array, confidence).
Source code in contextguard/generate/generator.py
72 73 74 75 76 77 78 79 80 81 82 83 84 | |