Map in-stream error objects to `ModelAPIError` in `OpenAIChatModel`, `GroqModel` and `HuggingFaceModel`
- Closes #8722
An error object sent inside a 200 SSE stream (data: {"error": {...}}) escaped Agent.run_stream() as the raw SDK exception, so except ModelAPIError handlers and FallbackModel's default fallback_on=(ModelAPIError,) never saw it. The Stainless SDKs raise the base APIError for these, and our _map_api_errors only caught its APIStatusError and APIConnectionError subclasses. The same defect reproduced on GroqModel (raw groq.APIError) and HuggingFaceModel (raw huggingface_hub.errors.OverloadedError and the other TextGenerationErrors TGI raises from an in-stream error).
Each _map_api_errors now maps these to ModelAPIError, chained from the SDK error. The HTTP status of these responses is 200, so no status code is invented: the provider's error body stays on __cause__ (e.__cause__.body for OpenAI and Groq). Giving ModelAPIError a provider error code/type of its own is left to a separate design discussion.
This also covers OpenRouterModel when the error is the first chunk, since the first-chunk peek goes through the same OpenAI mapper; a mid-stream OpenRouter error keeps its current handling.
New public surface: none.
What changes for existing users: an in-stream provider error raises ModelAPIError instead of the raw SDK exception, and FallbackModel now falls back when it is the stream's first chunk. Code catching the SDK exception directly should catch ModelAPIError and read __cause__.
Verification
tests/models/test_openai.py::test_stream_error_object_raises_model_api_error[first-chunk]/[mid-stream]: real SSE bytes throughhttpx2.MockTransport, so the SDK's own decoder raises.tests/models/test_openai.py::test_stream_error_object_falls_back: the defaultFallbackModelanswers from the second model.tests/models/test_groq.py::test_stream_error_object_raises_model_api_error[first-chunk]/[mid-stream].tests/models/test_huggingface.py::test_stream_error_object_raises_model_api_error[first-chunk]/[mid-stream].- Each fails with its production change reverted.
Not in this PR (tracked separately): non-JSON 200 bodies (#8843, #8846), Responses API error/response.failed events, and transport errors that still leak from some SDKs.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.