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.
clean(raw)
- Stripping code fences (
json …) - De-prosing (removing leading “Here’s the JSON:” chatter)
- Finding the first valid JSON object or array
- Returning
nullwhen nothing parseable is found
verify(data, schema, rules?)
ContractResult<T> shape that contract.accept returns.
classify(raw, cleaned)
clean(raw), return the failure category:
repair(detail, overrides?)
false if the category is explicitly disabled via overrides.
instructions(schema, options?)
contract.accept auto-injects into attempt.instructions.
Recipes
Schema-first unit tests
Test that your rules behave correctly without running an LLM:Manual prompt, no loop
Skipcontract.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? Useclassify 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