nazrulworld / fhir.resources

FHIR Resources https://www.hl7.org/fhir/resourcelist.html
https://pypi.org/project/fhir.resources/
Other
365 stars 104 forks source link

Error in ID length on EPIC Sandbox data #152

Closed cyrilzakka closed 5 months ago

cyrilzakka commented 5 months ago

Description

Trying to parse Condition data from the Epic Sandbox by following the tutorial here: https://fhir.epic.com/Documentation?docId=fhir

b'{"resourceType":"Bundle","type":"searchset","total":3,"link":[{"relation":"self","url":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition?patient=eq081-VQEgP8drUUqCWzHfw3"}],"entry":[{"link":[{"relation":"self","url":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eVGf2YljIMIk76IcfbNpjWQ3"}],"fullUrl":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eVGf2YljIMIk76IcfbNpjWQ3","resource":{"resourceType":"Condition","id":"eVGf2YljIMIk76IcfbNpjWQ3","clinicalStatus":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-clinical","version":"4.0.0","code":"active","display":"Active"}],"text":"Active"},"verificationStatus":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-ver-status","version":"4.0.0","code":"confirmed","display":"Confirmed"}],"text":"Confirmed"},"category":[{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-category","code":"problem-list-item","display":"Problem List Item"}],"text":"Problem List Item"}],"code":{"coding":[{"system":"http://hl7.org/fhir/sid/icd-10-cm","code":"Z91.89","display":"Other specified personal risk factors, not elsewhere classified"},{"system":"http://snomed.info/sct","code":"315016007","display":"At Risk Of Coronary Heart Disease"}],"text":"Risk for coronary artery disease between 10% and 20% in next 10 years"},"subject":{"reference":"Patient/eq081-VQEgP8drUUqCWzHfw3","display":"Lin, Derrick"},"onsetDateTime":"2019-05-28","recordedDate":"2019-05-28"},"search":{"mode":"match"}},{"link":[{"relation":"self","url":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eY-LMUKgFarb5r10D5sXS7nGJO9qELcndS5oncvyDjPHp.lFiCEKE6mt2pIDbyFeBHvU6Z0XikLVgIqkXp8XV1Q3"}],"fullUrl":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eY-LMUKgFarb5r10D5sXS7nGJO9qELcndS5oncvyDjPHp.lFiCEKE6mt2pIDbyFeBHvU6Z0XikLVgIqkXp8XV1Q3","resource":{"resourceType":"Condition","id":"eY-LMUKgFarb5r10D5sXS7nGJO9qELcndS5oncvyDjPHp.lFiCEKE6mt2pIDbyFeBHvU6Z0XikLVgIqkXp8XV1Q3","clinicalStatus":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-clinical","version":"4.0.0","code":"active","display":"Active"}],"text":"Active"},"verificationStatus":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-ver-status","version":"4.0.0","code":"confirmed","display":"Confirmed"}],"text":"Confirmed"},"category":[{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/condition-category","code":"encounter-diagnosis","display":"Encounter Diagnosis"}],"text":"Encounter Diagnosis"},{"coding":[{"system":"http://open.epic.com/FHIR/StructureDefinition/condition-category","code":"visit-diagnosis","display":"Visit Diagnosis"}],"text":"Visit Diagnosis"}],"code":{"coding":[{"system":"http://snomed.info/sct","code":"315016007","display":"At risk of coronary heart disease (finding)"},{"system":"http://hl7.org/fhir/sid/icd-9-cm","code":"V49.89","display":"Other specified conditions influencing health status(V49.89)"},{"system":"http://hl7.org/fhir/sid/icd-10-cm","code":"Z91.89","display":"Other specified personal risk factors, not elsewhere classified"}],"text":"Risk for coronary artery disease between 10% and 20% in next 10 years"},"subject":{"reference":"Patient/eq081-VQEgP8drUUqCWzHfw3","display":"Lin, Derrick"},"encounter":{"reference":"Encounter/e6kBx6PwnxIv.5kCifScsWg3","identifier":{"use":"usual","system":"urn:oid:1.2.840.114350.1.13.0.1.7.3.698084.8","value":"27556"},"display":"Office Visit"},"onsetDateTime":"2019-05-28","recordedDate":"2019-05-28","evidence":[{"detail":[{"reference":"Condition/eVGf2YljIMIk76IcfbNpjWQ3","display":"Risk for coronary artery disease between 10% and 20% in next 10 years"}]}]},"search":{"mode":"match"}},{"link":[{"relation":"self","url":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eY-LMUKgFarb5r10D5sXS7tjBxvqEuuIjSj-3DP.gq8M3"}],"fullUrl":"https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Condition/eY-LMUKgFarb5r10D5sXS7tjBxvqEuuIjSj-3DP.gq8M3","resource":{"resourceType":"Condition","id":"eY-LMUKgFarb5r10D5sXS7tjBxvqEuuIjSj-3DP.gq8M3","category":[{"coding":[{"system":"http://open.epic.com/FHIR/StructureDefinition/condition-category","code":"reason-for-visit","display":"Reason for Visit"}],"text":"Reason for Visit"}],"code":{"coding":[{"system":"urn:oid:1.2.840.114350.1.13.0.1.7.2.728286","code":"83","display":"Annual Exam"}],"text":"Annual Exam"},"subject":{"reference":"Patient/eq081-VQEgP8drUUqCWzHfw3","display":"Lin, Derrick"},"encounter":{"reference":"Encounter/e6kBx6PwnxIv.5kCifScsWg3","identifier":{"use":"usual","system":"urn:oid:1.2.840.114350.1.13.0.1.7.3.698084.8","value":"27556"},"display":"Office Visit"},"recordedDate":"2019-05-28T13:56:10Z","recorder":{"reference":"Practitioner/eUXiD4IyBLJFoR2VHOjfTdg3","display":"Physician Family Medicine, MD"}},"search":{"mode":"match"}}]}'
Traceback (most recent call last):
  File "/Users/cyril/Documents/GitHub/almanac-agent/epic_tools.py", line 134, in <module>
    conditions_for_patient = fetch_conditions(base_url, access_token, patient_id)
  File "/Users/cyril/Documents/GitHub/almanac-agent/epic_tools.py", line 70, in fetch_conditions
    bundle = Bundle.parse_obj(bundle_json)
  File "/Users/cyril/Library/Python/3.9/lib/python/site-packages/pydantic/v1/main.py", line 526, in parse_obj
    return cls(**obj)
  File "/Users/cyril/Library/Python/3.9/lib/python/site-packages/fhir/resources/core/fhirabstractmodel.py", line 105, in __init__
    BaseModel.__init__(__pydantic_self__, **data)
  File "/Users/cyril/Library/Python/3.9/lib/python/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for Bundle
