GrepBoard/Manuals
605 manuals ·

Match merge tags in an AI email template to your list columns

·Tested on Claude Sonnet 5.5 · Python 3.13 · 2026-10·Test scope ↓

An AI email template invents tags your list lacks. Fix the allowed tags and fallback syntax, then check them against the CSV and preview rows with blank cells.

An email that goes out as "Hi ," usually did not fail because the AI wrote a bad draft. The template contained a tag that is not a column in your list, or a tag with no fallback met an empty cell. Start with what you get when you ask with no constraints.

The short prompt

text
Write a re-engagement email for yoga studio members as a template with personalization merge tags.

Three runs in English, three in Korean. The top of the first English output:

output
**Subject options**- {{first_name}}, your mat misses you- We saved your spot, {{first_name}}- It's been a while, {{first_name}}. Come back to {{studio_name}} **Preview text:** Your first class back is on us.…

The copy is fine. The tags are the problem. The member CSV in this test (fictional) has exactly three columns: first_name, last_class_date, plan. In all 6 runs the AI used tags that are not columns. The number of distinct tag names missing from the CSV was 16, 21 and 23 in the three English runs, and 16, 16 and 20 in the Korean ones. All 6 mentioned fallbacks in a closing note or table, but none put one on a tag in the email body. Any member with an empty name cell gets a greeting with a hole in it.

(A tag like {{company}} did not show up in these 6 runs. studio_name and offer_name did. Which names get invented changes from run to run.)

Fix the tags and the syntax

The AI is good at wording and at deciding where personalization goes. It does not know your column names, so the prompt lists the only tags allowed and spells out the syntax and fallback rules. This follows the advice in Anthropic's prompting best practices to state the output you want specifically.

text
Write a re-engagement email template for members of a yoga studio who have not attended a class in over a month. One subject line and a body of at most 5 sentences. The columns in the member CSV are exactly these three:{column names} Rules:- Personalize only from those columns. The only tags you may use are {allowed tags with a fallback slot}. Do not invent tags with any other name.- Tag syntax is {{column|fallback}}. Spell the column exactly as above (lowercase, underscore) and put no spaces inside the braces. If the cell is empty, the text after | is used.- Every tag needs a non-empty fallback. Write the sentence so it still reads naturally with the fallback in place.- Information that is not in the CSV (company name, studio name, sender name) must not become a tag; use fixed text such as "The yoga studio team".- Output only the template. No explanation, no heading, no code fence.

In the prompt actually sent, {column names} was first_name, last_class_date, plan and the allowed tags were {{first_name|fallback}}, {{last_class_date|fallback}} and {{plan|fallback}}. Change {{column|fallback}} to whatever your sending tool uses; the script below assumes this syntax.

console
$ claude -p --model claude-sonnet-5-5 --max-turns 1 --tools "" --setting-sources "" < rules.en.txt > out.rules.en.1.txt$ cat out.rules.en.1.txtSubject: {{first_name|Friend}}, your mat misses you Hi {{first_name|there}}, We've missed you on the mat! Your last class with us was {{last_class_date|a little while ago}}, and your {{plan|membership}} is still ready whenever you are. Even one gentle class can help you ease back into your routine. We'd love to see you in the studio again soon. Warmly,The yoga studio team

All 6 runs of this prompt (3 English, 3 Korean) used only the three allowed tags, each with a fallback.

Check against the list and preview the blanks

Check whether the prompt was followed by script. The list has one row each with an empty name, date and plan.

members.en.csv
first_name,last_class_date,planMina,2026-09-28,monthly pass,2026-10-02,10-class packJun,,drop-inSora,2026-08-30,

The script pulls every {{…}} tag and checks it against the CSV header, checks that each fallback is not empty, then builds the actual text for every row and looks for broken spots: leftover braces, a comma after a space, a double space. Any problem gives exit code 1.

check_tags.py
#!/usr/bin/env python3"""usage: check_tags.py members.csv template.txtChecks a {{column|fallback}} template against the CSV header, then prints a preview for every row."""import csv, re, syssys.stdout.reconfigure(encoding="utf-8") header = next(csv.reader(open(sys.argv[1], encoding="utf-8")))text = open(sys.argv[2], encoding="utf-8").read()TAG = re.compile(r"\{\{(.*?)\}\}", re.S)bad = 0 tags = []for m in TAG.finditer(text):    raw = m.group(1)    name, sep, fb = raw.partition("|")    tags.append((m.group(0), name, sep, fb))if not tags:    print("FAIL no tags found"); sys.exit(1) for full, name, sep, fb in tags:    problems = []    if name != name.strip() or fb != fb.strip() and sep:        problems.append("space inside braces")    if name.strip() not in header:        problems.append("not a CSV column")    if not sep or not fb.strip():        problems.append("no fallback")    print(("FAIL " if problems else "ok   ") + full + ("  <- " + ", ".join(problems) if problems else ""))    bad += bool(problems) # other merge syntaxes and braces outside a well-formed {{...}} tag, any letter caserest = TAG.sub("", text)OTHER = r"[{}]|\*\|[A-Za-z_]+\|\*|%[A-Za-z_]+%|\[(?:[A-Za-z]+_[A-Za-z_]+|[A-Z]{2,}|[A-Z][a-z]+[A-Z][A-Za-z]*)\]"for stray in re.findall(OTHER, rest):    print("FAIL stray marker outside {{ }}:", stray); bad += 1 def render(row):    def sub(m):        name, _, fb = m.group(1).partition("|")        val = row.get(name.strip(), "")        return val if val else fb    return TAG.sub(sub, text) print("--- preview")rows = list(csv.DictReader(open(sys.argv[1], encoding="utf-8")))for i, row in enumerate(rows, 1):    out = render(row)    body = out.splitlines()    issues = []    if re.search(r"[{}]", out): issues.append("leftover braces")    if re.search(r"(^|\s)[,.!?]", out, re.M): issues.append("punctuation after space")    if re.search(r"(^|\s)님", out, re.M): issues.append("'님' after space/line start")    if re.search(r"[ ]{2,}\S", out): issues.append("double space")    print(f"row {i} {dict(row)}")    for line in body:        if line.strip(): print("   ", line)    if issues:        print("    FAIL:", ", ".join(issues)); bad += 1print(f"{len(tags)} tags, {len(rows)} rows, {bad} problems")sys.exit(1 if bad else 0)

(The 님 check is for Korean honorific suffixes and does nothing on English text.) Run it on the first output of each prompt.

console
$ python3 -I check_tags.py members.en.csv out.short.en.1.txt | grep -m2 'not a CSV'FAIL {{studio_name}}  <- not a CSV column, no fallbackFAIL {{days_since_last_visit}}  <- not a CSV column, no fallback$ python3 -I check_tags.py members.en.csv out.short.en.1.txt | tail -123 tags, 4 rows, 28 problems$ python3 -I check_tags.py members.en.csv out.rules.en.1.txt | tail -1; echo "exit ${PIPESTATUS[0]}"4 tags, 4 rows, 0 problemsexit 0

All 6 outputs of the fixed prompt exited 0. The empty-name row came out as "Hi there," with the subject "Friend, your mat misses you" (first output).

Testing the checker itself

Ten broken inputs were made by hand from the first fixed-prompt output, one change each (missing column name, fallback removed, single braces, spaces, wrong letter case, a blank fallback, no tags, *|FNAME|*, [First_Name], %FIRST_NAME%). All ten exited 1 (they are not model outputs). One example, with the subject tag changed to {{company|Friend}}:

console
$ python3 -I check_tags.py members.en.csv badEn/company.txt | grep '^FAIL'FAIL {{company|Friend}}  <- not a CSV column

The first version failed all six good outputs (a double-space regex bug) and, looking only for lowercase patterns, missed the last three syntaxes above (both fixed). The other nine are in run/badEn/.

Where it still goes wrong

  • A fallback or value that does not fit the sentence. In the third English output the template says "Your {{plan|current}} plan", so the 10-class row becomes "Your 10-class pack plan is still ready". The script passes it, and it reads a little off. Read the preview for the rows with blanks.
  • Different fallbacks for the same column. In the output above the subject falls back to "Friend" and the greeting to "there". A subject line starting with a capitalized "Friend," reads oddly. Ask for one fallback per column.
  • Date format. The date goes into the sentence exactly as it is in the CSV, 2026-09-28. Format the date in your sending tool or add a display-ready column.
  • Your tool's own syntax. {{column|fallback}} is this post's assumption. Real tools differ in fallback syntax and can be picky about spaces and letter case in column names. GMass's post on failed personalization lists a column name that did not match and stray spaces around the braces. Paste your tool's documented syntax into the prompt.
  • When not to trust it. The script checks the shape of tags and sentences. It does not know whether the values are right (a surname in the first-name column) or whether a member opted in. Before a real send, test-send a row that has blanks.

Related: the same idea applied to ad links, in checking UTM links.

#email-merge#personalization#csv#prompt