Map provider errors from `GoogleModel.count_tokens` and `XaiModel` file uploads
Merged
DO
2mo ago+56+56+01234567890123456789-7-7-0123456789
Found while auditing how each provider adapter maps SDK errors (related: #8722). No issue was filed for it.
Two provider calls ran outside their adapter's error mapping, so a provider error escaped as the raw SDK exception, not as ModelHTTPError / ModelAPIError. That meant except ModelHTTPError handlers and FallbackModel's default fallback_on=(ModelAPIError,) both missed it.
GoogleModel.count_tokens: the call used byUsageLimits(count_tokens_before_request=True)had no mapping at all. A 429 raisedgoogle.genai.errors.ClientError. It now goes through_map_api_error, the same mapping as the request paths.XaiModel: the document upload for aBinaryContent/DocumentUrlprompt ran outside_map_api_errors. A gRPC error raisedgrpc.aio.AioRpcError. It is now mapped with the chat call's gRPC table, on both the streaming and non-streaming paths, since both build the prompt through the same helper.
New public surface: none.
What changes for existing users: these two calls now raise ModelHTTPError / ModelAPIError, chained from the SDK error, the same way the request itself already does. Nothing else changes.
Verification
tests/models/test_google.py::test_google_count_tokens_api_error_is_wrapped: a 429 fromcountTokenssurfaces asModelHTTPError(429)with the body,retry_afterand theClientErrorcause.tests/models/test_xai.py::test_xai_file_upload_error_is_mapped: a realAioRpcError(RESOURCE_EXHAUSTED)from the upload surfaces asModelHTTPError(429), chained from it.- Both tests fail when their production change is reverted.
Not changed here
- Transport errors (connect, timeout, mid-stream reset) and undecodable 200 bodies still leak raw from
GoogleModel. That is separate work. - The status numbers xAI's gRPC→HTTP table assigns are unchanged.
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.
🤖 Generated with Claude Code