Skip to content

feat: implement retry mechanism for LiteLlm #1361

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

petitoff
Copy link

In case of the issue: #1214

@pwwpche
Copy link
Contributor

pwwpche commented Jul 30, 2025

Hi @petitoff

Thanks for your contribution! Have you looked at using any libraries for implementing the retry logic?

For example, ADK already uses tenacity for retry

https://github.com/google/adk-python/blob/main/src/google/adk/sessions/vertex_ai_session_service.py#L121

@petitoff
Copy link
Author

@pwwpche hi,

I haven't tried using libraries like tenacity before, but I could explore that option. However, when creating this PR, I wanted a simple, fast, and working solution that could be implement immediately (like a hot fix)

@pwwpche
Copy link
Contributor

pwwpche commented Jul 30, 2025

@pwwpche hi,

I haven't tried using libraries like tenacity before, but I could explore that option. However, when creating this PR, I wanted a simple, fast, and working solution that could be implement immediately (like a hot fix)

I would highly recommend refactor this change using tenacity then. With ~10 lines of code, it could achieve the same outcome as the hundreds of lines of code we have in the lite_llm.py. Since this implementation is in the core of ADK, let's prioritize a bit more on longer term maintainability.

Something like

from tenacity import retry
from tenacity import retry_if_result
from tenacity import RetryError
from tenacity import stop_after_attempt
from tenacity import wait_exponential

# ...

    retry_wrapped_acompletion = retry(
          stop=stop_after_attempt(self.stop_attempts),
          wait=wait_exponential(multiplier=1, min=1, max=3),
          retry=retry_if_result(lambda response: not response),
          reraise=True,
      )(acompletion)

  return await retry_wrapped_acompletion(...)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants