Skip to content

fix(scholar): report malformed Serper responses instead of crashing the batched scholar call - #278

Open
DZH166 wants to merge 1 commit into
Alibaba-NLP:mainfrom
DZH166:codex/scholar-malformed-response-20261108
Open

DZH166 wants to merge 1 commit into
Alibaba-NLP:mainfrom
DZH166:codex/scholar-malformed-response-20261108

Conversation

@DZH166

@DZH166 DZH166 commented Nov 8, 2026

Copy link
Copy Markdown

Summary

Scholar.google_scholar_with_serp is designed to never raise: its retry loop returns a timeout string, and the parsing/rendering block is wrapped in a bare except that returns a "No results found" string. Two lines break that contract: res.read() and json.loads(data.decode("utf-8")) run outside every try block. When the Serper endpoint answers with anything that is not JSON — an HTML 403/502 error page from a proxy or gateway, an empty body, a truncated response — json.loads raises json.JSONDecodeError straight out of the tool.

That matters most for the batched path. Scholar.call runs multiple queries via list(executor.map(self.google_scholar_with_serp, query)), and executor.map propagates the first exception, so a single malformed response for one query discards the successful results of every other query in the batch. The escaping exception then hits the bare except around custom_call_tool in MultiTurnReactAgent._run and is reported to the model as the misleading message Error: Tool call is not a valid JSON....

The fix wraps res.read() + json.loads in a try/except that returns a per-query error string consistent with the existing timeout message, so a malformed response degrades to one failed query instead of killing the whole batch:

try:
    data = res.read()
    results = json.loads(data.decode("utf-8"))
except Exception as e:
    print(e)
    return f"Google Scholar returned a malformed response for '{query}', please try again later."

Validation

Offline regression tests (no network; http.client.HTTPSConnection is stubbed): inference/tests/test_tool_scholar_response_parsing.py

  • python -m pytest inference/tests/test_tool_scholar_response_parsing.py -v
  • Before the fix (red): 3 failed — test_non_json_body_returns_error_string_instead_of_raising, test_empty_body_returns_error_string_instead_of_raising, test_batch_survives_one_malformed_response all crash with json.JSONDecodeError: Expecting value: line 1 column 1 (char 0); test_valid_json_still_renders_results passed.
  • After the fix (green): 4 passed.
  • Environment: Python 3.13.9, qwen-agent==0.0.26 (the version pinned in requirements.txt).
  • git diff --check clean.

Note: google_search_with_serp in inference/tool_search.py has the same unguarded res.read()/json.loads pattern. It is intentionally left out of this PR to keep the change minimal — that file is currently being modified by several open provider-integration PRs.

AI assistance

This change was developed with the help of an AI coding agent (code analysis, patch drafting, and test writing); the analysis and patch were human-reviewed before submission.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant