AI-assisted mapping and conditions
How AI suggests field mappings and filter conditions, what is masked before a model sees your sample, and why the model never touches a live message.
On this page
AI can draft two things for you: field mappings from a sample message, and condition expressions from a plain-English description. You review what it drafts and save it. After you save, a mapping or condition is plain configuration. The engine runs it the same way every time, and no model is involved.
sequenceDiagram
participant You
participant Engine as MedHook engine
participant Model as AI model (Anthropic)
You->>Engine: AI suggest (workflow's saved sample)
Engine->>Engine: De-identify the sample
Engine->>Model: De-identified sample + accepted examples
Model-->>Engine: Suggested mappings with confidence
Engine-->>You: Rows added to the Data Map
You->>Engine: Review, edit, Save
Note over Engine,Model: Live messages: no model callBefore you start
- An engine with AI turned on. See AI is off by default in production.
- An
ANTHROPIC_API_KEYon the engine. - A workflow with a saved sample message. See Test with sample messages.
- The
operatorrole.
AI is off by default in production
Prompts go to an outside model provider, so a production engine (NODE_ENV=production) keeps every AI feature off until an administrator sets AI_FEATURES_ENABLED=true. Do that only once a business associate agreement with the provider covers the deployment. Adding an API key alone does not turn AI on. Outside production, AI is on unless you set AI_FEATURES_ENABLED=false.
When AI is off:
- every AI route answers
403with the codeAI_FEATURES_DISABLED; - the availability endpoints report
disabled: true; - the builder hides AI suggest.
Without an API key, the mapping route answers 503. The condition route still works with a rule-based fallback, described below.
Steps
1. Suggest mappings
Open the workflow, go to the Data Map, and choose Load sample, then AI suggest. The engine reads the workflow's saved sample on the server. The browser does not send it again.
The engine de-identifies the sample, adds your earlier accepted mappings as examples, and asks the model for mappings. It detects the sample's format (HL7v2, JSON, FHIR, X12 or CSV) and asks for targets in the format chosen in Suggest targets as: FHIR or JSON. In Copy and edit mode on an HL7 sample, that select is hidden and the targets are HL7 fields. Each suggestion whose source is a valid HL7 field, or a JSON path for a JSON sample, becomes a new row in the map. Existing rows are never changed. With the flat field-path view on (off by default), a source can name a repetition or occurrence, such as PID.3(2).1 or OBX[2].5, and a Retrohook key in an answer (PID.3[0].1) is saved as PID.3(1).1. The panel says how many rows it added, and tells you to review the targets and Save.
Each added row shows the model's confidence, such as AI 92%, in amber below 70%, with Accept and Reject next to it. See Teach it your conventions.
Each suggestion carries a confidence score from 0 to 1:
| Score | Meaning |
|---|---|
| 0.9 to 1.0 | Direct match, high certainty |
| 0.7 to 0.89 | Strong match, a small transformation may be needed |
| 0.5 to 0.69 | Probable match, review it |
| Below 0.5 | Uncertain, a person must verify it |
curl -s -X POST "$ENGINE_URL/api/ai/mapping-suggest" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"workflowId": "wf_example", "targetFormat": "fhir"}'With workflowId, sourceFormat is optional and is detected from the saved sample. The response has suggestions (each with sourceField, targetField, confidence, reasoning and an optional transform), a summary and warnings. See Suggest mappings. The route allows 10 calls a minute, and each call is audited as ai.mapping_suggest with formats and counts, never content.

