Add exponential backoff retry logic for AI service failures#125
Add exponential backoff retry logic for AI service failures#125
Conversation
Co-authored-by: RohanExploit <178623867+RohanExploit@users.noreply.github.com>
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ❌ Deployment failed View logs |
fixingbharat | bd49c76 | Jan 09 2026, 04:53 PM |
Co-authored-by: RohanExploit <178623867+RohanExploit@users.noreply.github.com>
Co-authored-by: RohanExploit <178623867+RohanExploit@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request adds exponential backoff retry logic to AI service calls to handle transient failures like rate limits and network timeouts. The implementation introduces a new retry utility module with decorators that wrap existing AI service calls from Gemini and Hugging Face APIs. Upon retry exhaustion, services return well-formed fallback responses instead of failing silently.
Changes:
- Created core retry infrastructure with exponential backoff decorator supporting configurable retry parameters
- Integrated retry logic into all Gemini AI services (action plan generation, chat assistant, MLA summary generation)
- Integrated retry logic into all Hugging Face detection endpoints (vandalism, infrastructure, flooding)
- Added comprehensive unit and integration tests covering retry timing, exhaustion, and fallback patterns
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/retry_utils.py | New module providing async and sync exponential backoff retry decorators with structured logging |
| backend/ai_service.py | Wrapped Gemini action plan and chat functions with retry decorators, added fallback helpers |
| backend/gemini_summary.py | Wrapped MLA summary generation with retry decorator, added fallback response |
| backend/hf_service.py | Wrapped Hugging Face API requests with retry decorator, improved error handling and logging |
| tests/test_retry_logic.py | Unit tests for retry decorator covering success, failure, timing, and exception handling scenarios |
| tests/manual_integration_test.py | Manual integration tests demonstrating retry behavior with simulated API failures |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import asyncio | ||
| import functools | ||
| import logging | ||
| from typing import TypeVar, Callable, Optional, Tuple, Type |
There was a problem hiding this comment.
Import of 'Optional' is not used.
| from typing import TypeVar, Callable, Optional, Tuple, Type | |
| from typing import TypeVar, Callable, Tuple, Type |
| with retry logic and exponential backoff. | ||
| """ | ||
| import pytest | ||
| import asyncio |
There was a problem hiding this comment.
Import of 'asyncio' is not used.
| import asyncio |
| import time | ||
| import sys | ||
| import os | ||
| from unittest.mock import AsyncMock, MagicMock, patch |
There was a problem hiding this comment.
Import of 'AsyncMock' is not used.
Import of 'MagicMock' is not used.
Import of 'patch' is not used.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from retry_utils import exponential_backoff_retry | ||
| import logging | ||
| import base64 |
There was a problem hiding this comment.
The base64 import at module level is unnecessary since it's only used within the _make_request_with_retry function. Moving it there (line 34) would be more appropriate, or keep it at module level and remove the duplicate import location.
| from async_lru import alru_cache | ||
| from retry_utils import exponential_backoff_retry | ||
| import logging | ||
| import json |
There was a problem hiding this comment.
The json import at module level is redundant since it was already used in the original code within the try block. Consider keeping it at module level throughout for consistency.
| import json |
| assert 0.08 < delay1 < 0.15 | ||
| assert 0.18 < delay2 < 0.25 | ||
| assert 0.38 < delay3 < 0.45 |
There was a problem hiding this comment.
The timing tolerances (±0.02-0.05s) may be too strict for CI/CD environments with variable load. Consider widening these ranges or using a more flexible approach to verify exponential growth pattern rather than absolute timing values.
AI service calls (Gemini, Hugging Face) fail silently on transient errors (rate limits, network timeouts), returning empty or fallback responses without retry attempts.
Changes
Core retry infrastructure
backend/retry_utils.py: Exponential backoff decorator with configurable retry parameters (default: 3 retries, 1s→2s→4s delays)AI service integration
ai_service.py,gemini_summary.py): Wrapped API calls with retry decoratorhf_service.py): Wrapped all detection endpoints with retry decoratorImplementation pattern
Testing
Behavior
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.