-
Notifications
You must be signed in to change notification settings - Fork 13
Add retry decorator with exponential backoff #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add retry decorator with exponential backoff #53
Conversation
Implements a configurable retry decorator for handling flaky API calls and network operations. Features: - Exponential backoff with configurable base and max delay - Customizable exception types to catch - Comprehensive logging for debugging - Type hints for better IDE support Addresses issue aviralgarg05#27
Add retry decorator to core module exports for easy access
Implements thorough test coverage for the retry decorator including: - Successful calls without retries - Retry behavior on exceptions - Exponential backoff verification - Max delay capping - Exception type filtering - Argument preservation - Metadata preservation - Logging verification - Real-world API scenario Addresses issue aviralgarg05#27
Provides comprehensive examples demonstrating: - Basic retry usage with defaults - Custom retry configurations - Quick retries for fast operations - Specific exception handling - Integration with adapter classes Addresses issue aviralgarg05#27
Learn moreAll Green is an AI agent that automatically: ✅ Addresses code review comments ✅ Fixes failing CI checks ✅ Resolves merge conflicts |
WalkthroughThis pull request introduces a retry decorator with exponential backoff to handle transient failures in flaky operations. The decorator is implemented in the core utilities, exposed as a public API, accompanied by comprehensive tests, and demonstrated through practical examples across various scenarios. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/agentunit/core/utils.py (1)
15-21: Consider narrower default exception catching.The default
exceptions=(Exception,)will catch all exceptions including programming errors (AttributeError, TypeError, etc.) which typically shouldn't be retried. Consider defaulting to network-related exceptions like(ConnectionError, TimeoutError, OSError)or documenting that users should specify appropriate exceptions for their use case.Suggested improvement
def retry( max_retries: int = 3, base_delay: float = 1.0, max_delay: float = 60.0, exponential_base: float = 2.0, - exceptions: tuple[type[Exception], ...] = (Exception,), + exceptions: tuple[type[Exception], ...] = (ConnectionError, TimeoutError, OSError), ) -> Callable[[F], F]: """ Decorator that retries a function with exponential backoff on failure. This decorator is useful for handling flaky API calls or network operations that may fail temporarily. It implements exponential backoff to avoid overwhelming the service with rapid retry attempts. Args: max_retries: Maximum number of retry attempts (default: 3). base_delay: Initial delay between retries in seconds (default: 1.0). max_delay: Maximum delay between retries in seconds (default: 60.0). exponential_base: Base for exponential backoff calculation (default: 2.0). - exceptions: Tuple of exception types to catch and retry (default: (Exception,)). + exceptions: Tuple of exception types to catch and retry (default: network-related exceptions).
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
examples/retry_decorator_usage.pysrc/agentunit/core/__init__.pysrc/agentunit/core/utils.pytests/test_retry_decorator.py
🧰 Additional context used
🧬 Code graph analysis (4)
src/agentunit/core/utils.py (1)
src/agentunit/comparison/statistics.py (1)
min(50-51)
tests/test_retry_decorator.py (1)
src/agentunit/core/utils.py (1)
retry(15-85)
src/agentunit/core/__init__.py (1)
src/agentunit/core/utils.py (1)
retry(15-85)
examples/retry_decorator_usage.py (1)
src/agentunit/core/utils.py (1)
retry(15-85)
🔇 Additional comments (3)
src/agentunit/core/__init__.py (1)
10-10: LGTM! Clean public API exposure.The retry decorator is correctly imported and exposed in
__all__, following the existing module patterns.Also applies to: 13-21
tests/test_retry_decorator.py (1)
1-194: LGTM! Comprehensive test coverage.The test suite thoroughly covers all key scenarios including success without retry, retry behavior, exponential backoff validation, max delay capping, exception filtering, argument/metadata preservation, logging verification, and realistic use cases. The tests are well-structured with appropriate mocking and fast execution times.
examples/retry_decorator_usage.py (1)
1-172: LGTM! Excellent examples demonstrating diverse use cases.The example file provides clear, practical demonstrations of the retry decorator across multiple scenarios: basic usage, custom configurations for critical operations, quick retries, specific exception handling, and integration with adapter classes. The code is well-documented and runnable, serving as effective documentation for users.

Summary
This PR implements a retry decorator with exponential backoff to handle flaky API calls and network operations, addressing issue #27.
Changes
Core Implementation
src/agentunit/core/utils.py- Implements the@retrydecorator with:Module Updates
src/agentunit/core/__init__.py- Exports theretrydecorator for easy accessTesting
tests/test_retry_decorator.py- Comprehensive test suite covering:Documentation
examples/retry_decorator_usage.py- Practical examples demonstrating:Features
✅ Exponential backoff to avoid overwhelming services
✅ Configurable retry parameters for different use cases
✅ Type hints for better IDE support
✅ Comprehensive logging for debugging
✅ Preserves function metadata and arguments
✅ Selective exception catching
✅ Well-tested with 15+ test cases
✅ Practical examples for common scenarios
Usage Example
Testing
All tests pass locally. The implementation includes:
Checklist
Closes #27
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.