Skip to content

Skip unsupported ontology prefixes in condition text search#1607

Open
davmlaw wants to merge 2 commits into
masterfrom
issue_1603_mpath_ontologyservice
Open

Skip unsupported ontology prefixes in condition text search#1607
davmlaw wants to merge 2 commits into
masterfrom
issue_1603_mpath_ontologyservice

Conversation

@davmlaw

@davmlaw davmlaw commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

What

Make condition_text_search() (classification/models/condition_text_search.py) resilient to bad results and bad responses from the Monarch Initiative search API. Two related failure modes in the one function are addressed:

  1. Unsupported ontology prefix — results whose prefix isn't a supported OntologyService are skipped instead of aborting the whole search.
  2. Error / non-JSON API response — the API call is guarded so an error or non-JSON response yields no results instead of raising.

Why

1. Unsupported prefix (ValueError)

condition_text_search builds OntologyTerm.get_or_stub(result.get("id")) for every item returned by the Monarch /v3/api/search endpoint. OntologyIdNormalized.normalize() (ontology/models/models_ontology.py) does prefix = OntologyService(prefix), which raises ValueError: 'MPATH' is not a valid OntologyService when the prefix isn't a member of the OntologyService enum. A single unexpected result (e.g. MPATH, the Mouse Pathology ontology) therefore breaks the entire search. (Rollbar 6250 / 7051.)

2. Error / non-JSON response (JSONDecodeError)

The function also called .json() directly on the response with no guard. The Monarch API is external and intermittently returns errors or a non-JSON error page (e.g. a 502 HTML page), so .json() raises JSONDecodeError: Expecting value: line 2 column 1 and aborts the search before normalize() is ever reached. This is a distinct failure from the MPATH bug — different exception type and line (.json() at line 18 vs get_or_stub/normalize at line 22). (Rollbar 7269.)

The call is now wrapped to raise_for_status() and catch RequestException/ValueError, returning [] (no results) on any API failure.

Choice of fix (prefix handling)

Two options were considered:

  • (a) Gracefully skip results whose prefix isn't a supported OntologyService.
  • (b) Add MPATH to the OntologyService enum.

Chose (a). OntologyService deliberately enumerates only the ontologies VariantGrid supports for conditions (MONDO, OMIM, HP, HGNC, DOID, ORPHA, MedGen, MeSH); it is not intended to mirror every prefix the external Monarch API might return. MPATH is a mouse pathology ontology, not a human disease/condition ontology, so it should not be added as a supported service. Skipping unknown prefixes keeps the strict enum intact while making the search robust to whatever the external API returns.

Tests

Adds classification/tests/models/test_condition_text_search.py covering: unsupported-prefix skip (MPATH alongside a valid MONDO term), non-JSON response, HTTP error response (502), and connection error — all mocking requests.get.

Risk

Low. Changes are confined to condition_text_search; the happy path (valid JSON, supported prefixes) is unaffected, and all failure modes degrade to "no search results" rather than an exception.

Addresses #1603

🤖 Generated with Claude Code

davmlaw added 2 commits June 11, 2026 12:13
The Monarch Initiative search API can return disease results whose IDs
use an ontology prefix that isn't a member of OntologyService (e.g.
MPATH, the Mouse Pathology ontology). OntologyIdNormalized.normalize()
constructs OntologyService(prefix), which raises
ValueError: 'MPATH' is not a valid OntologyService, aborting the whole
search.

Skip any result whose ID can't be resolved to a supported OntologyService
rather than letting it abort the search. OntologyService deliberately
enumerates only the ontologies we support, so MPATH is correctly excluded.
…1603

The Monarch /v3/api/search endpoint intermittently returns errors or a
non-JSON error page (e.g. a 502 HTML response), which made requests'
.json() raise JSONDecodeError and abort the whole condition search
(Rollbar item 7269). Catch RequestException/ValueError around the call
and treat any failure as no results.

Adds regression tests covering the non-JSON, HTTP error and connection
error cases alongside the existing unsupported-prefix (MPATH) skip.
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