Skip to content

refactor: migrate prompt .replace() chains to substitute_placeholders#89

Merged
lipikaramaswamy merged 2 commits into
mainfrom
andreatgretel/chore/migrate-prompt-placeholders
Apr 6, 2026
Merged

refactor: migrate prompt .replace() chains to substitute_placeholders#89
lipikaramaswamy merged 2 commits into
mainfrom
andreatgretel/chore/migrate-prompt-placeholders

Conversation

@andreatgretel
Copy link
Copy Markdown
Collaborator

Summary

  • Extracts render_template from rewrite/parsers.py into a shared engine/prompt_utils.py module, renamed to substitute_placeholders
  • Uses single-pass regex substitution to prevent cross-contamination when user-controlled text contains placeholder tokens (e.g. <<SCORE>> appearing in a replacement value won't be double-substituted)
  • Adds strict mode (default) that raises ValueError on malformed keys and unresolved placeholders, and warns on unused keys
  • Migrates all 12 prompt-building functions across detection/, replace/, and rewrite/ modules

Closes #66

Changes

File Summary
engine/prompt_utils.py (new) substitute_placeholders() with strict/non-strict modes
tests/engine/test_prompt_utils.py (new) 11 tests: basic substitution, cross-contamination, strict mode, non-strict mode
detection/detection_workflow.py 3 prompt functions migrated
replace/llm_replace_workflow.py 1 prompt function migrated
rewrite/domain_classification.py .replace() chain -> substitute_placeholders()
rewrite/evaluate.py import swap
rewrite/final_judge.py import swap
rewrite/parsers.py Removed render_template and unused import re
rewrite/qa_generation.py 2 prompt functions migrated
rewrite/repair.py import swap
rewrite/rewrite_generation.py 1 prompt function migrated
rewrite/sensitivity_disposition.py .replace() chain -> substitute_placeholders()
tests/engine/test_parsers.py Removed render_template tests (moved to test_prompt_utils)

Test plan

  • All 500 existing tests pass
  • 11 new tests for substitute_placeholders covering basic substitution, cross-contamination prevention, strict mode validation, and non-strict fallback
  • ruff format and ruff check pass

@andreatgretel andreatgretel requested a review from a team as a code owner April 2, 2026 15:46
Comment thread tests/engine/test_prompt_utils.py
Comment thread src/anonymizer/engine/prompt_utils.py
Copy link
Copy Markdown
Collaborator

@lipikaramaswamy lipikaramaswamy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good, workflow ran e2e, just a few nits.

@andreatgretel andreatgretel force-pushed the andreatgretel/chore/migrate-prompt-placeholders branch from 3a6b451 to 4b2f5da Compare April 3, 2026 19:35
…placeholders

Extract render_template from rewrite/parsers.py into a shared
engine/prompt_utils.py module, renamed to substitute_placeholders.
The new function uses a single-pass regex to prevent cross-contamination
when user-controlled text contains placeholder tokens.

Strict mode (default) raises on malformed keys and unresolved
placeholders, and warns on unused keys.

Migrated all 12 prompt-building functions across detection, replace,
and rewrite modules.

Closes #66
Add inline comment documenting that _PLACEHOLDER_RE only matches
[a-zA-Z0-9_] (no hyphens). Add two tests for repeated placeholders
and empty replacement values.
@andreatgretel andreatgretel force-pushed the andreatgretel/chore/migrate-prompt-placeholders branch from 4b2f5da to b77af49 Compare April 3, 2026 19:38
@lipikaramaswamy lipikaramaswamy merged commit 1a903e2 into main Apr 6, 2026
9 checks passed
@lipikaramaswamy lipikaramaswamy deleted the andreatgretel/chore/migrate-prompt-placeholders branch April 6, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: migrate prompt .replace() chains to render_template()

2 participants