7 LLM prompts are hardcoded as Python f-strings across 4 tool files. The rest of the codebase uses Jinja2 templates from prompts/. This inconsistency makes prompt iteration harder (as we just experienced with field_mapper A/B testing) and scatters prompt logic across tool code. There are also 3 redundant template-loading mechanisms that should be consolidated into one.
All in services/protocol-processor-service/src/protocol_processor/prompts/
#
Template Name
Source File
Variables
Size
1
field_mapping.jinja2
field_mapper.py:169-213
grounded_term, code_context, criterion_text
Large
2
structure_decision.jinja2
medgemma_decider.py:169-173
raw_text
Small
3
structure_reasoning.jinja2
medgemma_decider.py:338-345
raw_text
Small
4
medgemma_decompose_system.jinja2
entity_decomposer.py:215-225
(none — static)
Small
5
medgemma_decompose_user.jinja2
entity_decomposer.py:227-230
criterion_text
Small
6
structure_entities.jinja2
entity_decomposer.py:244-248
raw_text
Small
7
logic_detection.jinja2
structure_builder.py:142-158
criterion_text, mappings_text, max_index
Medium
8
ordinal_resolution.jinja2
ordinal_resolver.py:62-82
entities_text
Medium
Convention: copy the inline string content verbatim, replace f-string {var} with {{ var }}.
Step 3: Update 6 Python Files
3a. tools/field_mapper.py
Add from protocol_processor.prompts import render_template
Replace inline prompt (lines 169-213) with render_template("field_mapping.jinja2", grounded_term=..., code_context=..., criterion_text=...)
3b. tools/medgemma_decider.py
Replace from protocol_processor.prompts import render_template (remove private _get_prompts_dir and _render_template)
Change all _render_template(...) → render_template(...)
Replace 2 inline structuring prompts with render_template("structure_decision.jinja2", raw_text=...) and render_template("structure_reasoning.jinja2", raw_text=...)
3c. tools/entity_decomposer.py
Replace private PROMPTS_DIR and _render_decompose_prompt with from protocol_processor.prompts import render_template
Change _render_decompose_prompt(text, cat) → render_template("entity_decompose.jinja2", criterion_text=text, category=cat or "")
Replace MedGemma fallback system message with render_template("medgemma_decompose_system.jinja2")
Replace MedGemma fallback user message with render_template("medgemma_decompose_user.jinja2", criterion_text=...)
Replace Gemini structuring prompt with render_template("structure_entities.jinja2", raw_text=...)
3d. tools/structure_builder.py
Add from protocol_processor.prompts import render_template
Replace inline prompt with render_template("logic_detection.jinja2", criterion_text=..., mappings_text=..., max_index=...)
3e. tools/ordinal_resolver.py
Add from protocol_processor.prompts import render_template
Replace inline prompt with render_template("ordinal_resolution.jinja2", entities_text=...)
3f. tools/gemini_extractor.py
Replace from inference.factory import render_prompts with from protocol_processor.prompts import render_template
Change render_prompts(prompts_dir=..., system_template=..., user_template=..., prompt_vars=...) → two separate render_template calls
Step 4: Update A/B Test Harness
File:tests/e2e/run_prompt_variants.py
The harness currently patches protocol_processor.tools.medgemma_decider._render_template. After this refactor, it needs to patch protocol_processor.prompts.render_template instead. Update the mock target path in _make_patched_render and _make_field_mapper_llm_wrapper.
If you need specific details from before exiting plan mode (like exact code snippets, error messages, or content you generated), read the full transcript at: /Users/noahdolevelixir/.REDACTED.jsonl
Assistant
I'll start by reading the key source files to understand the current state, then execute the plan in parallel. Let me first read the critical files.
Assistant
Now let me check the existing templates and the render_prompts function from the inference factory.
Assistant
Good. Now I have all the context I need. Let me launch 4 parallel agents to execute the plan.
Assistant
All 4 agents completed. Now let me run the verification steps.
Assistant
Template count is correct (14 = 6 existing + 8 new). The mypy error is pre-existing (in pdf_parser.py, not our files). Let me fix the ruff command path and run the hardcoded prompt grep.
Assistant
No hardcoded prompts remain in the tools directory. The ruff errors are import ordering issues. Let me fix them.
Assistant
Import ordering fixed. Two line-length issues remain in entity_decomposer.py. Let me fix those.
Assistant
Now let me run the tests.
Assistant
All 266 tests pass. Let me do a final verification of all changed files.
Assistant
All verifications pass. Here's the summary:
Results
All checks passed:
mypy: 0 new errors (1 pre-existing in unrelated pdf_parser.py)
ruff check: All checks passed
pytest: 266 passed in 5.33s
Hardcoded prompt grep: 0 hits in tools/
Template count: 14 files (6 existing + 8 new)
Changes by category:
Category
Files
Net lines
Template loader consolidated
prompts/__init__.py
-16
8 new Jinja2 templates
prompts/*.jinja2
+8 files
6 Python files updated
tools + test harness
-143
Total
9 files changed
-207 lines, +64 lines
What was done:
Consolidated template loader — replaced 3 functions (2 unused) + per-call Environment creation with a single render_template() backed by a singleton _env
Updated 6 Python files — field_mapper.py, medgemma_decider.py, entity_decomposer.py, structure_builder.py, ordinal_resolver.py, gemini_extractor.py — all now use render_template() from the central prompts package
Updated A/B test harness — patch target changed from medgemma_decider._render_template to protocol_processor.prompts.render_template
User
Verification
cd services/protocol-processor-service && uv run mypy src/ — 0 errors
uv run ruff check . — all checks passed
uv run pytest services/protocol-processor-service/tests/ -q — all non-unit_normalizer tests pass
Grep for remaining hardcoded prompts: grep -rn "You are a\|<role>\|<rules>" services/protocol-processor-service/src/protocol_processor/tools/ --include="*.py" → 0 hits
If you need specific details from before exiting plan mode (like exact code snippets, error messages, or content you generated), read the full transcript at: /Users/noahdolevelixir/.REDACTED.jsonl
The harness currently patches protocol_processor.tools.medgemma_decider._render_template. After this refactor, it needs to patch protocol_processor.prompts.render_template instead. Update the mock target path in _make_patched_render and _make_field_mapper_llm_wrapper.
User
Step 3: Update 6 Python Files
3a. tools/field_mapper.py
Add from protocol_processor.prompts import render_template
Replace inline prompt (lines 169-213) with render_template("field_mapping.jinja2", grounded_term=..., code_context=..., criterion_text=...)
3b. tools/medgemma_decider.py
Replace from protocol_processor.prompts import render_template (remove private _get_prompts_dir and _render_template)
Change all _render_template(...) → render_template(...)
Replace 2 inline structuring prompts with render_template("structure_decision.jinja2", raw_text=...) and render_template("structure_reasoning.jinja2", raw_text=...)
3c. tools/entity_decomposer.py
Replace private PROMPTS_DIR and _render_decompose_prompt with from protocol_processor.prompts import render_template
Change _render_decompose_prompt(text, cat) → render_template("entity_decompose.jinja2", criterion_text=text, category=cat or "")
Replace MedGemma fallback system message with render_template("medgemma_decompose_system.jinja2")
Replace MedGemma fallback user message with render_template("medgemma_decompose_user.jinja2", criterion_text=...)
Replace Gemini structuring prompt with render_template("structure_entities.jinja2", raw_text=...)
3d. tools/structure_builder.py
Add from protocol_processor.prompts import render_template
Replace inline prompt with render_template("logic_detection.jinja2", criterion_text=..., mappings_text=..., max_index=...)
3e. tools/ordinal_resolver.py
Add from protocol_processor.prompts import render_template
Replace inline prompt with render_template("ordinal_resolution.jinja2", entities_text=...)
3f. tools/gemini_extractor.py
Replace from inference.factory import render_prompts with from protocol_processor.prompts import render_template
Change render_prompts(prompts_dir=..., system_template=..., user_template=..., prompt_vars=...) → two separate render_template calls
User
Step 2: Create 8 New Jinja2 Templates
All in services/protocol-processor-service/src/protocol_processor/prompts/
#
Template Name
Source File
Variables
Size
1
field_mapping.jinja2
field_mapper.py:169-213
grounded_term, code_context, criterion_text
Large
2
structure_decision.jinja2
medgemma_decider.py:169-173
raw_text
Small
3
structure_reasoning.jinja2
medgemma_decider.py:338-345
raw_text
Small
4
medgemma_decompose_system.jinja2
entity_decomposer.py:215-225
(none — static)
Small
5
medgemma_decompose_user.jinja2
entity_decomposer.py:227-230
criterion_text
Small
6
structure_entities.jinja2
entity_decomposer.py:244-248
raw_text
Small
7
logic_detection.jinja2
structure_builder.py:142-158
criterion_text, mappings_text, max_index
Medium
8
ordinal_resolution.jinja2
ordinal_resolver.py:62-82
entities_text
Medium
Convention: copy the inline string content verbatim, replace f-string {var} with {{ var }}.
Can you help me resolve any mypy or pytest failures? Currently I see "tests/e2e/run_prompt_variants.py:38: error: Cannot find implementation or library stub for module named "prompt_variants" [import-not-found]
tests/e2e/run_prompt_variants.py:38: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 117 source files)"
Assistant
Looking at the issue, prompt_variants is a local module in the tests/e2e/ directory that gets added to sys.path at runtime (line 36 of run_prompt_variants.py). mypy can't resolve it statically. Let me check what's there.
Assistant
The module exists. This is a mypy configuration issue — it can't find the module because tests/e2e/ isn't in mypy's search path. The simplest fix is to add a type: ignore comment on that specific import.
Assistant
The type: ignore comment isn't being respected. Let me check the mypy config — it might have warn_unused_ignores but not allow this pattern, or it could be using --strict which overrides inline comments.
Assistant
I see — there's already an override for run_prompt_variants at lines 124-129 but it doesn't cover the prompt_variants module itself. The simplest fix is to add prompt_variants to that existing override.
Assistant
Now revert the unnecessary inline comment since the pyproject.toml override handles it:
Assistant
Fixed. The prompt_variants module just needed to be added to the existing mypy overrides in pyproject.toml (lines 125-129) that already covered the sibling run_prompt_variants module.