fix: prevent needless retry of TokenLimitExceeded and fix search engine fallback#1348
Open
Ricardo-M-L wants to merge 1 commit intoFoundationAgents:mainfrom
Open
Conversation
…ne fallback Bug 1: The retry decorator on ask(), ask_with_images(), and ask_tool() included `Exception` in retry_if_exception_type alongside `OpenAIError` and `ValueError`. Since `TokenLimitExceeded` inherits from `Exception`, it was retried 6 times despite the comment saying otherwise. Removed `Exception` from the filter. Bug 2: In _try_all_engines(), the call to _perform_search_with_engine() was not wrapped in try/except. If an engine raised an exception, remaining fallback engines were never tried. Added try/except to log the warning and continue to the next engine. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
Friendly bump — this fork PR's workflow run is stuck on |
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.
Summary
This PR fixes two bugs:
Bug 1:
TokenLimitExceededretried 6 times despite intent not to retry itFile:
app/llm.py(3 locations:ask,ask_with_images,ask_tool)The
@retrydecorator usesretry_if_exception_type((OpenAIError, Exception, ValueError)). The comment says "Don't retry TokenLimitExceeded", but sinceTokenLimitExceededinherits fromException, tenacity matches it and retries 6 times — wasting tokens and time on a condition that cannot succeed on retry.Fix: Remove
Exceptionfrom the tuple.OpenAIErrorandValueErroralready cover all intended retry cases.TokenLimitExceeded(which does not inherit from either) will now propagate immediately as intended.Bug 2: Search engine fallback broken when an engine raises an exception
File:
app/tool/web_search.py, method_try_all_enginesThe call to
_perform_search_with_engine()is not wrapped intry/except. If an engine raises an exception (e.g., network error, API key issue), the exception propagates up and the remaining fallback engines are never tried — defeating the purpose of the fallback mechanism.Fix: Wrap the call in
try/except Exception, log a warning with the engine name and error, append tofailed_engines, andcontinueto the next engine. Also addedfailed_engines.append(engine_name)whensearch_itemsis empty so the final log message accurately reports all engines that were attempted.Test plan
TokenLimitExceededis raised immediately without retries when the token limit is exceededOpenAIErrorandValueErrorare still retried up to 6 times🤖 Generated with Claude Code