LLM Output Schema Validation: Strukturierte Ausgaben absichern 2026
LLM-Ausgaben sind untrusted Input für alle downstream-Systeme. Ohne Schema-Validierung können prompt-injizierte Ausgaben Datenbanken korrumpieren, APIs missbrauchen oder PII leaken.
Define JSON Schema for LLM Outputs
Define a strict schema for every LLM output type. Reject anything that doesn't conform — never pass unvalidated output downstream.
# JSON Schema for agent decision output
{
"type": "object",
"required": ["action", "confidence", "reasoning"],
"additionalProperties": false,
"properties": {
"action": { "type": "string", "enum": ["approve","reject","escalate"] },
"confidence": { "type": "number", "minimum": 0, "maximum": 1 },
"reasoning": { "type": "string", "maxLength": 500 }
}
}Moltbot Output Validation Pipeline
Every LLM output passes through schema validation, PII scanning, and content policy checks before reaching downstream systems.
# Moltbot output pipeline config
output_pipeline:
stages:
- name: schema_validation
schema_file: "schemas/agent-decision.json"
on_failure: reject_with_fallback
- name: pii_scan
patterns: [email, phone, credit_card, iban]
on_detection: redact
- name: content_policy
rules: [no_system_prompt_leak, no_credentials]
on_violation: reject_and_log
fallback:
action: "escalate"
confidence: 0
reasoning: "Validation failed — human review required"Pydantic Type Safety (Python Agents)
Use Pydantic BaseModel for type-safe LLM output parsing with automatic validation and safe fallbacks.
from pydantic import BaseModel, Field, validator
from typing import Literal
class AgentDecision(BaseModel):
action: Literal["approve", "reject", "escalate"]
confidence: float = Field(ge=0, le=1)
reasoning: str = Field(max_length=500)
def parse_output(raw: str) -> AgentDecision:
try:
return AgentDecision.model_validate_json(raw)
except Exception as e:
log_validation_error(e, raw)
return AgentDecision(
action="escalate", confidence=0.0,
reasoning="Validation failed — human review required"
)PII Detection & Redaction
LLMs can accidentally include PII from context. Automatic detection and redaction before output reaches any downstream system.
# Moltbot PII scanner
pii_scanner:
enabled: true
mode: redact
detectors:
- type: regex
name: email
pattern: '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+'
replacement: "[EMAIL REDACTED]"
- type: ml_model
name: presidio
entities: [PERSON, IBAN, CREDIT_CARD]
confidence_threshold: 0.8
action: redact
audit:
log_all_detections: true
alert_on: [CREDIT_CARD, IBAN]Häufige Fragen
Why is LLM output schema validation a security control, not just quality control?
How do I handle LLM outputs that fail schema validation?
What is the difference between output filtering and schema validation?
Weiterführende Ressourcen
LLM-Ausgaben automatisch validieren?
Moltbot erzwingt Schema-Validierung und PII-Redaktion für alle LLM-Ausgaben — konfigurierbar, auditierbar.
🛡️ Kostenloser Security Check