Raise `ModelQuotaExceededError` for the Gemini API's daily quota and disabled billing, and Moonshot's exhausted balance
Posted by Claude Code on Douwe's behalf.
Part of #12412 (part B: the Gemini and Moonshot rows).
Why
Leaving ModelQuotaExceededError out of fallback_on (or out of a retry predicate) only works if the adapter raises it. Three out-of-credit/quota signals were raised as something else:
| Provider | Signal | Before | After |
|---|---|---|---|
| Gemini API / Vertex | 429 RESOURCE_EXHAUSTED whose google.rpc.QuotaFailure lists a daily quota (quotaId containing PerDay, e.g. GenerateRequestsPerDayPerProjectPerModel-FreeTier) | ModelRateLimitError | ModelQuotaExceededError |
| Gemini API | 400 FAILED_PRECONDITION "⦠Please enable billing on your project in Google AI Studio." | ModelRequestRejectedError | ModelQuotaExceededError |
Moonshot (moonshotai provider) | 429 with type exceeded_current_quota_error | ModelRateLimitError | ModelQuotaExceededError |
A per-minute RESOURCE_EXHAUSTED (only PerMinute quota IDs, or no details) stays a ModelRateLimitError, and other FAILED_PRECONDITIONs (e.g. "User location is not supported") stay rejections. Daily quotas reset at midnight Pacific time (Google rate-limits docs), so waiting won't help. The snake_case quota_exceeded / failed_precondition codes in Google's API errors page belong to the Interactions API, which Pydantic AI doesn't use; generateContent sends gRPC statuses with details, so the mapping reads those. Like Z.ai's codes (#12042), the Moonshot type is mapped only for the Moonshot provider.
Test bodies: Gemini daily quota from gemini-cli#8437, billing from NextChat#5255, Moonshot from a staff reply on the Moonshot forum.
Not in this PR: xAI (the 403 "used all available credits" body isn't documented and needs an empty account to confirm), and Anthropic spend limits (#12061).
New public surface
None.
Verification
New cases in tests/models/test_error_categories.py: google-daily-quota, google-per-minute-quota, google-billing-not-enabled, moonshotai-exceeded-current-quota, moonshotai-rate-limit, openai-moonshot-quota-type-rate-limit. The same mapping covers Gemini batch results and GoogleEmbeddingModel.
What changes for existing users
Code that caught ModelRateLimitError for a Gemini daily-quota 429, or ModelRequestRejectedError for Gemini's billing precondition, now gets ModelQuotaExceededError. Both are still ModelHTTPErrors, and FallbackModel / ModelFallback still fall back on them by default.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code
Summary by cubic
ModelQuotaExceededError is now raised for Gemini's daily quota and billing-not-enabled errors, and for Moonshot's exhausted-balance 429, instead of ModelRateLimitError or ModelRequestRejectedError.
- Gemini
RESOURCE_EXHAUSTED429s are quota errors only when aQuotaFailurenames aPerDayquota; per-minute limits stayModelRateLimitError. - Only the Moonshot provider maps
exceeded_current_quota_error, matching how Z.ai codes are scoped. - Code catching
ModelRateLimitErrororModelRequestRejectedErrorfor these cases now getsModelQuotaExceededError; both remainModelHTTPErrorsubclasses, so fallback behavior is unchanged.