entry -> 1 -> resource -> id
  ensure this value has at most 64 characters (type=value_error.any_str.max_length; limit_value=64)

What I Did


def fetch_conditions(base_url, access_token, patient_id):
    """
    Fetch condition data for a patient using the patient's id.

    :param base_rul: Base URL for the FHIR API.
    :param access_token: Access token for authenticating API requests.
    :param patient_id: The patient's id.
    :return: List of patient conditions or None if an error occurred.
    """
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/fhir+json',
        'Accept': 'application/fhir+json'
    }
    response = requests.get(f"{base_url}Condition?patient={patient_id}", headers=headers)
    if response.status_code == 200:
        conditions = []
        print(response.content)
        bundle_json = json.loads(response.content.decode('utf-8'))
        for entry in bundle_json.get('entry', []):
            resource = entry.get('resource', {})
            resource_id = resource.get('id', '')
            if len(resource_id) > 64:
                # Truncate the ID or handle it as needed
                resource['id'] = resource_id[:64]
        bundle = Bundle.parse_obj(bundle_json)
nazrulworld commented 5 months ago

@cyrilzakka please have a look here https://github.com/nazrulworld/fhir.resources?tab=readme-ov-file#resourceid-aka-fhirtypesid-constraint-extensibility

cyrilzakka commented 5 months ago

Thank you!

nazrulworld commented 5 months ago

Can you find the value of ID that cause validation and paste here

cyrilzakka commented 5 months ago

My bad, I forgot to import the correct sub-package! Apologies for the trouble