AISchemaGate

Python + TypeScript SDK · non-streaming in v1

Stop LLMs & AI Agents from Outputting Bad JSON.

Patent-Pending Sub-15ms Semantic Enforcement Engine for AI Outputs. Enforce strict JSON schemas, catch out-of-range and off-enum values the contract declares, and sanitize PII before bad data hits your application logic or database.

$ pip install aischemagate$ npm i @nightmoose/aischemagate
import os
from aischemagate import LLMGate
from openai import OpenAI

openai_client = OpenAI()
gate = LLMGate(
    api_key=os.environ["CONTRACTGATE_API_KEY"],
    contract_id="extract_invoice_v1",
    llm=openai_client,
)

result = gate.completion(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Extract invoice JSON from: ..."}],
    response_format={"type": "json_object"},
    auto_repair=True,
)
if result.ok:
    store_invoice(result.payload)   # sanitized; safe to forward
contract_id takes the contract's name or UUID. Same API key as DataContractGate.
Works withOpenAIAnthropicvLLMOllamaLangChainCrewAIMCP agents

How it works

  1. 1

    Write the contract from real outputs

    Hand 5–20 outputs your pipeline would accept to the inference endpoint, then tighten the YAML: enums, ranges, patterns, required fields.

  2. 2

    Put the gate after the model call

    LLMGate pulls the JSON out of OpenAI, Anthropic, vLLM or Ollama responses and validates it against the deployed contract.

  3. 3

    Forward only what passed

    A pass returns the sanitized payload. A failure returns the violations, and with auto-repair on, the model gets them back as a re-prompt.

The contract is the schema, and then some

Types and required fields catch malformed output. Patterns, enums and ranges catch the plausible-looking values a JSON schema lets through: an invoice id in the wrong shape, a currency the model invented, a negative amount. The gate reports each one as a violation your code can act on.

name: "extract_invoice_v1"
ontology:
  entities:
    - name: invoice_id
      type: string
      required: true
      pattern: "^INV-[0-9]{6}$"
    - name: amount
      type: number
      required: true
      min: 0
      max: 1000000
    - name: currency
      type: string
      required: true
      enum: ["USD", "EUR", "GBP"]

What you get

Sub-15ms Output Gate

Real-time semantic validation of LLM outputs.

Auto-Infer Contracts

Generate schema contracts automatically from 5–20 known-good JSON outputs.

Auto-Repair

Re-prompt automatically up to a bounded retry budget when validation fails. Replay quarantined events against a corrected contract version.

Boundary PII Masking

Strip sensitive data before sending prompts out or storing model responses.

One engine, two front doors

AISchemaGate runs on the DataContractGate engine. Same API key, same dashboard, same quarantine and audit log. Validate what your models return here; validate what your pipelines ingest there.