Repository navigation
Commit 8a57d42
authored
fix(ai,coding-agent): report HTTP error responses to onResponse (#12502)
Refs #12376
Problem: `onResponse` (and the `after_provider_response` extension
event) never fired for 4xx/5xx responses on SDK-based adapters. Example
on `openai-completions`: a 429 makes
`client.chat.completions.create(...).withResponse()` reject with the
SDK's `APIError`, so the `onResponse` call after it never runs, and the
catch block keeps only `errorMessage` (`"429 ..."`). The status and
`retry-after` header on the `APIError` were dropped. The fetch-based
adapters (OpenAI Codex SSE, Mistral, pi-messages) and Amazon Bedrock
already reported error responses, once per attempt.
Change:
- `retryProviderRequest` gets `onErrorResponse`, called for each failed
attempt that carries an HTTP status, before the retry decision. Not
called after an abort or for failures without a status (connection
errors). A throwing callback fails the request, like every other pi-ai
callback.
- Wired into `openai-completions`, `openai-responses`,
`azure-openai-responses`, `anthropic-messages`, OpenRouter images and
Google (`retryGoogleRequest` now takes the model). Google's `ApiError`
has no headers, so Google reports the status with `{}`; Google still
reports no successful responses (unchanged).
- Classifiers (`classifier-shared`, `llama-cpp-classify`) call
`onResponse` right after `fetch`, before reading the body, like the
fetch-based chat adapters, so an error whose body fails to arrive still
reports its status.
- Docs: `onResponse` and `AfterProviderResponseEvent` comments,
`docs/extensions.md`, changelogs.
Behavior change: one request can now fire the hook several times (each
retried attempt, Anthropic thread and strict-tools fallbacks report the
rejected 400 and then the 200). Codex already behaved this way.
Extensions are not affected by throwing handlers: the extension runner
catches them.
Other changes:
- Classifiers now call `onResponse` before reading the body instead of
after parsing it. On success it fires before JSON parsing, and the
callback runs inside the attempt's `timeoutMs` window.
- `retryGoogleRequest` (`api/google-shared.ts`, reachable through the
`./api/*` export) takes `model` as its second parameter. No caller
outside pi-ai in the repo.
Not covered: errors inside an already started stream (SSE error event
after a 200) and the Codex WebSocket transport have no HTTP status to
report; documented in `docs/extensions.md`.
Tests: `provider-error-response.test.ts` runs a local server answering
429 with `retry-after` against five adapters (fails without the fix);
unit tests in `provider-retry.test.ts`, `google-shared-retry.test.ts`,
`pi-classifier.test.ts`, `llama-cpp-classify.test.ts`.1 parent 433c1fd commit 8a57d42
22 files changed
Lines changed: 305 additions & 25 deletions
File tree
- packages
- ai
- src
- api
- utils
- test
- coding-agent
- docs
- src/core/extensions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
886 | 886 | | |
887 | 887 | | |
888 | 888 | | |
| 889 | + | |
889 | 890 | | |
890 | 891 | | |
891 | 892 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
| 91 | + | |
| 92 | + | |
91 | 93 | | |
92 | | - | |
| 94 | + | |
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
| |||
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
105 | | - | |
106 | | - | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
101 | 105 | | |
102 | 106 | | |
103 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
613 | 614 | | |
614 | 615 | | |
615 | 616 | | |
616 | | - | |
| 617 | + | |
| 618 | + | |
617 | 619 | | |
618 | 620 | | |
619 | 621 | | |
620 | | - | |
| 622 | + | |
| 623 | + | |
621 | 624 | | |
622 | 625 | | |
623 | 626 | | |
| |||
634 | 637 | | |
635 | 638 | | |
636 | 639 | | |
| 640 | + | |
637 | 641 | | |
638 | 642 | | |
639 | 643 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
117 | 121 | | |
118 | 122 | | |
119 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
| 246 | + | |
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
| |||
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
260 | 264 | | |
261 | | - | |
| 265 | + | |
262 | 266 | | |
263 | 267 | | |
264 | 268 | | |
265 | 269 | | |
266 | 270 | | |
267 | 271 | | |
268 | 272 | | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | 273 | | |
274 | 274 | | |
275 | 275 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
391 | 391 | | |
392 | 392 | | |
393 | 393 | | |
| 394 | + | |
394 | 395 | | |
395 | 396 | | |
396 | 397 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| 222 | + | |
222 | 223 | | |
223 | 224 | | |
224 | 225 | | |
| |||
0 commit comments