fix: guard keyword_optimizer.optimize_keywords() against unexpected exceptions#658
Open
octo-patch wants to merge 1 commit into
Open
fix: guard keyword_optimizer.optimize_keywords() against unexpected exceptions#658octo-patch wants to merge 1 commit into
octo-patch wants to merge 1 commit into
Conversation
…xceptions Fixes 666ghj#657 An UnboundLocalError could occur at the optimized_keywords access site when keyword_optimizer.optimize_keywords() raised an unexpected exception, because the assignment was skipped and the variable was left unbound. The fix wraps the call in a try/except block: on any exception the code falls back to a KeywordOptimizationResponse that uses the original query as the sole keyword, so optimized_response is always bound and the search can continue with a best-effort keyword set instead of crashing. Also imports KeywordOptimizationResponse directly in agent.py so the fallback can construct the response without a relative import chain.
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 #657
Problem
execute_search_toolinInsightEngine/agent.pycalledkeyword_optimizer.optimize_keywords()without any exception guard. If the call raised an unexpected exception, theoptimized_responsevariable was left unbound, producing:Solution
Wrap the
keyword_optimizer.optimize_keywords()call in atry/except. On any exception, construct a fallbackKeywordOptimizationResponseusing the original query as the sole keyword, so the search continues with a best-effort keyword set instead of crashing.KeywordOptimizationResponseis also added to the direct imports from.tools.Testing
Added
tests/test_optimized_response_fallback.pywith 4 tests covering the normal optimisation path and exception fallback scenarios.