Skip to main content
defineContract and enforce wrap a pipeline of five primitives. Each is exported individually so you can build custom flows — testing, manual prompt engineering, or entirely custom retry strategies.
All five are pure, synchronous, and side-effect free.

clean(raw)

Normalizes raw LLM output into a parsed JSON value. Handles:
  • Stripping code fences ( json … )
  • De-prosing (removing leading “Here’s the JSON:” chatter)
  • Finding the first valid JSON object or array
  • Returning null when nothing parseable is found

verify(data, schema, rules?)

Validates data against a schema and rules. No LLM involved — pure sync validation. Returns the same ContractResult<T> shape that contract.accept returns.
Perfect for unit tests — no mocking, no network.

classify(raw, cleaned)

Given the raw LLM output and the result of clean(raw), return the failure category:
Useful when you’re building your own repair loop and want the same categorization as Boundary’s built-in loop.

repair(detail, overrides?)

Given a failed attempt detail, generate repair messages. Returns false if the category is explicitly disabled via overrides.

instructions(schema)

Generate prompt instructions derived from a Zod schema — the same text that contract.accept auto-injects into attempt.instructions.
Call it once and paste the output into your system prompt if you want to control timing (e.g. to hit a prompt cache). The primitive does not take a public options object. If you want extra text appended to the instructions used by contract.accept, set instructions.suffix on defineContract.

Recipes

Schema-first unit tests

Test that your rules behave correctly without running an LLM:

Manual prompt, no loop

Skip contract.accept’s loop and drive the LLM yourself when you need full control (token budgeting, streaming, custom retry policy):

Classify a failure from another system

Got a raw LLM output and a validation error from somewhere else? Use classify to bucket it the same way Boundary would:

See also

enforce vs defineContract

Which entrypoint to pick

Testing contracts

No LLM, no network, deterministic tests