33import json
44from pathlib import Path
55
6+ import pytest
7+
68from complexity .inference .chat_template import (
79 CHAT_TEMPLATE_ID ,
810 LEGACY_CHAT_TEMPLATE_ID ,
911 THINK_FINAL_ENVELOPE ,
1012 default_chat_template ,
1113 huggingface_chat_template ,
14+ load_chat_template_jinja ,
1215 render_assistant_envelope ,
1316 render_inference_prompt ,
17+ render_jinja_inference_prompt ,
18+ render_jinja_messages ,
1419 render_messages_before_assistant ,
1520 validate_chat_template ,
1621)
@@ -144,6 +149,43 @@ def test_huggingface_template_matches_explicit_system_message() -> None:
144149 assert rendered == render_messages_before_assistant (messages , contract )
145150
146151
152+ def test_standalone_jinja_renderer_matches_hf_and_vllm_contract (tmp_path ) -> None :
153+ contract = default_chat_template ()
154+ source = huggingface_chat_template (contract )
155+ (tmp_path / "chat_template.jinja" ).write_text (source , encoding = "utf-8" )
156+
157+ rendered = render_jinja_messages (
158+ [
159+ {"role" : "user" , "content" : "First" },
160+ {"role" : "assistant" , "content" : "Answer" },
161+ {"role" : "user" , "content" : "Follow-up" },
162+ ],
163+ load_chat_template_jinja (tmp_path ),
164+ eos_token = "</s>" ,
165+ add_generation_prompt = True ,
166+ )
167+
168+ assert rendered == (
169+ "User:\n First\n \n Assistant:\n Answer</s>"
170+ "User:\n Follow-up\n \n Assistant:\n "
171+ )
172+
173+
174+ def test_standalone_jinja_renderer_builds_mlx_generation_prompt () -> None :
175+ rendered = render_jinja_inference_prompt (
176+ "Hello" ,
177+ huggingface_chat_template (default_chat_template ()),
178+ eos_token = "</s>" ,
179+ )
180+
181+ assert rendered == "User:\n Hello\n \n Assistant:\n "
182+
183+
184+ def test_standalone_jinja_loader_rejects_incomplete_mlx_bundle (tmp_path ) -> None :
185+ with pytest .raises (FileNotFoundError , match = "standalone Jinja" ):
186+ load_chat_template_jinja (tmp_path )
187+
188+
147189def test_vllm_config_declares_exported_template () -> None :
148190 template = default_chat_template ()
149191 config = build_config (
@@ -158,7 +200,7 @@ def test_vllm_config_declares_exported_template() -> None:
158200 template ,
159201 )
160202 assert config ["chat_template_id" ] == CHAT_TEMPLATE_ID
161- assert config ["chat_template_file" ] == "chat_template.json "
203+ assert config ["chat_template_file" ] == "chat_template.jinja "
162204
163205
164206def test_vllm_export_preserves_legacy_modulo_cyclic_routing () -> None :
@@ -304,6 +346,9 @@ def test_mlx_export_preserves_checkpoint_chat_template(tmp_path) -> None:
304346
305347 assert written == template
306348 assert (tmp_path / "chat_template.json" ).read_text ().endswith ("\n " )
349+ assert (tmp_path / "chat_template.jinja" ).read_text (encoding = "utf-8" ) == (
350+ huggingface_chat_template (template ) + "\n "
351+ )
307352 assert (
308353 __import__ ("json" ).loads (
309354 (tmp_path / "chat_template.json" ).read_text ()
0 commit comments