2. Teach it your conventions
Tell the engine which suggestions were right. On each row AI suggest added:
- Accept records the verdict and keeps the row. If you changed the target before you accepted, the engine records the target you kept and marks it as edited.
- Reject records the verdict and removes the row.
Then choose Save to keep the map. Saving does not accept anything for you, and rows you neither accept nor reject are plain mappings. The buttons are gone after you reload the page.
Accepted mappings feed later suggestions in the same workspace only. The next AI suggest call adds the 20 most recently accepted pairs from that workspace to the prompt as examples, and tells the model to use them as guidance, not to copy them. A rejected suggestion is stored but never used as an example.
The engine stores field paths only, never message values:
sourceFieldandtargetFieldmust be field paths, such asPID.8,hl7:PID-5.1orPatient.name[0].given[0]: letters, digits and_ . [ ] : -, no spaces, up to 256 characters. With the flat field-path view on, an HL7 path with a repetition, such asPID.3(2).1, is accepted too. Anything else is refused with400.- Optional
sourceContextandtargetContextare reduced to the field paths in them. Values are dropped, and a key that is not a field name, such as a map keyed by a patient name, is dropped with everything under it.
The builder sends the verdict for you. To record one over the API, use POST /api/ai/mapping-feedback:
curl -s -X POST "$ENGINE_URL/api/ai/mapping-feedback" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-H "X-Workspace-Id: $WORKSPACE_ID" \
-d '{"suggestionId": "6f1c2a9e-4b7d-4c1a-9f0e-2d8b5a7c3e11", "sourceField": "PID.8", "targetField": "Patient.gender", "accepted": true, "confidence": 0.92, "workflowId": "wf_example"}'suggestionId is the id AI suggest gave the suggestion. A second verdict on the same id replaces the first.
To read the verdicts in a workspace, use GET /api/ai/mapping-feedback. ?limit= takes up to 500 rows and ?accepted=true or ?accepted=false filters them. ?stats=true returns the accepted and rejected counts and the acceptance rate instead:
curl -s "$ENGINE_URL/api/ai/mapping-feedback?stats=true" \
-H "Authorization: Bearer $TOKEN" -H "X-Workspace-Id: $WORKSPACE_ID"Recording a verdict needs the operator role and reading needs viewer. Recording allows 20 calls a minute per person and answers 403 with AI_FEATURES_DISABLED while AI is off. Reading still works when AI is off. This is feedback for the prompt only. It does not train or change the model.
3. Build a condition from a description
The condition builder turns a sentence into a boolean expression for a condition step. Send the description to Suggest a condition:
curl -s -X POST "$ENGINE_URL/api/ai/condition-suggest" \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"description": "the resource type is Observation and status is final"}'You get an expression, an explanation and a confidence. Descriptions are limited to 1000 characters.
If there is no API key, or the model call fails, the engine answers with a rule-based fallback. It matches a few common patterns: age comparisons, status is, contains, resourceType is, has coding and an HL7v2 ADT message type. Anything else returns a stub with a confidence of 0.2. A failed model call adds _fallback: true to the response.
With the flat field-path view on, the available fields can be HL7 paths such as PID.5.1 or OBX[2].5. A plain path becomes an expression on the parsed message. A path with [n] or (n) has no expression form, so the answer says so, with a lower confidence, or suggests a rules filter instead.
Paste the expression into the condition step and test it. See Test with sample messages.
What is masked and what is not
The model never gets your sample as it is. The engine de-identifies it first, with a salted hash. The salt is random for each call, so one value gets the same placeholder inside one prompt, and nobody can reverse the placeholders with a dictionary. A hashed value looks like # followed by ten letters.
HL7v2 samples use a keep-list. A field is kept only if it is known not to identify anyone. Every other non-empty field is replaced by a hash, including fields in unknown and Z segments.
| Segment | Fields kept as they are |
|---|---|
| MSH | The whole segment. Sending and receiving application and facility, timestamp and message control id are not masked. |
| PID | 1 (set id), 8 (sex) |
| PV1 | 1, 2, 4, 10, 14, 18 (set id, patient class, admission type, clinical service, admit source, patient type) |
| OBX | 1, 2, 3, 6, 7, 8, 11 (the value in OBX-5 is hashed) |
| EVN, PV2, ORC, OBR, MSA, ERR, AL1, DG1 | A short list of codes, set ids and statuses each |
| Any other segment | Field 1 only |
So the patient name, identifiers, birth date, address and phone number are all hashed. A hashed field loses its component structure, because the whole field becomes one placeholder.
With FLAT_FIELD_PATHS_ENABLED=true (beta), the model gets one line per field instead, such as PID.3(2).1 = #abcdefghij (Patient Identifier List · ID Number) or MSH.9.1 = ADT. Each component is hashed on its own under the same keep-list, so the model sees which components are present and can answer with exact paths (OBX[2].5, PID.3(2).1). JSON samples are hashed first and then listed as $.name[0].family = #... lines. The view is off by default; see Field paths to turn it on.
Other formats:
| Format | Kept | Hashed |
|---|---|---|
| JSON and FHIR | Keys, the resourceType value, true, false and null | Every other string and number, including codes |
| X12 | Segment ids and separators | Every element value |
| CSV | The header row | Every cell below it |
Free text goes through pattern masking. This covers the workflow name the builder sends as context, and any text that is not one of the formats above. These are replaced:
- street addresses and PO boxes, and ZIP codes after a state or the word "zip";
- social security numbers, email addresses and phone numbers;
- an MRN after "MRN" or "medical record";
- dates written
12/31/2026or2026-12-31; - any run of seven or more digits;
- a name that follows a cue such as "patient", "Mr.", "name:" or "named".
Not masked:
- A name typed without a cue, such as "send Maria's labs". The engine cannot tell it from an adapter or field name.
- Dates in other formats, short numbers, and clinical words in free text.
- The kept HL7 fields in the table above, which are sent as they are.
- The CSV header row, field paths and the structure of your sample.
- The text of a condition description that is not in quotes. Quoted values such as
"Doe"are replaced by placeholders ([value-1]) before the model sees them, and the engine puts them back in the answer.
The model is never in the message path
AI helps you build a workflow. It does not run in one. The execution engine, the adapters and the MLLP listener make no model call. A saved mapping is a list of source and target paths with optional lookups and defaults. A saved condition is an expression. Both give the same result for the same message, and you can test them with a dry run.
Test it
- Make sure AI is on and a key is set.
GET /api/ai/mapping-suggestshould return"available": trueand"disabled": false. - Save this sample on a workflow.
MSH|^~\&|SENDER|FAC|RECEIVER|FAC|20260101120000||ADT^A01|MSG0001|P|2.5.1
PID|1||MRN0001^^^FAC^MR||TESTPATIENT^SAMPLE||19800101|F- Choose AI suggest in the Data Map.
The model sees the PID segment as something like PID|1||#hkcmdeoalb||#bnolecgdaf||#fpmahkdgjc|F: the set id and sex are kept and everything else is hashed. You should get new rows for fields such as PID.5 and PID.7. Choose Run test to check the output.
Troubleshooting
| Problem | Fix |
|---|---|
Answers 403 with AI_FEATURES_DISABLED | AI is off. An administrator sets AI_FEATURES_ENABLED=true after the agreement is in place. |
| AI suggest is missing | AI is off, or ANTHROPIC_API_KEY is empty. |
Answers 503 "AI mapping not available" | The engine has no ANTHROPIC_API_KEY. |
| "Save a sample message for this workflow first" | The workflow has no saved sample. Paste one. |
| "No new fields to suggest" | Every suggested field is already in the map, or none named a valid HL7 field or JSON path. |
The condition answer is a stub with 0.2 | No rule matched and the model was not used. Rephrase the description, or write the expression by hand. |
Answers 429 | AI routes allow 10 calls a minute. Wait and retry. |