Map provider errors from `GoogleModel.count_tokens` and `XaiModel` file uploads

Merged
DO
2mo ago
+56-7

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 by UsageLimits(count_tokens_before_request=True) had no mapping at all. A 429 raised google.genai.errors.ClientError. It now goes through _map_api_error, the same mapping as the request paths.
  • XaiModel: the document upload for a BinaryContent / DocumentUrl prompt ran outside _map_api_errors. A gRPC error raised grpc.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 from countTokens surfaces as ModelHTTPError(429) with the body, retry_after and the ClientError cause.
  • tests/models/test_xai.py::test_xai_file_upload_error_is_mapped: a real AioRpcError(RESOURCE_EXHAUSTED) from the upload surfaces as ModelHTTPError(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