OpenAlex retriever crashes on malformed JSON responses
Summary
OpenAlexSearch.search() catches request/HTTP failures, but parses the response body only after leaving that try block:
try:
response = requests.get(self.BASE_URL, params=params, timeout=10)
response.raise_for_status()
except requests.RequestException as e:
print(f"An error occurred while accessing OpenAlex API: {e}")
return []
payload = response.json()If OpenAlex (or an intermediary) returns HTTP 200 with a malformed/non-JSON body, response.json() raises ValueError/JSONDecodeError and the retriever propagates the exception instead of degrading to an empty result set like the request-error path.
Why this matters
Retriever responses are external input. A transient proxy/CDN response, HTML error page returned with 200, or otherwise invalid body should not abort the whole research workflow when the retriever already has a graceful-error convention.
Other retrievers in this repository already guard JSON parsing together with transport errors (for example by catching ValueError alongside requests.RequestException).
Expected behavior
Malformed response JSON should be treated as a retriever failure and return [] rather than raising into the research pipeline.
A focused fix can keep the current behavior and message style while moving response.json() into the guarded block and catching ValueError:
try:
response = requests.get(...)
response.raise_for_status()
payload = response.json()
except (requests.RequestException, ValueError) as e:
print(f"An error occurred while accessing OpenAlex API: {e}")
return []Regression coverage
Stub a successful HTTP response whose .json() raises ValueError("invalid json") and assert that OpenAlexSearch("q").search() returns [] without propagating the parser exception.
I searched the issue and pull-request tracker for an existing OpenAlex malformed-JSON fix and did not find one.
AI-assisted review disclosure: an AI coding assistant was used to inspect retriever error handling and help draft this report.
Source: assafelovic/gpt-researcher