@withboundary/contract. All are pure, synchronous, and side-effect free.
clean
| Input | Output |
|---|---|
null / "" | null |
"```json\n{\"a\":1}\n```" | { a: 1 } |
"Here's the answer: {\"a\":1}" | { a: 1 } |
"no json here" | null |
| Malformed JSON | null (or the original raw — callers should still validate) |
verify
data against schema and, if provided, every rule. Returns the same ContractResult<T> shape contract.accept returns. No LLM involved.
result.ok === true→ data passed schema and all rules.result.datais typedT.result.ok === false→result.error.attempts[0]has the failurecategory("VALIDATION_ERROR"or"RULE_ERROR") and the list ofissues.
classify
raw | cleaned | Category |
|---|---|---|
"" / null | — | EMPTY_RESPONSE |
| contains refusal language | null | REFUSAL |
| has text but no JSON | null | NO_JSON |
| starts valid, ends mid-token | null / partial | TRUNCATED |
| malformed JSON | null | PARSE_ERROR |
| valid JSON, wrong types | unknown | VALIDATION_ERROR |
| right types, rule failed | T | RULE_ERROR |
repair
Message[]— messages to feed back to the model.false— the category is disabled (viaoverrides[category] = false), caller should not retry.
overrides shape
false— skip retry for this category.(detail) => Message[]— build custom repair messages for this category.
instructions
.describe() annotations.
suffix option appends additional text verbatim:
contract.accept and available on attempt.instructions.
createConsoleLogger
ContractLogger surface and prints formatted traces to console.log.
ConsoleLoggerOptions
| Option | Type | Default | Description |
|---|---|---|---|
prefix | string | "[contract]" | Prepended to every line |
showInstructions | boolean | false | Print the auto-generated prompt on onAttemptStart |
showRepairs | boolean | false | Print repair messages on onRepairGenerated |
showRawOutput | boolean | false | Print raw on onRawOutput |
showCleanedOutput | boolean | false | Print cleaned on onCleanedOutput |
showSuccessData | boolean | false | Print the accepted data on onRunSuccess |
maxStringLength | number | 1000 | Truncate printed strings over this length |
debug: true option on defineContract / enforce is equivalent to logger: createConsoleLogger().
See also
Engine primitives guide
Recipes and custom pipelines
Testing contracts
Use verify() for unit tests