Skip to content

Commit ebd035e

Browse files
committed
[evals] Fix trace chat template rendering
1 parent c248985 commit ebd035e

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

lib/levanter/src/levanter/data/text/trace_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def metadata(self) -> dict[str, Any]:
422422
"include_final_assistant_tag": self.include_final_assistant_tag,
423423
"parse_text_tool_calls": self.parse_text_tool_calls,
424424
"label_spec": {
425-
"id_to_name": self.label_spec.id_to_name,
425+
"id_to_name": {str(label_id): name for label_id, name in self.label_spec.id_to_name.items()},
426426
"aggregates": {name: list(label_ids) for name, label_ids in self.label_spec.aggregates.items()},
427427
},
428428
}

lib/levanter/src/levanter/tokenizers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,18 @@ def _passthrough(caller: jinja2.runtime.Macro) -> str:
195195

196196
def _make_jinja_env(extensions: list[type]) -> jinja2.Environment:
197197
"""Create a jinja2 environment matching HF's template rendering settings."""
198-
return jinja2.Environment(
198+
env = jinja2.Environment(
199199
undefined=jinja2.StrictUndefined,
200200
trim_blocks=True,
201201
lstrip_blocks=True,
202202
extensions=extensions,
203203
)
204+
env.globals["raise_exception"] = _raise_chat_template_exception
205+
return env
206+
207+
208+
def _raise_chat_template_exception(message: str) -> None:
209+
raise jinja2.exceptions.TemplateError(message)
204210

205211

206212
def _chat_template_block_contents(block: str) -> str:

lib/levanter/tests/test_tokenizers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,28 @@ def test_apply_chat_template_with_masks_batch(backend_tokenizer):
11031103
assert len(result["assistant_masks"]) == 2
11041104

11051105

1106+
@requires_model
1107+
def test_apply_chat_template_with_masks_supports_raise_exception(backend_tokenizer):
1108+
template = """\
1109+
{%- for message in messages -%}
1110+
{%- if message.role == 'tool' -%}
1111+
{{- raise_exception('tool messages are not supported') -}}
1112+
{%- endif -%}
1113+
{{ message.content }}
1114+
{%- endfor -%}
1115+
"""
1116+
backend_tokenizer.apply_chat_template_with_masks(
1117+
[[{"role": "user", "content": "hello"}]],
1118+
chat_template=template,
1119+
)
1120+
1121+
with pytest.raises(Exception, match="tool messages are not supported"):
1122+
backend_tokenizer.apply_chat_template_with_masks(
1123+
[[{"role": "tool", "content": "result"}]],
1124+
chat_template=template,
1125+
)
1126+
1127+
11061128
# ---------------------------------------------------------------------------
11071129
# 10. Code samples and real-world text
11081130
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)