Does AI-generated FHIR pass the HL7 validator? 3 LLMs tested

Three open models turned 30 synthetic clinical notes into FHIR. The best got 211 of 211 vital signs right, and none of the 90 Bundles passed the official HL7 validator.

Rogério Rodrigues

Rogério Rodrigues

Engenheiro de IA · mestrando em Informática em Saúde (UFSC)

Terminal escuro: Qwen3.8 27B com 211/211 sinais vitais certos e 0/30 no validador HL7, erro mais comum magic LOINC 2708-6 required. Texto: Acertou o dado. Errou o FHIR. 3 modelos, 90 Bundles, 0 aprovados.

Originally published in Portuguese on 16 October 2026: Portuguese version.

Hi everyone,

The RNDS (Brazil’s national health data network) only speaks FHIR. Anyone integrating health systems in Brazil has thought about the shortcut: ask the AI to build the Bundle straight from the clinical note. Or run the old HL7 v2 message through a converter and send the result.

I ran both paths and measured everything with the same yardstick. Of the 90 AI-generated Bundles, 0 passed the official HL7 validator. The short answer:

  • The two larger models get the content right and the form wrong. Qwen3.8 27B got 211 of 211 vital signs right (value, LOINC code and unit). The official HL7 validator rejected 30 of 30 of its Bundles. Across the three models, that was 90 Bundles and 0 passed.
  • The converter gets the form right and the content wrong. hl7gen 0.2.0 came out with 0 of 100 passing. After I added one line (the fullUrl of each entry), it went to 100 of 100. But none of the 250 Observations and Encounters points to the patient.
  • One repair round changes the score. Feeding the validator errors back to the model, just once, took gpt-oss-120b from 0 to 11 passing in the 16 notes the round got through. Qwen went to 6 of 9.

Passing the validator does not mean the data is usable. And correct data that fails the validator does not get into the RNDS. You need both yardsticks.

Test diagram: a synthetic note goes through three AI models and an HL7 v2 message goes through hl7gen; the FHIR Bundles from both sources go to the official HL7 validator and to the fidelity check against the answer key.

What exactly did I measure?

Two sources of FHIR, the same two yardsticks.

Source 1, AI. 30 synthetic clinical notes in Portuguese (made-up name, fictitious CNS (Brazilian national health card number), randomly drawn vital signs), each with an answer key. Three open models on Groq, all with the same frozen prompt and temperature 0: gpt-oss-20b, gpt-oss-120b and Qwen3.8 27B. The prompt asks for a FHIR R4 Bundle with Patient, Encounter and one Observation per vital sign, with LOINC and UCUM.

Source 2, converter. 100 synthetic HL7 v2 messages (50 ADT_A01, 50 ORU_R01) generated and converted by hl7gen 0.2.0, with no AI in between.

Yardstick 1, the official validator. The HL7 validator_cli (version 6.3.11), the same engine the FHIR community uses to validate Implementation Guides. It checks structure, type, cardinality and the core profiles. It does not check whether the data matches the note.

Yardstick 2, fidelity. A script of mine that compares each Bundle with the note’s answer key: is the vital sign there, with the right LOINC code, the right value and the right UCUM unit? Does the Observation point to the patient?

The prompt, verbatim (in Portuguese, as sent to the models):

Você é um sistema de integração hospitalar. Converta a nota clínica abaixo em um único
Bundle FHIR R4 em JSON (type "collection") com:
- 1 Patient (nome, sexo, data de nascimento, identificador CNS com system
  "https://saude.gov.br/fhir/sid/cns");
- 1 Encounter ambulatorial ligado ao Patient, com a data do atendimento;
- 1 Observation por sinal vital medido na nota, ligada ao Patient e ao Encounter, com código LOINC
  e unidade UCUM. Pressão arterial vai numa Observation só, com componentes sistólica e diastólica.
Responda APENAS com o JSON, sem texto antes ou depois e sem bloco de código.

Nota clínica:
<nota clínica sintética>

The scoreboard

SourceJSON parsedPasses validatorLinked to patientVital signs correctValid and faithful
hl7gen 0.2.0, as is100/1000/1000/250no answer keyn/a
hl7gen 0.2.0 + fullUrl100/100100/1000/250no answer keyn/a
gpt-oss-20b17/300/30118/118101/2110/30
gpt-oss-120b30/300/30211/211206/2110/30
Qwen3.8 27B30/300/30211/211211/2110/30
Scoreboard: gpt-oss-20b got 101 of 211 vital signs right, gpt-oss-120b 206 of 211, Qwen3.8 27B 211 of 211; all three had 0 of 30 Bundles pass the validator. With one repair round, gpt-oss-120b passed 11 of 16 and Qwen 6 of 9; gpt-oss-20b, 0 of 11.

