Repository navigation
fix(agent/tools): surface serpapi error responses in google search - #17062
Conversation
serpapi reports an invalid api key, exhausted quota or an empty result
set through an "error" field and omits "organic_results". the google
search tool indexed search["organic_results"] directly, so those cases
raised KeyError('organic_results'), which the retry loop reported to the
model as the opaque "Google error: 'organic_results'".
guard the missing key and surface serpapi's actual message (with a clear
fallback) through the existing retry/_ERROR path instead. adds a
regression test covering the error response and the normal result path.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughChangesSerpApi response handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
agent/tools/google.py (1)
504-505: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valuePrefer
RuntimeErrorover baseException.Raising the base
Exceptionclass is generally discouraged in Python as it can be too broad to catch safely in other contexts. Consider raising a more specific exception likeRuntimeError.♻️ Proposed refactor
if "organic_results" not in search: - raise Exception(search.get("error", "SerpApi returned no organic_results.")) + raise RuntimeError(search.get("error", "SerpApi returned no organic_results."))🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@agent/tools/google.py` around lines 504 - 505, In the organic-results validation within the Google search flow, replace the base Exception raised when "organic_results" is missing with RuntimeError, preserving the existing error message and condition.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@agent/tools/google.py`:
- Around line 508-513: Update the get_content lambda passed to _retrieve_chunks
to use r.get("snippet", "") as the fallback, preventing eager evaluation from
raising KeyError when description exists without snippet. Also use safe .get()
access for title and link in the adjacent get_title and get_url lambdas.
---
Nitpick comments:
In `@agent/tools/google.py`:
- Around line 504-505: In the organic-results validation within the Google
search flow, replace the base Exception raised when "organic_results" is missing
with RuntimeError, preserving the existing error message and condition.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: a25cf52f-5de8-46d8-a584-25b7f9cdc2f5
📒 Files selected for processing (2)
agent/tools/google.pytest/unit_test/agent/tools/test_google_unit.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #17062 +/- ##
==========================================
- Coverage 94.56% 90.65% -3.91%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 650 -28
- Misses 25 39 +14
- Partials 14 28 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
review follow-up: the get_content fallback `r["snippet"]` is evaluated
eagerly, so a result carrying a description but no snippet raised
KeyError('snippet') before the description fallback was ever used --
the same failure mode this pr set out to remove. read title, link and
snippet with .get() defaults.
also raise RuntimeError rather than the base Exception for the missing
organic_results case, and extend the test stub to apply the getters to
every result so the lambdas are actually exercised.
|
Thanks @Hz-186 — good catch, fixed in
Also switched the missing- |
Harry-Hz-Zhang
left a comment
There was a problem hiding this comment.
@Yurii214, LGTM ! ! !
Summary
agent/tools/google.pyindexessearch["organic_results"]directly afterGoogleSearch(params).get_dict(). serpapi returns{"error": ...}without anorganic_resultskey on realistic conditions — an invalid API key, an exhausted plan/quota, or a query that matched nothing. that raisedKeyError('organic_results'), which the tool's retry loop then surfaced to the model as the opaque"Google error: 'organic_results'"instead of the real reason.this guards the missing key and raises serpapi's actual
errormessage (with a clear fallback) into the existing retry/_ERRORpath, so the model sees e.g."Google error: Invalid API key.". valid responses are unchanged.adds
test/unit_test/agent/tools/test_google_unit.pycovering the error-response path (asserts the real message is surfaced, noKeyError) and the normal result path, mirroring the existingtest_googlescholar.pypattern (skips when theserpapiSDK is absent).