Skip to main content

fhir4ds.fhirpath

Core FHIRPath R4 evaluation engine. Provides a pure-Python evaluator and high-performance DuckDB SQL integration.

1. At a Glance

The most common entry points for direct evaluation:

from fhir4ds.fhirpath import evaluate # Evaluate against a Python object
from fhir4ds.fhirpath.duckdb import register_fhirpath # Register UDFs on DuckDB

2. Function Reference

evaluate

evaluate(resource, path, context=None, model=None, options=None) -> list

Evaluates a FHIRPath expression against a Python dictionary or list of resources.

ParameterTypeDescription
resource`dictlist`
pathstrThe FHIRPath expression.
contextdict(Optional) Map of variable names to values (e.g. %patient).
modeldict(Optional) The model data object specific to a domain, e.g. R4.
optionsdict(Optional) Config like {"strict_mode": True}.

3. SQL UDF Reference

Registered via fhir4ds.register().

FunctionReturnsDescription
fhirpath(json, expr)VARCHAR[]All matching elements as a list.
fhirpath_text(json, expr)VARCHARThe first matching element as text.
fhirpath_bool(json, expr)BOOLEANEvaluates expression to a boolean.
fhirpath_number(json, expr)DOUBLEThe first matching numeric value.

4. Supported Features

The FHIR4DS engine provides 100% coverage for the following FHIRPath R4 features:

Feature CategoryExamplesStatus
NavigationPatient.name.given✅ Full
FilteringObservation.where(status='final')✅ Full
Collectionsfirst(), last(), exists(), all(), count()✅ Full
Logicand, or, xor, implies, not✅ Full
Math+, -, *, /, mod, div✅ Full
Stringsubstring(), startsWith(), contains(), matches()✅ Full
Typesis(), as(), ofType()✅ Full
Utilitynow(), today(), trace()✅ Full

5. Advanced Configuration

Strict Mode

Enable strict validation to catch common authoring mistakes:

  • Choice-type suffix validation (e.g. valueQuantity).
  • Property name validation.
  • Polymorphic type narrowing enforcement.
evaluate(res, "Patient.typo", options={"strict_mode": True}) # Raises error

Custom Resource Types

The evaluator supports non-standard FHIR profiles:

evaluate(resource, "MyCustom.field", resource_type="MyCustomResource")