Skip to main content

fpml (FHIRPathMappingLanguage)

The FHIRPath Mapping Language (FPML) is a data DSL designed to convert data from QuestionnaireResponse (and not only) to any FHIR Resource.

For more details visit the FHIRPathMappingLanguage specification.

Installation

You can install the package from PyPI using the following command:

pip install fpml

API Reference

resolve_template

The resolve_template function processes a given template with a specified FHIR resource, optionally applying a context and additional processing options.

from fpml import resolve_template

result = resolve_template(
    resource,
    template,
    context=None,
    fp_options=None,
    strict=False
)

Arguments:

  • resource (Resource): The input FHIR resource to process.
  • template (Any): The template describing the transformation.
  • context (Optional[Context], optional): Additional context data. Defaults to None.
  • fp_options (Optional[FPOptions], optional): Options for controlling FHIRPath evaluation. Defaults to None.
  • strict (bool, optional): Whether to enforce strict mode. Defaults to False. See more details on strict mode.

Returns:

  • Any: The processed output based on the template.

Raises:

  • FPMLValidationError: If validation of the template or resource fails.

Usage

For the following QuestionnaireResponse resource:

resource = {
    "resourceType": "QuestionnaireResponse",
    "status": "completed",
    "item": [
        {
            "linkId": "name",
            "answer": [
                {
                    "valueString": "Name"
                }
            ]
        }
    ]
}

Here's an example demonstrating how to use the resolve_template function:

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString }}"
        }
    ]
}

context = {}

result = resolve_template(resource, template, context)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Using FHIR data-model

from fpml import resolve_template
from fhirpathpy.models import models


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.value }}"  # <-- according R4 model
        }
    ]
}

context = {}

fp_options = {
    "model": models["r4"]
}

result = resolve_template(resource, template, context, fp_options)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Using user-defined functions

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString.strip() }}"  # <-- Custom function
        }
    ]
}

context = {}

user_invocation_table = {
    "strip": {
        "fn": lambda inputs: [i.strip() for i in inputs],
        "arity": {0: []},
    }
}

fp_options = {
    "userInvocationTable": user_invocation_table
}

result = resolve_template(resource, template, context, fp_options)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Caching compiled expressions

Parsing FHIRPath expressions is expensive, so expressions can be compiled once and reused via ExpressionCache passed through fp_options. The cache size is the number of compiled expressions kept in memory, zero disables caching.

Entries are keyed by the expression only, while compilation binds the model and the user-defined functions, so keep one long-living cache per fp_options. A cache is safe to share between threads.

from fhirpathpy.models import models

from fpml import ExpressionCache, resolve_template


# 1024 long expressions take up to 100mb
fp_options = {
    "model": models["r4"],
    "cache": ExpressionCache(max_size=1024),
}

for resource in resources:
    resolve_template(resource, template, context, fp_options)

Handling validation errors

from fpml import FPMLValidationError, resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where( }}"  # <-- Invalid expression
        }
    ]
}

context = {}

try:
    resolve_template(resource, template, context)
except FPMLValidationError as e:
    print(f"Validation error: {e.error_message}")
    print(f"Error path: `{e.error_path}`")

Output:

"Validation error: Cannot evaluate 'item.where(': where wrong arity: got 0"
"Error path: `name.0.text`"

Using strict mode

In strict mode only context variables can be used (starting with percent sigh). Any access of the resource property will raise a validation error.

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ item.where(linkId='name').answer.valueString }}"  # <-- Accessing resource attribute
        }
    ]
}

context = {} 


resolve_template(resource, template, context, strict=True)

Raises an error:

FPMLValidationError: Cannot evaluate 'item.where(linkId='name').answer.valueString': "Forbidden access to resource property 'item' in strict mode. Use context instead.". Path 'name.0.text'

Meanwhile using context:

from fpml import resolve_template


template = {
    "resourceType": "Patient",
    "name": [
        {
            "text": "{{ %QuestionnaireResponse.item.where(linkId='name').answer.valueString }}"  # <-- Accessing context variable
        }
    ]
}

context = {"QuestionnaireResponse": resource}  # <-- Context 

result = resolve_template(resource, template, context, strict=True)
print(result)

Output:

{'resourceType': 'Patient', 'name': [{'text': 'Name'}]}

Development

Local environment and testing

cd ./python
poetry install

To run tests:

poetry run pytest

Pre-commit hook

In ./python directory:

Run in the shell

autohooks activate

And edit ../.git/hooks/pre-commit replacing the first line with

#!/usr/bin/env -S poetry --project=./python run python

License

This project is licensed under the MIT License.

Metadata

Release files for fpml 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fpml 0.3.1
File Size Uploaded
fpml-0.3.1.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fpml 0.3.1
File Interpreter ABI Platform
fpml-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / fpml-0.3.1.tar.gz

Download URL fpml-0.3.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
62eb1f068097c7f895f23e0c22f862355974d5090fb57e9fe25056485b58c7d1
BLAKE2b-256 checksum
How to use checksums
b16a1dbbde14f7f1d1a75d2a0748bea577f182bd7501aa522230835e341c9dea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release files / fpml-0.3.1-py3-none-any.whl

Download URL fpml-0.3.1-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88d49a95ae2803dffa30c706c415409930d8abacc8d86831af801cca0bd35ed5
BLAKE2b-256 checksum
How to use checksums
f1a37486a71e4d00bf5635ef9a95965982d260f57766c45632450d9efbca16c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release history Release notifications | RSS feed

1.0.0

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page