-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(provider): make outer OpenAI retry count configurable #9099
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
x1051445024
wants to merge
2
commits into
AstrBotDevs:master
Choose a base branch
from
x1051445024:configurable-provider-retries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+47
−4
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -398,6 +398,24 @@ def __init__(self, provider_config, provider_settings) -> None: | |
|
|
||
| self.reasoning_key = "reasoning_content" | ||
|
|
||
| def _provider_error_retries(self) -> int: | ||
| """Return retry attempts for provider-level recovery paths. | ||
|
|
||
| This outer retry loop handles payload mutation and key rotation (for | ||
| example context trimming, tool removal, image fallback, or switching to | ||
| another configured API key). Transport/status-code retries are handled | ||
| separately by ``request_max_retries`` in ``retry_provider_request``. | ||
| Keeping this value configurable prevents nested retry loops from | ||
| multiplying latency for proxy/aggregator providers that already perform | ||
| their own upstream retry and fallback. | ||
| """ | ||
| raw = self.provider_settings.get("provider_error_retries", 1) | ||
|
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. |
||
| try: | ||
| retries = int(raw) | ||
| except (TypeError, ValueError): | ||
| retries = 1 | ||
| return max(1, retries) | ||
|
|
||
| def _ollama_disable_thinking_enabled(self) -> bool: | ||
| value = self.provider_config.get("ollama_disable_thinking", False) | ||
| if isinstance(value, str): | ||
|
|
@@ -1188,7 +1206,7 @@ async def text_chat( | |
| payloads["tool_choice"] = tool_choice | ||
|
|
||
| llm_response = None | ||
| max_retries = 10 | ||
| max_retries = self._provider_error_retries() | ||
| available_api_keys = self.api_keys.copy() | ||
| chosen_key = random.choice(available_api_keys) | ||
| image_fallback_used = False | ||
|
|
@@ -1264,7 +1282,7 @@ async def text_chat_stream( | |
| if func_tool and not func_tool.empty(): | ||
| payloads["tool_choice"] = tool_choice | ||
|
|
||
| max_retries = 10 | ||
| max_retries = self._provider_error_retries() | ||
| available_api_keys = self.api_keys.copy() | ||
| chosen_key = random.choice(available_api_keys) | ||
| image_fallback_used = False | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -120,6 +120,22 @@ def fake_import(name, globals=None, locals=None, fromlist=(), level=0): | |||||||||||||||
| assert captured["httpx_module"] is openai_source_module.httpx | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def test_provider_error_retries_defaults_and_coerces_values(): | ||||||||||||||||
| provider = ProviderOpenAIOfficial.__new__(ProviderOpenAIOfficial) | ||||||||||||||||
|
|
||||||||||||||||
| provider.provider_settings = {} | ||||||||||||||||
| assert provider._provider_error_retries() == 1 | ||||||||||||||||
|
Comment on lines
+128
to
+129
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. Add a test case to verify that
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| provider.provider_settings = {"provider_error_retries": "3"} | ||||||||||||||||
| assert provider._provider_error_retries() == 3 | ||||||||||||||||
|
|
||||||||||||||||
| provider.provider_settings = {"provider_error_retries": 0} | ||||||||||||||||
| assert provider._provider_error_retries() == 1 | ||||||||||||||||
|
|
||||||||||||||||
| provider.provider_settings = {"provider_error_retries": "invalid"} | ||||||||||||||||
| assert provider._provider_error_retries() == 1 | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||
| async def test_get_models_retries_transient_request_error(monkeypatch): | ||||||||||||||||
| monkeypatch.setattr(request_retry, "REQUEST_RETRY_WAIT_MIN_S", 0) | ||||||||||||||||
|
|
||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Default Value of
1Disables All Recovery Paths:Setting the default value of
provider_error_retriesto1means the outer retry loop runs exactly once (i.e., 0 retries). As a result, all provider-level recovery paths (such as context length trimming, image fallback, key rotation, and tool removal) are completely disabled by default. If any of these errors occur on the first attempt, the request will immediately fail without attempting recovery.To preserve these robust recovery features out-of-the-box, consider keeping the default value higher (e.g.,
3or5), while still allowing proxy/aggregator users to configure it down to1if they want to avoid nested retries.Missing from
CONFIG_METADATA_3:This setting is not added to
CONFIG_METADATA_3indefault.py. SinceCONFIG_METADATA_3is used to render the settings in the WebUI, users will not be able to see or configure this setting from the admin panel. Please add it toCONFIG_METADATA_3underai_group -> metadata -> ai -> itemsand update the corresponding i18n locale files (config-metadata.json).