Commit 3a7306f
feat: Use Structured Errors in Responses and Conversations API (#4879)
## Summary
Replace ValueError/Exception with LlamaStackError subclasses in
Responses and Conversations APIs for predictable HTTP status codes (400,
404, 500, 503).
**Responses API**
- `InvalidParameterError` (400) for parameter validation: conversation
ID format, mutually exclusive `previous_response_id` + `conversation`,
prompt variables, temperature range, MCP Authorization in headers
- `InternalServerError` (500) for stream failures: `response.failed` in
non-streaming mode, multiple terminal events, stream never reaching
terminal state
- `ServiceNotEnabledError` (503) for guardrails without Safety API
- `ResponseNotFoundError` (404) replaces `ValueError` for missing
responses
- Remove dead `max_tool_calls < 1` guard (Pydantic enforces at API
boundary)
**Conversations API**
- `InvalidParameterError` (400) for invalid `conversation_id` format and
empty `conversation_id`/`item_id`
- `ConversationItemNotFoundError` (404) for missing items in
retrieve/delete
- Replace `InvalidConversationIdError` with `InvalidParameterError` for
consistency
**Other**
- MCP auth test catches both `llama_stack_client.BadRequestError` and
`openai.BadRequestError`
- `logger.error` with `extra=` context on failure paths for operator
debugging
- Docstring improvements for `ResourceNotFoundError`,
`InvalidParameterError`, `ServiceNotEnabledError`
## Stack
This is PR 2/4 in the error message consistency series (split from
#3913). Depends on #4878 (included in this branch).
1. Error types foundation (#4878) — merge first
2. **This PR** — Responses and Conversations API error handling
3. Record/replay error support (#record-replay-errors)
4. Integration tests (#error-integration-tests)
## Test plan
- [x] Unit tests updated for structured error types
(`InvalidParameterError`, `ServiceNotEnabledError`,
`InternalServerError`, `ResponseNotFoundError`,
`ConversationItemNotFoundError`)
- [x] `test_errors.py`: HTTP status code tests for all new error types
(400, 404, 500, 503)
- [x] `test_conversations.py`: expect `InvalidParameterError` for
invalid ID format and empty params
- [x] `test_mcp_authentication.py`: expect `BadRequestError` from both
OpenAI and llama-stack clients
- [x] Unit test for `response.failed` → `InternalServerError` in
non-streaming mode
- [x] All pre-commit hooks pass (ruff, mypy, API spec codegen, license
checks)
- [x] CI passes
---------
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent 5042310 commit 3a7306f
13 files changed
Lines changed: 336 additions & 87 deletions
File tree
- src
- llama_stack_api
- common
- llama_stack
- core/conversations
- providers
- inline/agents/meta_reference/responses
- utils/responses
- tests
- integration/responses
- unit
- conversations
- core
- distribution
- providers/agents/meta_reference
- utils/responses
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| |||
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
68 | | - | |
| 73 | + | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
| |||
176 | 181 | | |
177 | 182 | | |
178 | 183 | | |
179 | | - | |
180 | | - | |
181 | | - | |
| 184 | + | |
182 | 185 | | |
183 | 186 | | |
184 | 187 | | |
| |||
242 | 245 | | |
243 | 246 | | |
244 | 247 | | |
245 | | - | |
246 | | - | |
247 | | - | |
| 248 | + | |
248 | 249 | | |
249 | | - | |
| 250 | + | |
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
254 | 255 | | |
255 | 256 | | |
256 | 257 | | |
257 | | - | |
| 258 | + | |
258 | 259 | | |
259 | 260 | | |
260 | 261 | | |
261 | 262 | | |
262 | 263 | | |
263 | 264 | | |
264 | 265 | | |
265 | | - | |
266 | | - | |
267 | | - | |
| 266 | + | |
268 | 267 | | |
269 | 268 | | |
270 | 269 | | |
| |||
300 | 299 | | |
301 | 300 | | |
302 | 301 | | |
303 | | - | |
304 | | - | |
305 | | - | |
| 302 | + | |
306 | 303 | | |
307 | | - | |
| 304 | + | |
308 | 305 | | |
309 | 306 | | |
310 | 307 | | |
| |||
313 | 310 | | |
314 | 311 | | |
315 | 312 | | |
316 | | - | |
| 313 | + | |
317 | 314 | | |
318 | 315 | | |
319 | 316 | | |
| |||
Lines changed: 52 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | 61 | | |
| |||
304 | 306 | | |
305 | 307 | | |
306 | 308 | | |
307 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
308 | 314 | | |
309 | 315 | | |
310 | 316 | | |
| |||
581 | 587 | | |
582 | 588 | | |
583 | 589 | | |
584 | | - | |
585 | | - | |
586 | | - | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
587 | 594 | | |
588 | 595 | | |
589 | 596 | | |
590 | 597 | | |
591 | 598 | | |
592 | 599 | | |
593 | | - | |
594 | | - | |
595 | | - | |
596 | | - | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
597 | 603 | | |
598 | 604 | | |
599 | 605 | | |
600 | 606 | | |
601 | | - | |
602 | | - | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
603 | 611 | | |
604 | 612 | | |
605 | 613 | | |
606 | | - | |
| 614 | + | |
607 | 615 | | |
608 | 616 | | |
609 | 617 | | |
| |||
668 | 676 | | |
669 | 677 | | |
670 | 678 | | |
671 | | - | |
672 | 679 | | |
673 | 680 | | |
674 | 681 | | |
675 | 682 | | |
676 | | - | |
677 | | - | |
678 | | - | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
679 | 693 | | |
| 694 | + | |
680 | 695 | | |
681 | 696 | | |
682 | 697 | | |
683 | 698 | | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
684 | 715 | | |
685 | 716 | | |
686 | 717 | | |
687 | | - | |
688 | | - | |
689 | | - | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | 718 | | |
696 | | - | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
697 | 724 | | |
698 | 725 | | |
699 | 726 | | |
| |||
Lines changed: 10 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
193 | 195 | | |
194 | 196 | | |
195 | 197 | | |
196 | | - | |
| 198 | + | |
197 | 199 | | |
198 | 200 | | |
199 | 201 | | |
| |||
268 | 270 | | |
269 | 271 | | |
270 | 272 | | |
271 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
272 | 278 | | |
273 | 279 | | |
274 | 280 | | |
| |||
289 | 295 | | |
290 | 296 | | |
291 | 297 | | |
292 | | - | |
| 298 | + | |
293 | 299 | | |
294 | | - | |
| 300 | + | |
295 | 301 | | |
296 | 302 | | |
297 | 303 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | | - | |
| 104 | + | |
| 105 | + | |
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
107 | 109 | | |
| 110 | + | |
108 | 111 | | |
| 112 | + | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
| |||
653 | 657 | | |
654 | 658 | | |
655 | 659 | | |
| 660 | + | |
656 | 661 | | |
657 | 662 | | |
658 | 663 | | |
| |||
736 | 741 | | |
737 | 742 | | |
738 | 743 | | |
| 744 | + | |
739 | 745 | | |
740 | 746 | | |
741 | 747 | | |
742 | 748 | | |
743 | | - | |
| 749 | + | |
744 | 750 | | |
745 | 751 | | |
746 | 752 | | |
| |||
1017 | 1023 | | |
1018 | 1024 | | |
1019 | 1025 | | |
| 1026 | + | |
| 1027 | + | |
1020 | 1028 | | |
1021 | 1029 | | |
1022 | 1030 | | |
| |||
1059 | 1067 | | |
1060 | 1068 | | |
1061 | 1069 | | |
| 1070 | + | |
1062 | 1071 | | |
1063 | 1072 | | |
1064 | 1073 | | |
| |||
0 commit comments