Blog

    FHIR Data Pipelines for AI and ML: Bulk Export, SQL on FHIR ViewDefinitions and Point-in-Time Features

    By GauravNovember 27, 20269 mins read
    On this page

    FHIR is built for exchanging records about one patient at a time, and model training wants wide tables of many patients. Teams that skip the step in between end up with notebooks full of custom JSON parsing, features that silently use the future, and training sets nobody can rebuild.

    This post is for data engineers and ML teams working from FHIR sources. It covers getting data out with Bulk export, flattening resources with SQL on FHIR ViewDefinitions, choosing the right timestamps, and building point-in-time feature tables you can reproduce.

    How do you build a FHIR data pipeline for AI and ML?

    Choosing where exported FHIR data should live?

    Our comparison of FHIR data stores sets out the options for holding and querying FHIR data at scale.

    A FHIR data pipeline for AI and ML exports resources in bulk, keeps the raw files, flattens each resource type into tables with shared view definitions, and builds features using only data known before each prediction time. Agnotic Technologies builds these pipelines so every training set can be traced to its export, views and code.

    The order matters. Each stage has one job, which makes errors easier to find and lets you rebuild a training set months later.

    Why is nested FHIR hard to train on?

    Nested FHIR is hard to train on because a single resource can hold lists of codings, components and references, and models want one row per entity with fixed columns. Every team that flattens FHIR by hand makes slightly different choices.

    • Repeated codings: an Observation may carry a LOINC code and a local code in the same list.
    • Components: a blood pressure Observation holds systolic and diastolic values as components.
    • Choice types: a value can be a quantity, a code or a string, depending on the observation.
    • References: medications, encounters and practitioners point to other resources by id.

    Hand-written parsers bury these choices in code. Two analysts then build two blood pressure features that disagree, and nobody can tell why.

    What is a SQL on FHIR ViewDefinition?

    A ViewDefinition is a shared, declarative recipe for turning one FHIR resource type into a flat table. It replaces custom parsing code with a definition that any compliant runner can execute.

    The SQL on FHIR implementation guide defines a ViewDefinition as a portable format for tabular views of FHIR data: each view is tied to a single resource type and uses FHIRPath expressions to define its columns, filters and unnesting.

    Because the view is data, not code, it can be reviewed, versioned and reused across projects. The specification is still moving through HL7 ballots, so pin the version your runner supports and test views when you upgrade.

    Getting the data out with Bulk export

    Bulk export is the usual way to pull FHIR data for many patients at once without paging through the regular API. It produces files per resource type that the rest of the pipeline reads.

    The HL7 FHIR Bulk Data Access guide defines an asynchronous export: the client sends a kick-off request, for example to [base]/Group/[id]/$export, polls a status location, then downloads output files that servers must support as newline-delimited JSON.

    • Scope: export only the resource types the model needs, for a defined group of patients.
    • Raw zone: store files exactly as received, with the export time, manifest and checksums.
    • Incremental runs: where the server supports it, request only changes since the last run.
    • Access: raw files are identified data and stay with a small, named group.

    Keeping raw files matters more for ML than for reporting. When a view or feature bug is found, you rerun from the raw zone instead of asking the source system for a new export of data that may have changed since.

    How should views be designed for model features?

    Design views as neutral, reusable tables, and keep model-specific logic in the feature step. A view for lab results should serve every model, not only the one you are building today.

    FHIR to machine learning pipeline: a FHIR server exports resources through Bulk Data export, raw NDJSON files are kept unchanged, SQL on FHIR ViewDefinitions flatten each resource type into tables, and a point-in-time feature builder joins them using only data available before each prediction time, with versioning of exports, views and feature sets across every step
    • Keep identifiers: resource id, patient reference and encounter reference on every row.
    • Keep all times: effective, issued and last updated, so the feature step can choose.
    • Unnest deliberately: one row per coding or component, with the code system as a column.
    • Keep status: final, amended and entered-in-error, so the feature step can filter.

    Resist filtering in the view. A view that drops cancelled results today makes it impossible to study them tomorrow without rewriting the view.

    Which FHIR timestamps should features use?

    Features should use the time the information was available to clinicians, not only the time the event happened. Using the wrong timestamp is the most common way FHIR features leak future data into training.

    • Observation effective time: when the sample was taken or the measurement made.
    • Observation issued: when the result was released, often hours or days later for some labs.
    • Condition onset and recorded date: when the problem began versus when someone documented it.
    • Meta last updated: when the server record last changed, which can follow any later edit.

    A lab drawn before the prediction time but issued after it was not known to the care team yet. A feature built on effective time alone counts it, and the model learns from information it will not have in production.

    Point-in-time feature tables

    A point-in-time feature table answers one question for every training row: what did we know about this patient at the prediction time? It joins view tables on patient and filters each source to data available before that time.

    • Prediction times: defined first, for example admission plus a set interval, per row.
    • As-of joins: each feature uses the latest value available before the prediction time.
    • Labels: outcomes taken strictly after the prediction time, inside a defined window.
    • Corrections: amended or entered-in-error records handled as they would be at prediction time.

    Build the same feature code for training and for live scoring. If production computes features differently, the model sees a different patient from the one it learned on.

    Split training and test sets by time as well as by patient. A model tested only on a random sample of the same period looks better than it will on next year's data.

    What goes wrong in FHIR ML pipelines?

    FHIR ML pipelines usually fail on time, codes and reproducibility rather than on modelling.

    • Future leakage: features built on effective time or last updated instead of availability.
    • Code drift: a site adds a local lab code and the view misses new results.
    • Silent deletions: entered-in-error records kept as valid values in training.
    • Unpinned views: a view changes between training and scoring without a version bump.
    • Unrepeatable sets: a training set built from a live table that has since changed.

    Each of these has a control: availability timestamps, code coverage checks, status filters in the feature step, versioned views and frozen training snapshots.

    A worked example: an inpatient fall risk model

    A health system wants a fall risk score at a set point after admission. The team exports Patient, Encounter, Observation and MedicationRequest for adult inpatients and keeps the raw files.

    ViewDefinitions flatten encounters, vital signs, mobility assessments and medication orders, keeping effective and issued times. The feature step sets each prediction time from the admission time and uses only results issued and orders authored before it.

    Labels come from fall events documented after the prediction time within the stay. The training set records the export run, view versions and feature code, so the same set can be rebuilt for a model review.

    Who owns what?

    FHIR ML pipelines cross data engineering, data science and clinical teams, so ownership of each layer must be explicit.

    • Data engineering: export jobs, the raw zone, view runners and view versioning.
    • Data science: prediction times, feature logic, labels and training set snapshots.
    • Clinical informatics: code lists, the meaning of statuses and which times reflect availability.
    • Privacy and security: access to raw files, de-identification rules and retention.

    Review views like code. A change to a shared view can alter features for every model that uses it, so it needs an owner, a review and a release note.

    How do you test the pipeline and its features?

    Test each layer against the one before it, then test features against hand-checked patients. Counts, code coverage and timing checks catch most problems before training.

    • Export check: files and counts match the manifest for every run.
    • View check: row counts per resource type and nulls per column, compared with the previous run.
    • Code coverage: share of Observations whose codes map to a known feature, by site.
    • Leakage test: rebuild features for sample rows and confirm no source time falls after the prediction time.

    Keep a small set of synthetic patients with known answers. Running them through every new view version shows exactly what a change did.

    What are FHIR ML data pipeline best practices?

    Export with Bulk Data, keep raw files unchanged, and flatten with versioned ViewDefinitions in place of custom parsers. Keep all timestamps and statuses in views and make choices in the feature step.

    Build point-in-time features from availability times, use the same feature code for training and scoring, and freeze every training set with its lineage.

    How Agnotic can help

    Agnotic builds FHIR data pipelines for AI teams: Bulk export jobs, raw storage, ViewDefinition libraries, point-in-time feature builders and the checks that keep training sets honest. We work with your FHIR servers, cloud platform and privacy rules.

    Build the data pipeline behind your models

    Our AI data pipeline service covers ingestion, transformation, feature stores and lineage for healthcare AI.

    Sources

    Frequently Asked Questions

    A declarative definition that turns one FHIR resource type into a flat table, using FHIRPath expressions for columns, filters and unnesting. Any compliant runner can execute it.

    Talk to an engineer about your project

    Share your FHIR sources and the models you plan. We will outline the export, views and feature builds they need.

    Go deeper

    Services and solutions on this topic