Skip to main content
Constraints let you validate what the LM returns. Add them to output fields to enforce rules like “confidence must be between 0 and 1” or “answer can’t be empty”.

Two Flavors

#[check] - Soft constraint. The call succeeds, but you can see if the check passed or failed. #[assert] - Hard constraint. The call fails if the assertion doesn’t hold.

Check Example

If the LM returns confidence: 1.5, the call still succeeds - you just see passed: false when you inspect the check result. The label is required for checks. It’s how you identify which constraint passed or failed.

Assert Example

If confidence is 1.5, the call returns a PredictError::Parse error. Label is optional for asserts.

Writing Expressions

Use this to refer to the field value:
Boolean logic:
String methods:
Collections:

Inspecting Check Results

Predicted<O> carries per-field metadata including constraint results. Access it via .metadata():

Handling Assert Failures

When an assert fails, you get a PredictError:

Common Patterns

Probability/confidence range:
Non-empty output:
Length limits:
URL format:
Multiple constraints on one field:

Compile-time Errors

If you forget the label on a check:
You get: