test_deepseek_chat_templates.py 12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Unit tests for DeepSeek chat template tool call handling.

Tests verify that the DeepSeek chat templates (v3, v3.1, v3.2) correctly handle
both dict and string types for tool['function']['arguments'] without double-escaping,
addressing issue #11700.
"""

import json
import os
import unittest

from jinja2 import Template


class TestDeepSeekChatTemplateToolCalls(unittest.TestCase):
    """Test DeepSeek chat templates handle tool calls correctly."""

    @classmethod
    def setUpClass(cls):
        """Load all DeepSeek chat templates."""
        base_path = os.path.join(
            os.path.dirname(__file__), "..", "..", "examples", "chat_template"
        )

        cls.templates = {}
        template_files = {
            "v3": "tool_chat_template_deepseekv3.jinja",
            "v3.1": "tool_chat_template_deepseekv31.jinja",
            "v3.2": "tool_chat_template_deepseekv32.jinja",
        }

        for version, filename in template_files.items():
            template_path = os.path.join(base_path, filename)
            with open(template_path, "r") as f:
                template_content = f.read()
            cls.templates[version] = Template(template_content)

    def _render_template(
        self, version, messages, tools=None, add_generation_prompt=True
    ):
        """Helper method to render a template with given messages and tools."""
        template = self.templates[version]

        # Common template variables
        context = {
            "messages": messages,
            "add_generation_prompt": add_generation_prompt,
            "bos_token": "<|begin▁of▁sentence|>",
        }

        if tools is not None:
            context["tools"] = tools

        return template.render(**context)

    def test_tool_arguments_as_dict(self):
        """Test that tool arguments as dict are properly JSON-encoded (normal case)."""
        # This tests the normal case where arguments come from OpenAI API as dict

        for version in ["v3", "v3.1", "v3.2"]:
            with self.subTest(version=version):
                messages = [
                    {"role": "user", "content": "What's the weather in NYC?"},
                    {
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [
                            {
                                "type": "function",
                                "function": {
                                    "name": "get_weather",
                                    "arguments": {
                                        "city": "New York",
                                        "unit": "celsius",
                                    },  # Dict
                                },
                            }
                        ],
                    },
                ]

                tools = [
                    {
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "description": "Get weather information",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "city": {"type": "string"},
                                    "unit": {"type": "string"},
                                },
                            },
                        },
                    }
                ]

                output = self._render_template(version, messages, tools)

                # Should contain properly formatted JSON (not double-escaped)
                self.assertIn('"city"', output, f"{version}: Should contain city key")
                self.assertIn(
                    '"New York"', output, f"{version}: Should contain city value"
                )

                # Should NOT contain double-escaped quotes
                self.assertNotIn(
                    '\\"city\\"', output, f"{version}: Should not double-escape"
                )
                self.assertNotIn(
                    '\\\\"', output, f"{version}: Should not have escaped backslashes"
                )

    def test_tool_arguments_as_string(self):
        """Test that tool arguments as string are used as-is (multi-round case)."""
        # This tests the multi-round function calling case from issue #11700
        # where arguments might already be JSON strings from previous model output

        for version in ["v3", "v3.1", "v3.2"]:
            with self.subTest(version=version):
                messages = [
                    {"role": "user", "content": "What's the stock price of NVDA?"},
                    {
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [
                            {
                                "type": "function",
                                "function": {
                                    "name": "get_stock_info",
                                    "arguments": '{"symbol": "NVDA"}',  # Already a JSON string
                                },
                            }
                        ],
                    },
                ]

                tools = [
                    {
                        "type": "function",
                        "function": {
                            "name": "get_stock_info",
                            "description": "Get stock information",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "symbol": {"type": "string"},
                                },
                            },
                        },
                    }
                ]

                output = self._render_template(version, messages, tools)

                # Should contain the JSON string as-is
                self.assertIn(
                    '{"symbol": "NVDA"}',
                    output,
                    f"{version}: Should contain JSON as-is",
                )

                # Should NOT double-escape (the bug from issue #11700)
                # Bad output would look like: "{\"symbol\": \"NVDA\"}" or "{\\"symbol\\": \\"NVDA\\"}"
                self.assertNotIn(
                    '{\\"symbol\\"', output, f"{version}: Should not double-escape"
                )
                self.assertNotIn(
                    '"{\\"symbol', output, f"{version}: Should not wrap and escape"
                )

                # Verify it's not triple-quoted or escaped
                self.assertNotIn(
                    '""{"', output, f"{version}: Should not have extra quotes"
                )

    def test_multiple_tool_calls_mixed_types(self):
        """Test multiple tool calls with mixed dict and string argument types."""
        # This tests a complex scenario with multiple tools, some with dict args, some with string

        for version in ["v3", "v3.1", "v3.2"]:
            with self.subTest(version=version):
                messages = [
                    {"role": "user", "content": "Get weather and stock info"},
                    {
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [
                            {
                                "type": "function",
                                "function": {
                                    "name": "get_weather",
                                    "arguments": {"city": "Boston"},  # Dict
                                },
                            },
                            {
                                "type": "function",
                                "function": {
                                    "name": "get_stock_info",
                                    "arguments": '{"symbol": "TSLA"}',  # String
                                },
                            },
                        ],
                    },
                ]

                tools = [
                    {
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "description": "Get weather",
                            "parameters": {
                                "type": "object",
                                "properties": {"city": {"type": "string"}},
                            },
                        },
                    },
                    {
                        "type": "function",
                        "function": {
                            "name": "get_stock_info",
                            "description": "Get stock info",
                            "parameters": {
                                "type": "object",
                                "properties": {"symbol": {"type": "string"}},
                            },
                        },
                    },
                ]

                output = self._render_template(version, messages, tools)

                # First tool (dict) should be properly JSON-encoded
                self.assertIn(
                    '"city"', output, f"{version}: First tool should have city key"
                )
                self.assertIn(
                    '"Boston"',
                    output,
                    f"{version}: First tool should have Boston value",
                )

                # Second tool (string) should be used as-is
                self.assertIn(
                    '{"symbol": "TSLA"}',
                    output,
                    f"{version}: Second tool should use string as-is",
                )

                # Neither should be double-escaped
                self.assertNotIn(
                    '\\"city\\"',
                    output,
                    f"{version}: First tool should not double-escape",
                )
                self.assertNotIn(
                    '\\"symbol\\"',
                    output,
                    f"{version}: Second tool should not double-escape",
                )

    def test_tool_call_with_content(self):
        """Test tool calls that also include content text."""
        # Some models include explanatory text along with tool calls

        for version in ["v3", "v3.1", "v3.2"]:
            with self.subTest(version=version):
                messages = [
                    {"role": "user", "content": "What's the weather?"},
                    {
                        "role": "assistant",
                        "content": "Let me check the weather for you.",
                        "tool_calls": [
                            {
                                "type": "function",
                                "function": {
                                    "name": "get_weather",
                                    "arguments": {"city": "Seattle"},
                                },
                            }
                        ],
                    },
                ]

                tools = [
                    {
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "description": "Get weather",
                            "parameters": {
                                "type": "object",
                                "properties": {"city": {"type": "string"}},
                            },
                        },
                    }
                ]

                output = self._render_template(version, messages, tools)

                # Should contain both the content and the tool call
                self.assertIn(
                    "Let me check the weather",
                    output,
                    f"{version}: Should include content",
                )
                self.assertIn(
                    '"city"', output, f"{version}: Should include tool arguments"
                )
                self.assertNotIn(
                    '\\"city\\"', output, f"{version}: Should not double-escape"
                )


if __name__ == "__main__":
    unittest.main()