Summary mode and comments

Summary mode

The FHIR search parameter _summary=true asks a server to return only elements marked as summary in the specification. Every model_dump* method can produce that view with summary_only=True:

>>> from fhir.resources.organization import Organization
>>> org = Organization.model_validate({
...     "resourceType": "Organization",
...     "id": "f001",
...     "text": {"status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">Acme</div>"},
...     "active": True,
...     "name": "Acme Corporation",
...     "contact": [{"name": [{"text": "Reception"}]}],
... })
>>> org.model_dump_json(summary_only=True)
'{"resourceType":"Organization","id":"f001","active":true,"name":"Acme Corporation"}'

text, contained, extension and every non-summary element (here contact) are left out. It works the same for model_dump, model_dump_xml and model_dump_yaml.

Note

The specification says a server should mark subsetted output with the SUBSETTED tag in meta.tag. fhir.resources does not add it for you; add it yourself if you return summaries from a FHIR API.

To find out which elements are part of the summary for a model:

>>> Organization.summary_elements_sequence()
['id', 'meta', 'implicitRules', 'modifierExtension', 'identifier', 'active', 'type', 'name', 'description', 'partOf']

Comments in JSON

JSON has no comments. The convention proposed by Grahame Grieve — a fhir_comments array on any object — is supported, including on primitive values through their _element:

>>> from fhir.resources.observation import Observation
>>> obs = Observation.model_validate({
...     "resourceType": "Observation",
...     "fhir_comments": ["Imported from the lab system"],
...     "status": "final",
...     "_status": {"fhir_comments": ["Status confirmed by phone"]},
...     "code": {"text": "Glucose"},
... })
>>> "fhir_comments" in obs.model_dump_json()
True

Strip them on output with exclude_comments=True:

>>> obs.model_dump_json(exclude_comments=True)
'{"resourceType":"Observation","status":"final","code":{"text":"Glucose"}}'

In XML output, comments become real <!-- --> comments.