@nightmoose/aischemagate
v0.4.1
Published
AISchemaGate — thin alias over @nightmoose/contractgate-sdk for LLM structured-output validation. Same engine as DataContractGate.
Maintainers
Readme
@nightmoose/aischemagate
AISchemaGate — thin alias package over
@nightmoose/contractgate-sdk
for LLM structured-output validation. Same Rust engine, same
cg_live_... API keys, same tenant service, same backend as
DataContractGate.
npm install @nightmoose/aischemagateWhy this package exists
The underlying engine, SDK, and API are DataContractGate — one validator,
one gateway. @nightmoose/aischemagate is the LLM / AI-agent-shaped
front door: it re-exports the SDK under a name that reads correctly from
the LLM-output-validation angle. Use whichever import name matches your
mental model; the behavior is identical.
See:
aischemagate.com— product siteaischemagate.dev— developer documentation- RFC-094 (
docs/rfcs/094-sdk-llm-output-guard.md) — SDK design
15-line quickstart
import { LLMGate } from '@nightmoose/aischemagate';
import OpenAI from 'openai';
const gate = new LLMGate({
apiKey: process.env.CONTRACTGATE_API_KEY!,
contractId: 'extract_invoice_v1',
llm: new OpenAI(),
});
const result = await gate.completion({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Extract invoice JSON from: ...' }],
response_format: { type: 'json_object' },
autoRepair: true,
});
if (result.ok) forwardDownstream(result.payload); // egress-sanitizedFor Anthropic, call gate.message({ model: 'claude-sonnet-4-5', ... }).
For vLLM / Ollama OpenAI-compat, call gate.completion(...) unchanged.
Vercel AI SDK guardrail
aischemagateGuardrail is a LanguageModelV4Middleware. It does not depend on the ai package. wrapGenerate reads the JSON object from text content (or a tool-call input) and posts it to POST /egress/{id}. On success the model text is replaced with the egress payload. Set autoRepair: true to re-prompt, up to maxRetries extra calls (hard cap 5), when min, max, pattern, enum, or compliance_mode fails.
import { generateText, Output, wrapLanguageModel } from 'ai';
import { aischemagateGuardrail } from '@nightmoose/aischemagate';
import { z } from 'zod';
const model = wrapLanguageModel({
model: yourModel,
middleware: aischemagateGuardrail({
apiKey: process.env.CONTRACTGATE_API_KEY!,
contractId: process.env.CONTRACTGATE_CONTRACT_ID!,
autoRepair: true,
maxRetries: 2,
}),
});
const result = await generateText({
model,
output: Output.object({
schema: z.object({ total: z.number(), currency: z.string() }),
}),
prompt: 'Extract the invoice as JSON.',
});validateStructuredOutput(options, payload) is the same egress check for a JSON object you already have. Copy-paste guides: docs/integrations/.
Running tests locally
Tests resolve the @nightmoose/contractgate-sdk dependency through
npm install. From this directory:
npm install
npm run build # one-time, or after changes
npm testWhat's new
See CHANGELOG.md and the parent
@nightmoose/contractgate-sdk CHANGELOG —
the alias version tracks initial release only; feature work is versioned
on contractgate-sdk.
