Commit 9584ce7
authored
feat(retry): add comprehensive tracking of all failed attempts and exceptions (#1802)
## Description
This PR enhances the retry mechanism in Instructor to track all failed
completions and exceptions across the entire range of retries, not just
the final failure. This provides much better debugging capabilities and
insight into retry patterns.
## Changes
### Core Changes
- **New data structure**: Tracks individual retry failures with attempt
number, exception, and completion response
- **Enhanced **: Now includes a list containing all retry failures
- **Updated retry functions**: Both and now collect comprehensive
failure information
### Key Features
- Track attempt number for each failure
- Store the actual exception that occurred
- Preserve completion responses (when available) for analysis
- Maintain backward compatibility with existing exception handling
### Benefits
- **Better debugging**: See exactly what failed at each retry attempt
- **Pattern analysis**: Identify if failures are consistent or changing
across retries
- **Completion inspection**: Access raw LLM responses that failed
validation
- **Comprehensive error reporting**: Full visibility into the retry
process
## Usage Example
```python
try:
response = client.chat.completions.create(
response_model=MyModel,
messages=messages,
max_retries=3
)
except InstructorRetryException as e:
print(f"Failed after {e.n_attempts} attempts")
# New: Access all failed attempts
for attempt in e.failed_attempts:
print(f"Attempt {attempt.attempt_number}: {attempt.exception}")
if attempt.completion:
# Analyze the raw completion that failed
analyze_completion(attempt.completion)
```
## Testing
- All existing tests pass (backward compatibility maintained)
- Linting and formatting checks pass
- Example demonstrates the new functionality
## Backward Compatibility
This change is fully backward compatible. The new field is optional and
defaults to an empty list if not provided.
This PR was written by [Cursor](https://cursor.com)
<!-- ELLIPSIS_HIDDEN -->
----
> [!IMPORTANT]
> Enhances retry mechanism to track all failed attempts and exceptions
for improved debugging and analysis.
>
> - **Behavior**:
> - Introduces `FailedAttempt` in `exceptions.py` to track retry
attempts with attempt number, exception, and completion.
> - Updates `InstructorRetryException` to include `failed_attempts`
list.
> - Modifies `retry_sync` and `retry_async` in `retry.py` to populate
`failed_attempts` with each failed attempt.
> - **Benefits**:
> - Provides detailed tracking of all retry attempts for better
debugging and analysis.
> - Maintains backward compatibility by defaulting `failed_attempts` to
an empty list if not provided.
>
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=567-labs%2Finstructor&utm_source=github&utm_medium=referral)<sup>
for 69f2017. You can
[customize](https://app.ellipsis.dev/567-labs/settings/summaries) this
summary. It will automatically update as commits are pushed.</sup>
<!-- ELLIPSIS_HIDDEN -->2 files changed
Lines changed: 56 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
12 | 20 | | |
13 | 21 | | |
14 | 22 | | |
| |||
34 | 42 | | |
35 | 43 | | |
36 | 44 | | |
| 45 | + | |
37 | 46 | | |
38 | 47 | | |
39 | 48 | | |
40 | 49 | | |
41 | 50 | | |
42 | 51 | | |
43 | 52 | | |
| 53 | + | |
44 | 54 | | |
45 | 55 | | |
46 | 56 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
| |||
200 | 203 | | |
201 | 204 | | |
202 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
203 | 215 | | |
204 | 216 | | |
205 | 217 | | |
| |||
231 | 243 | | |
232 | 244 | | |
233 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
234 | 255 | | |
235 | 256 | | |
236 | 257 | | |
| |||
261 | 282 | | |
262 | 283 | | |
263 | 284 | | |
| 285 | + | |
264 | 286 | | |
265 | 287 | | |
266 | 288 | | |
| |||
304 | 326 | | |
305 | 327 | | |
306 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
307 | 332 | | |
308 | 333 | | |
309 | 334 | | |
| |||
333 | 358 | | |
334 | 359 | | |
335 | 360 | | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
336 | 370 | | |
337 | 371 | | |
338 | 372 | | |
| |||
364 | 398 | | |
365 | 399 | | |
366 | 400 | | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
367 | 410 | | |
368 | 411 | | |
369 | 412 | | |
| |||
394 | 437 | | |
395 | 438 | | |
396 | 439 | | |
| 440 | + | |
397 | 441 | | |
0 commit comments