API Reference
This section documents the public API of veildata.
Main Interface
- class veildata.RegexRedactor(pattern: str, redaction_token: str = '[REDACTED_{counter}]', store: TokenStore | None = None)[source]
Bases:
ModuleRedact substrings in text using a regex pattern, optionally tracking reversibility.
- class veildata.TokenStore[source]
Bases:
objectA reversible token store that maps redacted tokens back to original values.
Example
store = TokenStore() store.record(“[REDACTED_1]”, “john.doe@example.com”) text = store.reveal(“Contact [REDACTED_1]”)
Redactors
The core of VeilData is the Redactor interface.
- class veildata.redactors.regex.RegexRedactor(pattern: str, redaction_token: str = '[REDACTED_{counter}]', store: TokenStore | None = None)[source]
Bases:
ModuleRedact substrings in text using a regex pattern, optionally tracking reversibility.
- class veildata.redactors.ner_spacy.SpacyNERRedactor(model: str = 'en_core_web_sm', entities: list[str] | None = None, redaction_token: str = '[REDACTED_{counter}]', store: TokenStore | None = None)[source]
Bases:
ModuleRedact named entities in text using a spaCy model, with optional reversible tracking.
- class veildata.redactors.ner_bert.BERTNERRedactor(model_name: str = 'dslim/bert-base-NER', redaction_token: str = '[REDACTED_{counter}]', store: TokenStore | None = None, device: str | None = None, use_fp16: bool = False)[source]
Bases:
ModuleRedact named entities in text using a fine-tuned BERT NER model.
- train(mode: bool = True) BERTNERRedactor[source]
Toggle train/eval mode on BERT model.
Composition & Pipeline
Reversibility
- class veildata.revealers.TokenStore[source]
Bases:
objectA reversible token store that maps redacted tokens back to original values.
Example
store = TokenStore() store.record(“[REDACTED_1]”, “john.doe@example.com”) text = store.reveal(“Contact [REDACTED_1]”)
Configuration
- class veildata.core.config.BertConfig(*, enabled: bool = False, model_path: str = 'dslim/bert-base-NER', threshold: float = 0.5, label_mapping: Dict[str, str] | None = None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class veildata.core.config.GlobalOptions(*, hybrid: ~veildata.core.config.HybridOptions = <factory>, fallback: str = 'plaintext')[source]
Bases:
BaseModel- hybrid: HybridOptions
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class veildata.core.config.HybridOptions(*, strategy: str = 'union', prefer: str = 'ml')[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class veildata.core.config.MLConfig(*, spacy: ~veildata.core.config.SpacyConfig = <factory>, bert: ~veildata.core.config.BertConfig = <factory>)[source]
Bases:
BaseModel- bert: BertConfig
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- spacy: SpacyConfig
- class veildata.core.config.RedactionMethod(*values)[source]
-
- HYBRID = 'hybrid'
- NER_BERT = 'ner_bert'
- NER_SPACY = 'ner_spacy'
- REGEX = 'regex'
- class veildata.core.config.SpacyConfig(*, enabled: bool = False, model: str = 'en_core_web_lg', pii_labels: List[str] | None = None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class veildata.core.config.TraversalConfig(*, policy: ~veildata.core.config.TraversalPolicy = TraversalPolicy.ALLOW, keys_to_redact: ~typing.List[str] = <factory>, keys_to_ignore: ~typing.List[str] = <factory>, max_depth: int = 100)[source]
Bases:
BaseModelConfiguration for JSON traversal.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- policy: TraversalPolicy
- class veildata.core.config.VeilConfig(*, method: ~veildata.core.config.RedactionMethod = RedactionMethod.REGEX, patterns: ~typing.Dict[str, str] | None = None, pattern: ~typing.Dict[str, str] | None = None, ml: ~veildata.core.config.MLConfig = <factory>, traversal: ~veildata.core.config.TraversalConfig = <factory>, options: ~veildata.core.config.GlobalOptions = <factory>)[source]
Bases:
BaseModelRoot configuration for VeilData.
- get_patterns() Dict[str, str][source]
Helper to get patterns from either ‘patterns’ or ‘pattern’ field.
- method: RedactionMethod
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- options: GlobalOptions
- traversal: TraversalConfig
- veildata.core.config.load_config(config_path: str | None = None, verbose: bool = False) VeilConfig[source]
Load configuration from a file or environment variables.
- Parameters:
config_path – Path to the config file (YAML, JSON, TOML).
verbose – Whether to print loading status.
- Returns:
VeilConfig object.
- Raises:
ConfigMissingError – If specified config file is not found.
ValidationError – If config is invalid.
Engine
- veildata.engine.build_redactor(method: str = 'regex', detect_mode: str = 'rules', config_path: str | None = None, ml_config_path: str | None = None, verbose: bool = False, config: VeilConfig | None = None) Tuple[Module, TokenStore][source]
Factory function to build a redactor based on configuration.
Streaming
Streaming redaction buffer for chunk-based processing.
This module provides a buffer that can process text in chunks while correctly detecting and redacting entities that span across chunk boundaries.
- class veildata.streaming_buffer.ChunkMetadata(chunk_index: int, input_size: int, output_size: int, buffer_size: int, entities_detected: int)[source]
Bases:
objectMetadata about a processed chunk for debugging and tracking.
- class veildata.streaming_buffer.StreamingRedactionBuffer(pipeline: DetectionPipeline, overlap_size: int = 512, store: TokenStore | None = None, redaction_format: str = '[REDACTED_{counter}]')[source]
Bases:
objectBuffer for streaming redaction with cross-chunk entity detection.
This class maintains a sliding window to ensure entities that span chunk boundaries are correctly detected and redacted. It progressively yields sanitized output while maintaining minimal state.
Example
>>> from veildata.detectors import RegexDetector >>> from veildata.pipeline import DetectionPipeline >>> detector = RegexDetector({"EMAIL": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"}) >>> pipeline = DetectionPipeline(detector) >>> buffer = StreamingRedactionBuffer(pipeline, overlap_size=20) >>> for chunk in ["My email is john.do", "[email protected] and more"]: ... output = buffer.add_chunk(chunk) ... print(output, end="") >>> print(buffer.finalize())
- Parameters:
pipeline – DetectionPipeline to use for entity detection
overlap_size – Number of characters to retain between chunks for boundary detection
store – Optional TokenStore to record redacted mappings
redaction_format – Format string for redaction tokens (default: “[REDACTED_{counter}]”)
- add_chunk(chunk: str) str[source]
Add a chunk of text and return the redacted output for the safe zone.
The safe zone is the portion of the buffer that is guaranteed not to contain any entities that might continue in the next chunk.
- Parameters:
chunk – New text chunk to process
- Returns:
Redacted text for the safe portion of the buffer
- finalize() str[source]
Process any remaining text in the buffer.
This should be called after all chunks have been added to ensure the overlap region is processed.
- Returns:
Redacted text for the remaining buffer contents
- get_metadata() List[ChunkMetadata][source]
Get metadata about all processed chunks.
- Returns:
List of ChunkMetadata objects
- veildata.streaming_buffer.stream_redact(chunks: Generator[str, None, None], pipeline: DetectionPipeline, overlap_size: int = 512, store: TokenStore | None = None) Generator[str, None, None][source]
Convenience function for streaming redaction.
This is a simple wrapper around StreamingRedactionBuffer for common use cases.
- Parameters:
chunks – Generator yielding text chunks
pipeline – DetectionPipeline to use for entity detection
overlap_size – Number of characters to retain between chunks
store – Optional TokenStore to record redacted mappings
- Yields:
Redacted text chunks
Example
>>> def read_file_chunks(filepath, chunk_size=4096): ... with open(filepath, 'r') as f: ... while True: ... chunk = f.read(chunk_size) ... if not chunk: ... break ... yield chunk >>> >>> from veildata.engine import build_redactor >>> redactor, store = build_redactor(method="regex") >>> for redacted_chunk in stream_redact(read_file_chunks("input.txt"), redactor): ... print(redacted_chunk, end="")