Skip to content

fix: handle empty SPOKE API response in get_context_using_spoke_api#47

Open
octo-patch wants to merge 1 commit intoBaranziniLab:mainfrom
octo-patch:fix/issue-42-empty-node-context
Open

fix: handle empty SPOKE API response in get_context_using_spoke_api#47
octo-patch wants to merge 1 commit intoBaranziniLab:mainfrom
octo-patch:fix/issue-42-empty-node-context

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #42

Problem

When the SPOKE Knowledge Graph API returns an empty list for a node (e.g. neurofibromatosis 2 which may have been removed in a KG update), get_context_using_spoke_api raises an IndexError at line 128:

context += node_value + " has a " + node_context[0]["data"]["properties"]["source"] + ...

This crashes because node_context[0] is accessed without checking whether node_context is empty.

Solution

Wrap the identifier-appending line in a guard that only executes when node_context is non-empty:

if node_context:
    context += node_value + " has a " + node_context[0]["data"]["properties"]["source"] + ...

When the API returns no results for a node, the function now gracefully returns an empty context string and an empty DataFrame instead of crashing.

Testing

Added unit tests in kg_rag/test/test_spoke_api_empty_response.py that mock the SPOKE API:

  1. test_empty_node_context_does_not_raise — verifies that an empty API response no longer raises IndexError, and that the function returns an empty context and empty DataFrame.
  2. test_non_empty_node_context_includes_identifier — verifies that normal (non-empty) API responses still include the identifier information in the returned context.

Both tests pass with the fix applied.

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.

IndexError: list index out of range in utility.get_context_using_spoke_api

1 participant