Skip to main content

lexigram-ai-guard

AI input/output guard pipeline for the Lexigram Framework — LLM safety and content filtering


Overview

Content safety and guardrails for the Lexigram Framework. Runs an ordered pipeline of input and output guards against every LLM call — blocking prompt injections, redacting PII, enforcing length limits, restricting topics, and optionally using an LLM classifier for advanced jailbreak detection. Zero-config usage starts with sensible defaults.

Install

uv add lexigram-ai-guard

Quick Start

from lexigram import Application
from lexigram.di.module import Module, module

from lexigram.ai.guard import GuardModule
from lexigram.ai.guard.config import GuardConfig

@module(imports=[
    GuardModule.configure(
        GuardConfig(
            injection_detection=True,
            pii_detection=True,
            pii_action="redact",
            max_input_chars=8000,
        )
    )
])
class AppModule(Module):
    pass

app = Application(modules=[AppModule])
if __name__ == "__main__":
    app.run()

Configuration

Zero-config usage: Call GuardModule.configure() with no arguments to use defaults.

Option 1 — YAML file

# application.yaml
ai_guard:
  injection_detection: true
  pii_detection: true
  pii_action: "redact"
  max_input_chars: 8000
  restricted_topics: []

Option 2 — Profiles + Environment Variables (recommended)

export LEX_AI_GUARD__PII_ACTION=redact
# Environment variables for each field

Option 3 — Python

from lexigram.ai.guard.config import GuardConfig
from lexigram.ai.guard import GuardModule

config = GuardConfig(
    injection_detection=True,
    pii_action="redact",
    max_input_chars=8000,
    restricted_topics=["violence", "adult_content"],
)
GuardModule.configure(config)

Config reference

Field Default Env var Description
enabled True LEX_AI_GUARD__ENABLED Master on/off switch
injection_detection True LEX_AI_GUARD__INJECTION_DETECTION Enable heuristic prompt injection detector
injection_action "block" LEX_AI_GUARD__INJECTION_ACTION Action on injection: "block" or "warn"
pii_detection True LEX_AI_GUARD__PII_DETECTION Enable PII detection on user inputs
pii_action "redact" LEX_AI_GUARD__PII_ACTION Action on PII: "redact", "block", or "warn"
pii_entities [] LEX_AI_GUARD__PII_ENTITIES PII entity types to scan
pii_redaction_output True LEX_AI_GUARD__PII_REDACTION_OUTPUT Apply PII redaction to LLM outputs
max_input_chars 0 LEX_AI_GUARD__MAX_INPUT_CHARS Maximum input character count
max_output_chars 0 LEX_AI_GUARD__MAX_OUTPUT_CHARS Maximum output character count
length_action "block" LEX_AI_GUARD__LENGTH_ACTION Action when a length limit is exceeded
restricted_topics [] LEX_AI_GUARD__RESTRICTED_TOPICS Topic keywords to block
enable_llm_guards False LEX_AI_GUARD__ENABLE_LLM_GUARDS Use LLM for advanced injection/jailbreak detection
guard_model "gpt-4o-mini" LEX_AI_GUARD__GUARD_MODEL Model used for LLM-based guards
llm_guard_threshold 0.7 LEX_AI_GUARD__LLM_GUARD_THRESHOLD Confidence threshold for LLM guard action
sensitivity_level "medium" LEX_AI_GUARD__SENSITIVITY_LEVEL Guard aggressiveness: "low", "medium", "high"

Module Factory Methods

Method Description
GuardModule.configure(config) Configure with explicit config
GuardModule.stub(config) Minimal config for testing

Key Features

  • Input guards: PromptInjectionDetector, PIIDetector, TopicRestrictor, InputLengthGuard
  • Output guards: PIIRedactor, OutputLengthGuard
  • LLM-based guards: Optional LLMInjectionDetector and LLMJailbreakDetector
  • Guard results: AggregateGuardResult with passed, blocked, redacted, warned states
  • @guarded decorator: Function-level guard attachment
  • Lifecycle hooks: GuardInputCheckedHook, GuardOutputCheckedHook, GuardPipelineCompletedHook

Testing

async with Application.boot(modules=[GuardModule.stub(
    GuardConfig(
        injection_detection=True,
        injection_action="block",
    )
)]) as app:
    # your test code
    ...

Key Source Files

File What it contains
src/lexigram/ai/guard/module.py GuardModule.configure(), .stub()
src/lexigram/ai/guard/config.py GuardConfig
src/lexigram/ai/guard/pipeline/guard_pipeline.py GuardPipeline orchestrator
src/lexigram/ai/guard/pipeline/result.py GuardCheckResult, AggregateGuardResult, GuardAction
src/lexigram/ai/guard/input/injection.py PromptInjectionDetector heuristics
src/lexigram/ai/guard/input/pii.py PIIDetector regex patterns
src/lexigram/ai/guard/output/pii_redactor.py PIIRedactor
src/lexigram/ai/guard/di/provider.py GuardProvider boot and registration

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lexigram_ai_guard-0.1.2.tar.gz (46.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lexigram_ai_guard-0.1.2-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file lexigram_ai_guard-0.1.2.tar.gz.

File metadata

  • Download URL: lexigram_ai_guard-0.1.2.tar.gz
  • Upload date:
  • Size: 46.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for lexigram_ai_guard-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f9c4319b61da3a9f1d27f5294e77501f21b011d5aa4f404fcbcb0d82693c5e46
MD5 f07fa546b22cbd7e796616a73c46b1d6
BLAKE2b-256 37781479a4537d2b281255d04e0071d703dcbe985ac4d2df7574bbdfc8a7ecfe

See more details on using hashes here.

File details

Details for the file lexigram_ai_guard-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for lexigram_ai_guard-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dfe0ffccf002c0d62343116964bca3e30faeb0f3de23b71994be90c8f54807dc
MD5 bb4c65793a5456813c61c9d2c4ca80eb
BLAKE2b-256 a896834b41403ff3476ed8ebba33360d59b3c1d18827a791bce0e177f4ee644c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page