fix(agents): catch Anthropic RateLimitError in ChatAgent retry loop - #4140
fix(agents): catch Anthropic RateLimitError in ChatAgent retry loop#4140RajanChavada wants to merge 4 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Fixes camel-ai#3882 The retry logic in `_get_model_response` and `_aget_model_response` imported `RateLimitError` exclusively from the `openai` package. When using `AnthropicModel`, a rate-limit response raises `anthropic.RateLimitError` instead, which bypassed the retry loop entirely and surfaced as an unhandled exception to the caller. Introduce `_RATE_LIMIT_ERRORS`, a tuple assembled at import time from whichever provider SDKs are installed (openai always present, anthropic optional). Both `except` clauses now catch the full tuple, so retry-with-backoff works for Anthropic models without adding a hard dependency on the anthropic package.
1b30182 to
2a3c98d
Compare
|
Tagging @fengju0213 @maoxin1234: would appreciate a review when you get a chance! This fixes #3882 (Anthropic |
Use specific ModelProcessingError instead of bare Exception in pytest.raises, satisfying ruff B017. Apply ruff-format style changes to existing test assertions (line-wrap style only, no logic changes).
|
Nice, targeted fix and the root cause is right: the retry loop only caught The important part is handled well. The Non-blocking suggestions:
CI is red only from the fork-secrets collection errors (missing Coordination note: this overlaps with #3949, #3958, and #3974, which all target the same issue. Of that set this one is the most mergeable for the specific reported bug (guarded import plus real tests). Might be worth flagging to maintainers to avoid splitting the effort. |
|
Flagging @lightaime @fengju0213 to take a look to avoid unnecessary developer overlap |
Related Issue
Closes #3882
Description
Problem
ChatAgent._get_model_responseand_aget_model_responseretry on rate-limit errors using exponential backoff. Theexceptclause importedRateLimitErrorexclusively from theopenaipackage:When a user runs
ChatAgentwithAnthropicModel, a 429 response from the Anthropic API raisesanthropic.RateLimitError— a completely separate class with no shared base. The retry loop never fires, and the error propagates uncaught to the caller as an unhandled exception, crashing the agent instead of retrying.Fix
Introduce
_RATE_LIMIT_ERRORS, a tuple built at import time from every provider rate-limit exception that is installed in the current environment:Both
exceptclauses now catch_RATE_LIMIT_ERRORSinstead of the bareRateLimitError. Theanthropicpackage is already an optional dependency inpyproject.toml— no new dependencies added.What is the purpose of this pull request?
Changes Made
camel/agents/chat_agent.pyanthropic.RateLimitError; build_RATE_LIMIT_ERRORStuple; swap bothexcept RateLimitErrorclausestest/agents/test_chat_agent.pyopenai.RateLimitErrortriggers the retry loop; one verifiesanthropic.RateLimitErroris included in the tuple when the package is installedNet diff: +14 lines production code, +41 test lines.
Testing Done
test_rate_limit_retry_on_openai_rate_limit_error: mocksmodel_backend.runto always raiseopenai.RateLimitError, assertsrunis called exactlyretry_attempts(3) times before the final exception is raised.test_rate_limit_retry_respects_anthropic_error_when_installed: assertsanthropic.RateLimitErroris present in_RATE_LIMIT_ERRORSwhen theanthropicpackage is installed, and gracefully skips otherwise.Checklist
pyproject.tomland runuv lock— no new deps;anthropicis already optional