genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[plugins] Anthropic Python: map APIError to Genkit status + Retry-After

Open hilariie opened this issue 5 months ago • 1 comments

Parent: #5617 (Phase B, P1: item 21)

Why

Anthropic API errors surface raw: there is no translation into a Genkit status and no Retry-After handling. As a result, callers cannot distinguish rate limiting from authentication failures from bad requests, and clients cannot back off correctly on 429/503/529. JS already maps these.

This is a small, self-contained reliability fix with no ordering constraints relative to the tool-schema default (item 28, #5631 ).

What needs to be done

  • Translate Anthropic API errors into the appropriate Genkit status, matching the JS mapping:
    • rate limited → resource-exhausted
    • unauthorized → unauthenticated
    • forbidden → permission-denied
    • bad request → invalid-argument
    • server error → internal
    • service unavailable / overloaded → unavailable
    • anything else → unknown
  • Parse the Retry-After header (both delay-seconds and HTTP-date forms) and surface it on the error so clients can back off appropriately.

Acceptance

  • Anthropic failures raise Genkit errors with the correct mapped status.
  • Retry-After is parsed and surfaced on rate-limit / unavailable responses.
  • Tests cover the status mapping and Retry-After parsing.

Reference

  • Current calls have no error mapping: py/plugins/anthropic/src/genkit/plugins/anthropic/models.py:111-163 and models.py:278-351.
  • JS target: js/plugins/anthropic/src/models.ts:50-216 (status switch + parseRetryAfterMs).

hilariie avatar Jun 26 '26 11:06 hilariie

This is a good parity fix with the JS plugin. The test cases should probably cover at least 429, 503, and 529 with and without Retry-After, plus auth failures that must not be retried. It would also help to assert the mapped Genkit status and retry metadata separately, so callers can both display the right error class and implement backoff consistently.

hiSandog avatar Jun 27 '26 01:06 hiSandog