Skip to content

Fixing Semantic Scholar crash on max over empty list#1297

Merged
jamesbraza merged 1 commit intomainfrom
fixing-s2-crash
Feb 21, 2026
Merged

Fixing Semantic Scholar crash on max over empty list#1297
jamesbraza merged 1 commit intomainfrom
fixing-s2-crash

Conversation

@jamesbraza
Copy link
Copy Markdown
Collaborator

There's an edge case with Semantic Scholar seen in our production logs:

request failed for query 'approximately 30% rheumatoid arthritis inadequate response to biologic DMARDs systematic review'

Traceback (most recent call last):
  ...
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/paperqa/clients/client_models.py", line 114, in query
    return await self._query(client_query)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/paperqa/clients/semantic_scholar.py", line 373, in _query
    return await get_s2_doc_details_from_title(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    )
    ^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/paperqa/clients/semantic_scholar.py", line 358, in get_s2_doc_details_from_title
    return await s2_title_search(
           ^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    )
    ^
  File "/layers/google.python.pip/pip/lib/python3.13/site-packages/paperqa/clients/semantic_scholar.py", line 263, in s2_title_search
    title_similarity, result = max(
                               ~~~^
        # need to check if nested under a 'data' key or not (depends on filtering)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        (strings_similarity(entry["title"], title), entry)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        for entry in data.get("data", data)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
ValueError: max() iterable argument is empty

The issue is max over an empty data will throw a ValueError

@jamesbraza jamesbraza self-assigned this Feb 20, 2026
Copilot AI review requested due to automatic review settings February 20, 2026 21:56
@jamesbraza jamesbraza added the bug Something isn't working label Feb 20, 2026
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Feb 20, 2026
@dosubot
Copy link
Copy Markdown

dosubot bot commented Feb 20, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a crash in the Semantic Scholar client when the API returns an empty result set. The issue occurs when calling max() on an empty iterable, which raises a ValueError. This edge case was discovered in production logs when certain queries returned no results.

Changes:

  • Added ValueError to the exception handling in s2_title_search to catch the empty iterable case
  • Added comprehensive test coverage with both mocked and VCR-based tests
  • Documented the edge case with an inline comment

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/paperqa/clients/semantic_scholar.py Added ValueError handling for empty data responses from Semantic Scholar API
tests/test_clients.py Added two tests to verify empty data responses are handled correctly
tests/cassettes/test_s2_title_search_empty_data.yaml VCR cassette capturing real API response with empty data

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jamesbraza jamesbraza merged commit 10f036a into main Feb 21, 2026
7 checks passed
@jamesbraza jamesbraza deleted the fixing-s2-crash branch February 21, 2026 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants