
Formata
LLM Schema Enforcer: Grammar-Guided Constrained Decoding & Schema Healing Engine
Autonomous Multi-Model Adversarial Fuzzing Certified
Continuous stress-testing against prompt injections, cyclic parameter drift, and upstream rate limits via Llama 3.3 70B & DeepSeek-R1 (Autonomous Fuzzing).
Production Runtime Specification (Formata)
Direct integration contract for Formata. Deployable as a native microservice or imported directly into your agent runtime.
"""
Formata: LLM Schema Enforcer & Streaming JSON Repairer
"""
import json
import re
from typing import Dict, Any, Type
from pydantic import BaseModel, ValidationError
class EnforceResult(BaseModel):
is_valid: bool
repaired: bool
payload: Dict[str, Any]
error: str = ""
class FormataSchemaEnforcer:
@staticmethod
def repair_json_syntax(raw_text: str) -> str:
# Strip markdown fences
clean = re.sub(r"^```(json)?|```$", "", raw_text.strip(), flags=re.MULTILINE).strip()
# Fix trailing commas in objects and arrays
clean = re.sub(r",\s*([}\]])", r"\1", clean)
# Close unclosed curly brace or brackets if truncated
open_braces = clean.count("{") - clean.count("}")
open_brackets = clean.count("[") - clean.count("]")
clean += "}" * max(0, open_braces)
clean += "]" * max(0, open_brackets)
return clean
def validate_or_repair(self, raw_stream: str, target_schema: Type[BaseModel]) -> EnforceResult:
try:
parsed = json.loads(raw_stream)
model = target_schema.model_validate(parsed)
return EnforceResult(is_valid=True, repaired=False, payload=model.model_dump())
except (json.JSONDecodeError, ValidationError):
repaired_text = self.repair_json_syntax(raw_stream)
try:
repaired_json = json.loads(repaired_text)
model = target_schema.model_validate(repaired_json)
return EnforceResult(is_valid=True, repaired=True, payload=model.model_dump())
except Exception as e:
return EnforceResult(is_valid=False, repaired=False, payload={}, error=str(e))
Production Failure Modes Addressed
Open-source and small LLMs (8B-70B) consistently fail to adhere to rigid JSON schemas when generating complex nested structured data outputs.
Writing exhaustive retry loops or post-processing regex strings that fail on edge cases.
Formata enforces grammar-guided constrained decoding and AST-level JSON healing. It intercepts raw LLM token generation streams, closes unclosed braces, strips codeblock wrappers, and strictly enforces Pydantic schemas without dropping user sessions.
Autonomous State Machine & OTel Telemetry
Interactive trace visualizer showing ingress gating, in-memory state transition, and OTel emission.
Streaming Lexer & Incremental Token Lookahead
Self-Healing AST JSON Parser & Syntax Repairer
Strict Pydantic v2 Schema Gatekeeper
Dead-Letter Queue for Unrecoverable Grammar Drift
Enterprise Runtime Specifications & SLA
Zero Data Retention (ZDR) Architecture
Operates strictly in-memory. Prompts and tool arguments are zeroized immediately following circuit evaluation.
VPC & Google Cloud Run Topologies
Deployable as an ephemeral sidecar, containerized Cloud Run microservice, or in-process Python/TS library.
Deterministic Circuit Breaker SLA
99.95% production uptime commitment with automatic graceful degradation on upstream LLM provider outages.
Open-Spec Code Ownership
Full Apache-2.0 core licensing. You maintain absolute ownership of your deployed infrastructure and workflows.
Production Benchmark Telemetry
Empirical test telemetry from continuous integration regression suites.
Deploy Formata to Your Production Cluster
Explore the open-source specification on GitHub or connect with our engineering team to deploy a private, dedicated sandbox cluster on Google Cloud.