Skip to content

Conversation

rohan-019
Copy link
Contributor

🧠 Add @fhir.predict() Decorator for Simplified ML Model Deployment

Closes : #143

Summary

This PR introduces a new @fhir.predict() decorator in the BaseFHIRGateway to make deploying machine learning models as FHIR-compliant endpoints simpler and more consistent. It abstracts the boilerplate involved in constructing FHIR resources, letting developers focus purely on model inference logic.

Key Features

  • New decorator: @fhir.predict(resource=RiskAssessment)
  • ✅ Automatically wraps raw prediction outputs (float or dict) into FHIR RiskAssessment resources.
  • ✅ Supports both sync and async prediction functions.
  • ✅ Integrates seamlessly with FastAPI route registration and the existing FHIR gateway pattern.
  • ✅ Updates CapabilityStatement to include prediction endpoints.

Implementation Details

  • Added prediction handling logic in healthchain/gateway/fhir/base.py:
    • _wrap_prediction() to construct FHIR-compliant resources.
    • Enhanced _execute_handler() to handle async/sync predictions.
    • predict() decorator to register ML endpoints.
  • Added comprehensive unit tests in tests/gateway/test_base_fhir_gateway.py.

Example Usage

@fhir.predict(resource=RiskAssessment)
def sepsis_risk(patient_id: str) -> float:
    return 0.85  # High risk

Automatically serves a FHIR endpoint:
GET /predict/RiskAssessment/{id}

Response:

{
  "resourceType": "RiskAssessment",
  "status": "completed",
  "subject": {"reference": "Patient/123"},
  "prediction": [{"probabilityDecimal": 0.85}]
}

Checklist

  • I have read the contributing guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Signed-off-by: rohan-019 <[email protected]>
Signed-off-by: rohan-019 <[email protected]>
Signed-off-by: rohan-019 <[email protected]>
Signed-off-by: rohan-019 <[email protected]>
Signed-off-by: rohan-019 <[email protected]>
Signed-off-by: rohan-019 <[email protected]>
@adamkells
Copy link
Contributor

Hi @rohan-019, just a heads up that you can run the tests and code formatting locally before pushing. There are instructions in the Contributing.md file.

@rohan-019
Copy link
Contributor Author

rohan-019 commented Oct 4, 2025

Hey, @jenniferjiangkells
please review !!

@rohan-019
Copy link
Contributor Author

Hi @rohan-019, just a heads up that you can run the tests and code formatting locally before pushing. There are instructions in the Contributing.md file.

Hey @adamkells
Thankyou for your instructions and also it's good things to check the tests and code formatting.
and I have checked tests and formatting.

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.

Create @fhir.predict() decorator for simplified ML model deployment

2 participants