Arize-ai/phoenix
formatTemplate() HTML-escapes variables, corrupting LLM prompt content
Summary
Context: The
formatTemplate()function injs/packages/phoenix-evals/src/template/applyTemplate.tsis used to populate LLM prompt templates with variable content for evaluation tasks such as hallucination detection and document relevancy scoring.Bug: The function uses
Mustache.render()which HTML-escapes all variables by default, converting characters like<,>,&, and"to HTML entities (<,>,&,").Actual vs. expected: LLM prompts containing code snippets or comparison operators are corrupted with HTML entities instead of preserving the original characters as plain text.
Impact: Code snippets, technical documentation, and mathematical expressions in prompts are silently corrupted, leading to incorrect LLM evaluations without any error messages.
Code with bug
Example affected template (HALLUCINATION_TEMPLATE.ts):
Mustache.js Default Behavior
The Mustache.js library documentation explicitly states: “All variables are HTML-escaped by default.”
The library’s source code implements escaping for these characters:
Example corruption:
Failing Test
Test output: Both tests fail. The function returns HTML-escaped strings instead of preserving the original characters.
Recommended fix
Disable HTML escaping by passing a custom escape function that returns text unchanged, since LLM prompts are plain text, not HTML:
This approach preserves all special characters while maintaining compatibility with existing templates.