Repository navigation
fix: retry web searches after key-specific failures - #10877
Merged
Soulter merged 4 commits intoNov 1, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/tools/web_search_tools.py" line_range="258" />
<code_context>
reason = await response.text()
# Retryable errors are saved so the final failure is meaningful.
- if response.status in _RETRYABLE_HTTP_STATUSES:
+ if response.status in _TAVILY_RETRYABLE_HTTP_STATUSES:
last_error = Exception(
f"Tavily web search failed: {reason}, status: {response.status}",
</code_context>
<issue_to_address>
**Usable search keys go untried**
When concurrent requests advance the same provider's rotator between a failed attempt and its retry, the Tavily, Exa, and DeepSeek retry loops call the shared `_KeyRotator.get` again, so a request can reuse a key it already tried and exhaust its attempts without trying a usable configured key; the caller sees a search or fetch failure.
Track keys tried by each request and select an untried key for each retry instead of reacquiring one from the shared rotator.
Also at `astrbot/core/tools/web_search_tools.py:314`, `astrbot/core/tools/web_search_tools.py:442-453`, `astrbot/core/tools/web_search_tools.py:1069-1070`, `astrbot/core/tools/web_search_tools.py:1125`, `astrbot/core/tools/web_search_tools.py:1636-1637`, `astrbot/core/tools/web_search_tools.py:1654`, `astrbot/core/tools/web_search_tools.py:1698-1699`, `astrbot/core/tools/web_search_tools.py:1716`, `astrbot/core/tools/web_search_tools.py:2172-2173`, `astrbot/core/tools/web_search_tools.py:2190`, `astrbot/core/tools/web_search_tools.py:2278-2279`, `astrbot/core/tools/web_search_tools.py:2296`.
</issue_to_address>
### Comment 2
<location path="tests/unit/test_web_search_tools.py" line_range="1837" />
<code_context>
+ with pytest.raises(Exception, match=f"last key failed, status: {status}"):
+ await search_function(settings, payload)
+ else:
+ assert await search_function(settings, payload)
+
+ header = "x-api-key" if provider == "exa" else "Authorization"
</code_context>
<issue_to_address>
**Wrong success result passes**
When a retry returns a nonempty but incorrect value, or a string containing “Example” that is not the expected result, `test_search_key_errors_try_next_key` accepts any truthy result at line 1837, and `test_deepseek_key_errors_try_next_key` accepts any string containing “Example” at line 1950; a bogus fallback or error string therefore passes despite the second-key result being wrong.
Assert the expected result from the mocked successful response, rather than only truthiness or a substring.
Also at `tests/unit/test_web_search_tools.py:1950`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and on selected provider errors, the tool now sends the same search or fetch request using additional configured API keys; if a retry succeeds, the provider may meter that request. Reverting stops future retries but cannot undo requests already sent or any usage charges they incurred.
Blocking findings: astrbot/core/tools/web_search_tools.py:258, tests/unit/test_web_search_tools.py:1837
…keys after concurrent rotation Updated the get method to include tried_keys parameter for tracking used API keys.
Cover concurrent cursor advancement, duplicate configured keys, exhaustion errors, and exact successful provider outputs.
Soulter
approved these changes
Nov 1, 2026
ilya-bogin-keenable
added a commit
to keenableai/AstrBot
that referenced
this pull request
Nov 3, 2026
Follow the request-local tried-keys rotation from AstrBotDevs#10877, so duplicate keys and concurrent requests do not make a failover retry a key it already tried.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #10871.
A request can currently fail on an exhausted or invalid search key even when another configured key works. This adds the missing provider-specific failures to key failover and applies the existing bounded retry pattern to Exa and DeepSeek.
Modifications
Retry Firecrawl 402 and Tavily 433 responses with the next key for both search and extraction.
Retry Brave 422 only when the response identifies
SUBSCRIPTION_TOKEN_INVALID; validation errors and malformed error bodies still fail immediately.Add Exa search/contents and DeepSeek search/fetch failover for 401, 402 and 429. Preserve the last error after all configured keys fail, and keep other errors, including DeepSeek 403 refusals, non-retryable.
Add regression coverage for successful failover, exhausted keys, unchanged request payloads and non-key errors.
This is not a breaking change.
Test Results
Changes were made in the GitHub web editor. The submitted source was checked against the intended changes and the diff is limited to the search tools and their unit tests. Pytest and Ruff have not been run locally; repository CI is awaiting maintainer approval.
Verification steps:
Checklist
Summary by Sourcery
Improve web-search reliability by failing over between configured API keys only for provider-specific authentication, quota, and rate-limit failures.
Bug Fixes:
Enhancements:
Tests: