Skip to main content

helix.fhir.client.sdk

Continuous Integration Latest Release GitHub license

Fluent API to call the FHIR server that handles:

  1. Authentication to FHIR server
  2. Renewing access token when they expire
  3. Retry when there are transient errors
  4. Un-bundling the resources received from FHIR server
  5. Paging
  6. Streaming
  7. Logging
  8. Simulating a $graph call when the server does not support it

Usage

pip install helix.fhir.client.sdk

Documentation

https://icanbwell.github.io/helix.fhir.client.sdk/

Test Project using this

https://github.com/icanbwell/fhir-server-performance

Python Version Support

  • 1.x supports python 3.7+
  • 2.x supports python 3.10+
  • 3.x supports python 3.12+

Asynchronous Support

When communicating with FHIR servers, a lot of time is spent waiting for the server to respond. This is a good use case for using asynchronous programming. This SDK supports asynchronous programming using the async and await keywords.

The return types are Python AsyncGenerators. Python makes it very easy to work with AsyncGenerators.

For example, if the SDK provides a function like this:

async def get_resources(self) -> AsyncGenerator[FhirGetResponse, None]:
    ...

You can iterate over the results as they become available:

response: Optional[FhirGetResponse]
async for response in client.get_resources():
    print(response.resource)

Or you can get a list of responses (which will return AFTER all the responses are received:

responses: List[FhirGetResponse] = [response async for response in client.get_resources()]

Or you can aggregate the responses into one response (which will return AFTER all the responses are received:

response: Optional[FhirGetResponse] = await FhirGetResponse.from_async_generator(client.get_resources())

Data Streaming

For FHIR servers that support data streaming (e.g., b.well FHIR server), you can just set the use_data_streaming parameter to stream the data as it is received. The data will be streamed in AsyncGenerators as described above.

Streaming $merge Responses

use_data_streaming also applies to $merge calls (merge_async, merge_resources_async, merge_bundle_async, merge_bundle_uncompressed). When enabled, the SDK sends Accept: application/fhir+ndjson so a b.well FHIR server that supports it (see merge.md) can stream the $merge response back instead of building the whole response in memory first.

from helix_fhir_client_sdk.fhir_client import FhirClient

fhir_client = FhirClient().url(fhir_server_url).resource("Patient").use_data_streaming(True)

merge_response = await FhirMergeResponse.from_async_generator(
    fhir_client.merge_async(json_data_list=[json.dumps(patient_resource)])
)

This only changes how the response is read; it does not change the request body. If you also want the request body sent with chunked transfer encoding (independent of response streaming), opt in separately with send_data_as_chunked(True).

Error responses (e.g. a 400 with an OperationOutcome) are unaffected by streaming - the SDK always returns the full error body via response.error/response.responses[...]["issue"], whether or not use_data_streaming is enabled.

Persistent Sessions (Connection Reuse)

By default, the SDK creates a new HTTP session for each request. For better performance (~4× faster), you can use persistent sessions to reuse connections across multiple requests.

Important: When you provide a custom session factory using use_http_session(), YOU are responsible for managing the session lifecycle, including closing it when done. The SDK will NOT automatically close user-provided sessions.

import aiohttp
from helix_fhir_client_sdk.fhir_client import FhirClient

# Create a persistent session for connection reuse
session = aiohttp.ClientSession()

try:
    # Configure FhirClient to use persistent session
    fhir_client = (
        FhirClient()
        .url("https://fhir.example.com")
        .resource("Patient")
        .use_http_session(lambda: session)  # User provides session factory
    )
    
    # Multiple requests reuse the same connection (~4× performance boost)
    response1 = await fhir_client.get_async()
    response2 = await fhir_client.clone().resource("Observation").get_async()
    
finally:
    # User must close the session when done
    await session.close()

Session Lifecycle Rules:

  • No custom factory (default): SDK creates and closes the session automatically
  • Custom factory provided: User is responsible for closing the session

JSON Handling

The SDK reads and writes JSON with orjson. Compared with earlier versions, by default:

  • request bodies and get_response_text() are compact ({"a":1}), not {"a": 1};
  • non-ASCII characters are written as raw UTF-8, not \uXXXX escapes;
  • NaN and Infinity are written as null;
  • an integer outside -2^63..2^64-1 is parsed as a float, and NaN, Infinity and nesting deeper than 1024 levels are rejected when parsing a response.

Raw UTF-8 $merge bodies need a FHIR server that decodes multi-byte characters split across request chunks (icanbwell/fhir-server#2775). Deploy that first.

If you assert on, hash or persist the exact text, keep the previous behavior with:

fhir_client = FhirClient().byte_identical_json()

See FhirClient.byte_identical_json in the API reference for the full table. Cache hashes (raw_hash) are the same in both modes.

Storage Compression

The FHIR client SDK supports two types of compression:

  1. HTTP Compression (compress): Compresses HTTP request body when sending data to the server. Default: enabled
  2. In-Memory Storage (storage_mode): Controls how FHIR resources are stored in memory. Default: raw (no compression)

Disabling HTTP Compression

HTTP compression (gzip) is enabled by default for request bodies. To disable it:

from helix_fhir_client_sdk.fhir_client import FhirClient

# Disable HTTP compression for requests
fhir_client = FhirClient().url("https://fhir.example.com").compress(False)

In-Memory Storage Modes

The SDK supports different storage modes for FHIR resources through the set_storage_mode() method. By default, resources are stored as raw Python dictionaries (no compression).

from helix_fhir_client_sdk.fhir_client import FhirClient
from compressedfhir.utilities.compressed_dict.v1.compressed_dict_storage_mode import CompressedDictStorageMode

# Use raw storage (default) - no compression, resources stored as plain Python dicts
fhir_client = FhirClient().set_storage_mode(CompressedDictStorageMode(storage_type="raw"))

# Use msgpack storage - stores resources in msgpack format
fhir_client = FhirClient().set_storage_mode(CompressedDictStorageMode(storage_type="msgpack"))

# Use compressed msgpack storage - stores resources in compressed msgpack format
fhir_client = FhirClient().set_storage_mode(CompressedDictStorageMode(storage_type="compressed_msgpack"))

Available storage types:

  • raw: Default. Resources are stored as standard Python dictionaries (no compression)
  • msgpack: Resources are serialized using MessagePack for efficient storage
  • compressed_msgpack: Resources are serialized using MessagePack and then compressed

Getting Raw Python Dictionaries

To completely bypass the compressedfhir library and get plain Python dictionaries:

# Returns plain Python dicts, not FhirResource objects
result = await fhir_client.get_raw_resources_async()
resources = result["_resources"]  # list[dict[str, Any]]

Metadata

Release files for helix.fhir.client.sdk 6.1.0

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

Source distribution (sdist)

Source distribution for helix.fhir.client.sdk 6.1.0
File Size Uploaded Provenance Actions
helix_fhir_client_sdk-6.1.0.tar.gz 254.3 kB Without provenance Details for helix_fhir_client_sdk-6.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for helix.fhir.client.sdk 6.1.0
File Interpreter ABI Platform Actions
helix_fhir_client_sdk-6.1.0-py3-none-any.whl Python 3 none any Details for helix_fhir_client_sdk-6.1.0-py3-none-any.whl

Total release size: 661.9 kB

Release files / helix_fhir_client_sdk-6.1.0.tar.gz

Download URL helix_fhir_client_sdk-6.1.0.tar.gz
Size 254.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bc33db88b4adf22bc696c7ceb75fec6429b461af34a5225de2467794605a643b
BLAKE2b-256 checksum
How to use checksums
4f6b218a5a95ebca77dc96fab76c9bd2712c52fe47dac315bb5f08372816c728
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / helix_fhir_client_sdk-6.1.0-py3-none-any.whl

Download URL helix_fhir_client_sdk-6.1.0-py3-none-any.whl
Size 407.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83ca93554f2b8db7b5627fa9870c9a88a937b84480f0cd662ba1ac295a0165ac
BLAKE2b-256 checksum
How to use checksums
855257bc9223a0c69c9d1c6238f778dc670da49cd404834f89aede71e025f5e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for helix.fhir.client.sdk 6.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

6.1.0 This release

2 release files No provenance

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