Skip to content

fix(models): standardize Anthropic API error handling in AnthropicLlm#5511

Open
sebastienc wants to merge 2 commits intogoogle:mainfrom
sebastienc:feat/anthropic-error-handling
Open

fix(models): standardize Anthropic API error handling in AnthropicLlm#5511
sebastienc wants to merge 2 commits intogoogle:mainfrom
sebastienc:feat/anthropic-error-handling

Conversation

@sebastienc
Copy link
Copy Markdown

Summary

Fixes #5396

Disclaimer: I'm using Claude Code to generate the following text. I find the summary is better than what I would've wrote.

Standardizes how AnthropicLlm handles errors from the Anthropic API, bringing it in line with the error handling pattern used elsewhere in ADK.

  • AnthropicRateLimitError — new exception class (subclasses Exception, not anthropic.RateLimitError) raised when the API returns a 429; includes a helpful mitigation message pointing users toward rate limit remediation options; original error is chained via from e so the full traceback is preserved
  • generate_content_async — wraps the non-streaming messages.create call in a try/except anthropic.RateLimitError block and re-raises as AnthropicRateLimitError
  • _generate_content_streaming — same wrapping around the streaming messages.create call; all other anthropic exceptions propagate unchanged

Testing plan

4 new tests added to tests/unittests/models/test_anthropic_llm.py. Full suite: 63 passed.

tests/unittests/models/test_anthropic_llm.py::test_non_streaming_rate_limit_raises_anthropic_rate_limit_error PASSED
tests/unittests/models/test_anthropic_llm.py::test_streaming_rate_limit_raises_anthropic_rate_limit_error PASSED
tests/unittests/models/test_anthropic_llm.py::test_non_streaming_other_errors_propagate PASSED
tests/unittests/models/test_anthropic_llm.py::test_streaming_other_errors_propagate PASSED

@adk-bot adk-bot added the models [Component] Issues related to model support label Apr 28, 2026
@rohityan rohityan self-assigned this Apr 30, 2026
@sebastienc sebastienc force-pushed the feat/anthropic-error-handling branch from 69a7eee to 2189e3e Compare May 5, 2026 13:50
@sebastienc
Copy link
Copy Markdown
Author

Rebased the branch on main

@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label May 8, 2026
@rohityan rohityan requested a review from Jacksunwei May 8, 2026 23:01
@rohityan rohityan removed the needs review [Status] The PR/issue is awaiting review from the maintainer label May 8, 2026
@rohityan rohityan removed the request for review from Jacksunwei May 8, 2026 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicLlm does not translate Anthropic API errors to ADK error codes

3 participants