Skip to content

Commit 6396ae7

Browse files
committed
docs: Clarify assertion error handling in examples, distinguishing between AxAssertionError for retries and Error for immediate failure.
1 parent 0ef498b commit 6396ae7

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

docs/EXAMPLES.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,27 @@ gen.addAssert(({ next10Numbers }) => {
109109
return true; // Pass validation
110110
});
111111

112-
// Method 3: Throw custom errors for immediate failure
112+
// Method 3: Throw AxAssertionError to trigger retry
113+
import { AxAssertionError } from '@ax-llm/ax';
114+
113115
gen.addAssert(({ next10Numbers }) => {
114116
if (next10Numbers?.some(n => n <= 0)) {
115-
throw new Error(`Invalid numbers found: ${next10Numbers.filter(n => n <= 0)}`);
117+
throw new AxAssertionError({
118+
message: `Invalid numbers found: ${next10Numbers.filter(n => n <= 0)}`
119+
});
120+
}
121+
return true;
122+
});
123+
124+
// Method 4: Throw standard Error for immediate failure (no retries)
125+
gen.addAssert(({ next10Numbers }) => {
126+
if (!next10Numbers) {
127+
throw new Error('Critical: Numbers missing entirely!');
116128
}
117129
return true;
118130
});
119131

120-
// Method 4: Conditional validation with undefined return
132+
// Method 5: Conditional validation with undefined return
121133
gen.addAssert(({ summary }) => {
122134
if (!summary) return undefined; // Skip if summary not provided
123135
return summary.length >= 20; // Only validate if present
@@ -131,10 +143,11 @@ const result = await gen.forward(ai({ name: 'openai' }), {
131143

132144
**Assertion Return Values:**
133145
- `true`: Assertion passes, continue generation
134-
- `false`: Assertion fails, use provided message parameter
135-
- `string`: Assertion fails, use the returned string as error message
146+
- `false`: Assertion fails, triggers retry with provided message
147+
- `string`: Assertion fails, triggers retry with returned string as error message
136148
- `undefined`: Skip this assertion (useful for conditional validation)
137-
- `throw Error()`: Immediate failure with custom error (no retries)
149+
- `throw new AxAssertionError(...)`: Assertion fails, triggers retry
150+
- `throw new Error(...)`: Immediate failure (crashes program, no retries)
138151

139152
**Streaming Assertions:**
140153

0 commit comments

Comments
 (0)