Implementation:Protectai Llm guard Vault
| Knowledge Sources | |
|---|---|
| Domains | Security, Data_Privacy, State_Management |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Concrete tool for storing anonymization placeholder-to-original-value mappings provided by the LLM Guard library.
Description
The Vault class is an in-memory tuple store that holds (placeholder, original_value) pairs created during anonymization. It is shared between the Anonymize input scanner (which populates it) and the Deanonymize output scanner (which reads from it). The class provides methods to append, extend, remove, and query stored tuples.
Usage
Import and instantiate this class before creating Anonymize and Deanonymize scanners. Pass the same Vault instance to both scanners to enable reversible anonymization across the LLM call boundary.
Code Reference
Source Location
- Repository: llm-guard
- File: llm_guard/vault.py
- Lines: L4-35
Signature
class Vault:
"""
Stores tuples of (placeholder, original_value) for anonymization/deanonymization.
"""
def __init__(self, tuples: Optional[List[Tuple]] = None):
"""
Args:
tuples: Pre-existing placeholder-to-original mappings. Default creates empty vault.
"""
def append(self, new_tuple: Tuple) -> None:
"""Add a single (placeholder, original) mapping."""
def extend(self, new_tuples: List[Tuple]) -> None:
"""Add multiple mappings at once."""
def remove(self, tuple_to_remove: Tuple) -> None:
"""Remove a specific mapping."""
def get(self) -> List[Tuple]:
"""Return all stored mappings."""
def placeholder_exists(self, placeholder: str) -> bool:
"""Check if a placeholder already exists in the vault."""
Import
from llm_guard.vault import Vault
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tuples | Optional[List[Tuple]] | No | Pre-existing placeholder mappings (default: None creates empty vault) |
Outputs
| Name | Type | Description |
|---|---|---|
| Vault instance | Vault | Object with append, extend, remove, get, placeholder_exists methods |
| get() | List[Tuple] | All stored (placeholder, original_value) tuples |
| placeholder_exists() | bool | Whether a given placeholder string exists in the vault |
Usage Examples
Basic Vault Usage
from llm_guard.vault import Vault
# 1. Create empty vault
vault = Vault()
# 2. Pass to both Anonymize and Deanonymize
from llm_guard.input_scanners import Anonymize
from llm_guard.output_scanners import Deanonymize
anonymize = Anonymize(vault)
deanonymize = Deanonymize(vault)
# 3. After scanning prompt, vault is populated
sanitized_prompt, is_valid, score = anonymize.scan("My name is John Smith")
print(vault.get()) # [("[REDACTED_PERSON_1]", "John Smith")]
# 4. Deanonymize reads from the same vault
output = "Hello [REDACTED_PERSON_1], your request is processed."
restored, _, _ = deanonymize.scan(sanitized_prompt, output)
print(restored) # "Hello John Smith, your request is processed."
Pre-populated Vault
from llm_guard.vault import Vault
# Initialize with existing mappings
vault = Vault(tuples=[
("[REDACTED_PERSON_1]", "Jane Doe"),
("[REDACTED_EMAIL_ADDRESS_1]", "jane@example.com"),
])
# Check for existing placeholders
vault.placeholder_exists("[REDACTED_PERSON_1]") # True
vault.placeholder_exists("[REDACTED_PERSON_2]") # False