Commit graph

1 commit

Author SHA1 Message Date
Daniel Han
44f113cf0d
Escape the system message spliced into predefined chat templates (#7746)
* Escape the system message spliced into predefined chat templates

get_chat_template(..., system_message = ...) substitutes the message into a
{system_message} placeholder that sits inside a Jinja string literal in all 15
predefined templates that carry one, so a quote closes the literal and a
backslash is read as an escape:

  vicuna  "Answer the user's question."  -> TemplateSyntaxError
  vicuna  r"Put it in \boxed{}."         -> renders '\x08oxed{}'
  vicuna  r"C:\Users\me"                 -> TemplateSyntaxError

Reuse the escaper PR #7731 added for construct_chat_template, promoted to a
module-level _escape_jinja_literal and extended to escape double quotes so the
one helper covers llama-3.1's "..." literal as well as the '...' the rest use.
Apply it to the predefined branch of _change_system_message and to the ShareGPT
mapping values, and drop the hand-escaping from the two vicuna defaults, which
would otherwise be escaped twice.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Tighten the escaping comments

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-02 07:18:09 -07:00