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

async with Application.boot(modules=[AppModule]) as app:
    # use app.container to resolve services
    ...

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

lexigram_ai_guard-0.1.3007-py3-none-any.whl (44.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for lexigram_ai_guard-0.1.3007-py3-none-any.whl
Algorithm Hash digest
SHA256 b11cd6cc851205280f173443032faf41e11b0af79461a7301a197b892c953701
MD5 8fcd9bfef33ecde48d1cf67a4132e2a2
BLAKE2b-256 02872e4cb5bfdc91cf57a01cd7991beea44a46c0ba4ca294e2fced20a9280c77

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 Sentry Error logging StatusPage Status page