“Valid and faithful” is the only number that matters for integration: it passed the validator AND said what the note says. With direct generation, it was zero for everyone.

What did the validator catch?

Four errors explain almost everything.

1. Oxygen saturation without the code the profile requires. 59 of the 60 Bundles from gpt-oss-120b and Qwen failed here. Both coded SpO2 with LOINC 59408-5 (pulse oximetry), which is a correct code. But the core FHIR vital signs profile also requires 2708-6 in the same Observation. The validator says it plainly:

Error @ Bundle.entry[6].resource/*Observation/obs-spo2*/.code
OxygenSatCode: magic LOINC code 2708-6 required, but not found
(from http://hl7.org/fhir/StructureDefinition/oxygensat|4.3.0).
Note that other Observation codes are allowed in addition to this required magic code

The fix is one more entry in coding:

{
  "code": {
    "coding": [
      {
        "system": "http://loinc.org",
        "code": "2708-6",
        "display": "Oxygen saturation in Arterial blood"
      },
      {
        "system": "http://loinc.org",
        "code": "59408-5",
        "display": "Oxygen saturation in Arterial blood by Pulse oximetry"
      }
    ]
  }
}

I wouldn’t remember that detail off the top of my head either. The model has no way to guess this rule; the validator catches it.

2. A fullUrl that isn’t a UUID. The models write urn:uuid:patient-001. It looks like a UUID, but it isn’t. The validator rejects it: UUIDs must be valid and lowercase (patient-001). It showed up in all 30 Qwen Bundles.

3. Bundle without fullUrl. The same error as hl7gen, and also in 13 of the 17 readable Bundles from gpt-oss-20b. In a Bundle of type collection, every entry needs a fullUrl. It is the resource’s identity inside the package.

4. Made-up unit. gpt-oss-20b wrote beats/min, breaths/min, bpm, irpm and 1/min as the UCUM code in 9 of the 17 readable Bundles (and the 120b, in 2). The value was right, 93 is 93. But the vital signs profile fixes the code to /min, and the validator flags it: Value is 'beats/min' but must be '/min'. (1/min is actually valid UCUM, it just isn’t what the profile asks for.)

// what gpt-oss-20b wrote (nota-007)
"valueQuantity": {"value": 93, "unit": "beats/min",
                  "system": "http://unitsofmeasure.org", "code": "beats/min"}

// what the profile requires: code "/min"
"valueQuantity": {"value": 93, "unit": "beats/minute",
                  "system": "http://unitsofmeasure.org", "code": "/min"}

What does the validator not catch?

Everything below passes the validator silently (without a terminology server, which is how I ran it here).

Observations that don’t point to the patient. That’s the hl7gen case: 0 of 250 references. You get a “valid” Bundle with Observations that belong to nobody, with a random code and no system. On yardstick 1 it isn’t even an error. It becomes a best-practice warning (all observations should have a subject), lost among the other warnings.

A time nobody measured. The note only gives the date of the visit. Qwen filled in the time as T00:00:00Z (for example, 2026-10-03T00:00:00Z) in 24 of the 30 Bundles. Midnight UTC is 9 p.m. the previous day in Brasília. The record moves to another day and the validator is perfectly happy.

The right code for the wrong thing. In 2 Bundles gpt-oss-120b coded capillary blood glucose (mg/dL) with LOINC 14749-6, which is glucose in mmol/L in serum. The unit says mg/dL and the code says a different measurement.

What happens when the agent feeds the errors back to the model?

The test agent does this: generate, validate, send the list of errors back to the model and validate again. One round only, without me touching the JSON.

# src/repair.py (simplified excerpt): one round, no hand edits to the JSON
erros = sorted({m for s, m in val.get(src.name, []) if is_error(s, m)})
if not erros:          # already passed the validator: don't spend a call
    dest.write_text(src.read_text()); continue
prompt = REPAIR.format(nota=note["texto"],
                       erros="\n".join(f"- {e}" for e in erros[:25]),
                       bundle=src.read_text())
text, _ = call(model, prompt, max_tokens=budget)
dest.write_text(extract_json(text))   # only if the JSON is valid; broken counts as a failure

Result of the repair round:

Model (notes in the round)JSON parsedPasses validatorVital signs correctValid and faithful
gpt-oss-20b (11)4/110/1128/800/11
gpt-oss-120b (16)16/1611/16115/11610/16
Qwen3.8 27B (9)6/96/944/666/9

gpt-oss-120b passed 11 of the 16 Bundles and kept 115 of 116 vital signs correct: fixing the form did not break the content. Qwen passed the 6 Bundles that came back whole. The other 3 came back truncated, and that one is on me: to fit the free Groq limit I lowered the response token cap, and the JSON ended mid-way. gpt-oss-20b passed none. Of the 11 it attempted, 7 came back with broken JSON: 6 were already broken and 1, which had been readable, broke during the repair.

There’s a big asterisk: the round stopped halfway because the free Groq daily token limit ran out (200,000 per day per model). That’s why it’s 16, 9 and 11 notes, not 30. Running all 30 is the first item for the next version of the repo.

What didn’t work?

I’ll admit a good part of the time went into fighting the environment, not FHIR.

  • The HL7 package registry was blocked on the machine where I ran this. The validator downloads the core packages by itself, and here it couldn’t. I installed them via npm (packages published by Grahame Grieve). hl7.fhir.r4.core on npm is an empty security placeholder package, so I validated against R4B (4.3.0), not R4. For Patient, Encounter and Observation the rules used here are the same, but it is a swap and I’m stating it. In the repo, anyone with network access can validate against R4 with tools/validate.sh <pasta> <saida> 4.0.1.
  • No terminology server. I ran with -tx n/a. The validator doesn’t check whether the LOINC code exists, and it raises an error when it can’t check a required UCUM binding. Those errors (39 for Qwen, 39 for the 120b, 21 for the 20b) come from the environment, not the model, and were left out of the count.
  • The new validator ran out of memory. Version 7.0.2 died at 6 GB. I went back to 6.3.11.
  • gpt-oss-20b broke the JSON in 13 of 30 notes. It closes a brace in the wrong place, in the middle of the Bundle. The parser can’t read it, end of story.
  • An 8,000 tokens per minute limit on free Groq. The Qwen repair round crawled because of it.

When not to use this result

  • n = 30 notes, 3 models, 1 prompt. It shows the error pattern; it doesn’t rank models. A different prompt (for example, pasting the SpO2 profile into the prompt) changes the score.
  • Synthetic notes are cleaner than real ones. Real notes have abbreviations, typos and vital signs written ten different ways. Fidelity will likely drop.
  • I didn’t validate against the RNDS profiles. That’s the next step, and it’s where the “Ministry of Health standard” lives. The official validator already runs profiles; I just need to load the package.
  • Yardstick 2 is mine. It checks 8 vital signs and the link to the patient. It doesn’t check name, sex, CNS or date.

Source and how to reproduce

Repository: github.com/rogeriorrodrigues/fhir-eval (MIT). Everything is synthetic: notes, HL7 v2 messages and Bundles.

git clone https://github.com/rogeriorrodrigues/fhir-eval && cd fhir-eval
pip install -r requirements.txt
pytest -q -s tests                       # runs with no network and no model

# official HL7 validator (Java 17+)
curl -L -o tools/validator_6.3.11.jar \
  https://github.com/hapifhir/org.hl7.fhir.core/releases/download/6.3.11/validator_cli.jar

# generate with your model (Groq, Ollama or any OpenAI-compatible API)
cp .env.example .env && set -a && . ./.env && set +a
python3 src/generate.py data/notes.jsonl out/llm/meu-modelo
tools/validate.sh out/llm/meu-modelo out/llm-meu-modelo-oo.json 4.0.1   # R4
python3 src/semantic.py data/notes.jsonl out/llm/meu-modelo

Have you put AI-generated FHIR into production? What do you check besides the validator? Tell me in the comments.

Conhece alguém que precisa ler isso?

Manda pra essa pessoa. Leva dez segundos e é o que mais ajuda esse trabalho a chegar em quem usa IA com dado de verdade.

Rogério Rodrigues

Rogério Rodrigues

Engenheiro de IA há 20 anos em software, mestrando em Informática em Saúde na UFSC. Escrevo o que rodei: IA com dado de saúde, FHIR e LGPD, com código aberto.

Ficou uma dúvida? Discorda de algo? Me chama.

Ver publicações científicas