-
Notifications
You must be signed in to change notification settings - Fork 623
UN-3344 [FIX] Activate litellm retry for all LLM providers #1867
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?
Changes from all commits
2ae9c3f
9467015
a02f0d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,6 +207,7 @@ | |
|
|
||
| completion_kwargs = self.adapter.validate({**self.kwargs, **kwargs}) | ||
| completion_kwargs.pop("cost_model", None) | ||
| self._set_litellm_retry_params(completion_kwargs) | ||
|
|
||
| # if hasattr(self, "model") and self.model not in O1_MODELS: | ||
| # completion_kwargs["temperature"] = 0.003 | ||
|
|
@@ -274,7 +275,7 @@ | |
| message=error_msg, status_code=status_code, actual_err=e | ||
| ) from e | ||
|
|
||
| def stream_complete( | ||
|
Check failure on line 278 in unstract/sdk1/src/unstract/sdk1/llm.py
|
||
| self, | ||
| prompt: str, | ||
| callback_manager: object | None = None, | ||
|
|
@@ -295,6 +296,7 @@ | |
|
|
||
| completion_kwargs = self.adapter.validate({**self.kwargs, **kwargs}) | ||
| completion_kwargs.pop("cost_model", None) | ||
| self._set_litellm_retry_params(completion_kwargs) | ||
|
|
||
| for chunk in litellm.completion( | ||
| messages=messages, | ||
|
|
@@ -363,6 +365,7 @@ | |
|
|
||
| completion_kwargs = self.adapter.validate({**self.kwargs, **kwargs}) | ||
| completion_kwargs.pop("cost_model", None) | ||
| self._set_litellm_retry_params(completion_kwargs) | ||
|
|
||
| response = await litellm.acompletion( | ||
| messages=messages, | ||
|
|
@@ -454,6 +457,32 @@ | |
| def get_usage_reason(self) -> object: | ||
| return self.platform_kwargs.get("llm_usage_reason") | ||
|
|
||
| @staticmethod | ||
| def _set_litellm_retry_params(completion_kwargs: dict[str, object]) -> None: | ||
| """Activate litellm's wrapper-level retry for all providers. | ||
|
|
||
| litellm's retry mechanism (completion_with_retries) only activates when | ||
| num_retries is set. Our adapters pass max_retries (from user UI config) | ||
| which only works for SDK-based providers (OpenAI, Azure). This bridges | ||
| the gap by copying max_retries into num_retries so httpx-based providers | ||
| (Anthropic, Vertex, Bedrock, Mistral, etc.) also get retries. | ||
|
|
||
| SDK-based providers (OpenAI, Azure) default to max_retries=2 internally, | ||
| which would multiply with wrapper retries. Setting max_retries=0 ensures | ||
| all retries go through the wrapper uniformly. | ||
| """ | ||
| max_retries = completion_kwargs.get("max_retries") | ||
| # Use explicit `is not None and > 0` so an explicit max_retries=0 | ||
| # (opt-out) is honored and non-int / negative values don't slip through. | ||
| if isinstance(max_retries, int) and max_retries > 0: | ||
| completion_kwargs["num_retries"] = max_retries | ||
| completion_kwargs["max_retries"] = 0 | ||
| completion_kwargs["retry_strategy"] = "exponential_backoff_retry" | ||
|
Comment on lines
+474
to
+480
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle zero and invalid retry counts explicitly. At Line 474, using a truthy check skips an explicit Suggested patch max_retries = completion_kwargs.get("max_retries")
- if max_retries:
- completion_kwargs["num_retries"] = max_retries
- completion_kwargs["retry_strategy"] = "exponential_backoff_retry"
+ if max_retries is None:
+ return
+ if not isinstance(max_retries, int) or max_retries < 0:
+ raise SdkError("Invalid max_retries: expected a non-negative integer")
+ completion_kwargs["num_retries"] = max_retries
+ if max_retries > 0:
+ completion_kwargs["retry_strategy"] = "exponential_backoff_retry"🤖 Prompt for AI Agents |
||
| logger.debug( | ||
| "[sdk1][LLM] Activated litellm wrapper retry: " | ||
| f"num_retries={max_retries}, retry_strategy=exponential_backoff_retry" | ||
| ) | ||
|
|
||
| def _record_usage( | ||
| self, | ||
| model: str, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.