dynamo-renderer 10.4.5

Standalone OpenAI chat-template / prompt formatting (HF chat_template via minijinja).
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//based on: https://github.com/EricLBuehler/mistral.rs/blob/d970bb5feb863acf8e8ec90de97e18221fb959f1/mistralrs-core/src/pipeline/chat_template.rs

use std::collections::HashMap;

use chrono::{DateTime, Local};
use either::Either;
use std::borrow::Cow;

use minijinja::value::{Kwargs, ObjectRepr, StringInput, ValueKind, from_args};
use minijinja::{Error, ErrorKind, Output, State, Value, escape_formatter};
use serde::ser::Error as _;
use serde::{Deserialize, Serialize};

use crate::python::{
    PyJsonFormatter, is_python_space, python_float_repr, python_split_whitespace, python_splitlines,
};

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct AddedTokensDecoder {
    __type: Option<String>,
    pub content: String,
    lstrip: bool,
    normalized: bool,
    rstrip: bool,
    single_word: bool,
    special: Option<bool>,
}

pub fn raise_exception(msg: String) -> Result<String, minijinja::Error> {
    Err(minijinja::Error::new(ErrorKind::InvalidOperation, msg))
}

#[derive(Debug, Deserialize)]
pub struct BeginEndUnkTok(
    #[serde(with = "either::serde_untagged")] pub Either<String, AddedTokensDecoder>,
);

/// Support older tool use patterns where the tool use template was separate from the default/chat template.
/// Modern patterns use a single template with a `tool_use` key, e.g.
///
/// ```jinja
/// {%- if tools is not none and tool_choice is not none %}
/// ```
#[derive(Debug, Deserialize)]
pub struct ChatTemplateValue(
    #[serde(with = "either::serde_untagged")] pub Either<String, Vec<HashMap<String, String>>>,
);

/// If present, pad_token is usually a single value. Deepseek R1 and it's distill's use a map.
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct PadTokenValue(
    #[serde(with = "either::serde_untagged")] pub Either<String, AddedTokensDecoder>,
);

#[allow(dead_code)]
#[derive(Debug, Deserialize, Default)]
/// Template for chat models including bos/eos/unk as well as the chat template.
pub struct ChatTemplate {
    pub bos_token: Option<BeginEndUnkTok>,
    pub eos_token: Option<BeginEndUnkTok>,
    pub unk_token: Option<BeginEndUnkTok>,

    /// Jinja format [chat templating] for chat completion.
    ///
    /// [chat templating]: https://huggingface.co/docs/transformers/chat_templating
    pub chat_template: Option<ChatTemplateValue>,

    // future
    add_bos_token: Option<bool>,
    add_eos_token: Option<bool>,
    added_tokens_decoder: Option<HashMap<String, AddedTokensDecoder>>,
    additional_special_tokens: Option<Vec<String>>,
    clean_up_tokenization_spaces: Option<bool>,
    device_map: Option<String>,
    legacy: Option<bool>,
    model_max_length: Option<f64>,
    pad_token: Option<PadTokenValue>,
    sp_model_kwargs: Option<HashMap<String, String>>,
    spaces_between_special_tokens: Option<bool>,
    tokenizer_class: Option<String>,
    truncation_size: Option<String>,
    use_default_system_prompt: Option<bool>,

    /// The `added_tokens` of the model's `tokenizer.json`; see
    /// [`ChatTemplate::with_tokenizer_json`].
    #[serde(skip)]
    tokenizer_added_tokens: Vec<AddedTokensDecoder>,

    /// See [`ChatTemplate::with_request_control_tokens_as_text`]; `None` is the
    /// default, on.
    #[serde(skip)]
    pub(crate) request_control_tokens_as_text: Option<bool>,
}

impl ChatTemplate {
    pub fn eos_tok(&self) -> Option<String> {
        match self.eos_token.as_ref()?.0 {
            Either::Left(ref lit) => Some(lit.clone()),
            Either::Right(ref added) => Some(added.content.clone()),
        }
    }

    pub fn bos_tok(&self) -> Option<String> {
        match self.bos_token.as_ref()?.0 {
            Either::Left(ref lit) => Some(lit.clone()),
            Either::Right(ref added) => Some(added.content.clone()),
        }
    }

    pub fn unk_tok(&self) -> Option<String> {
        match self.unk_token.as_ref()?.0 {
            Either::Left(ref lit) => Some(lit.clone()),
            Either::Right(ref added) => Some(added.content.clone()),
        }
    }

    /// Add the `added_tokens` of the model's `tokenizer.json` to
    /// [`ChatTemplate::added_token_spellings`]. Some repos declare no
    /// `added_tokens_decoder` in `tokenizer_config.json` (DeepSeek V3/R1,
    /// GLM-5, Gemma 4), so their chat markers are listed only there.
    pub fn with_tokenizer_json(mut self, tokenizer_json: &str) -> anyhow::Result<Self> {
        #[derive(Deserialize)]
        struct TokenizerJson {
            #[serde(default)]
            added_tokens: Vec<AddedTokensDecoder>,
        }
        self.tokenizer_added_tokens =
            serde_json::from_str::<TokenizerJson>(tokenizer_json)?.added_tokens;
        Ok(self)
    }

    /// Whether control-token spellings in request text render as ordinary
    /// text, so that only the template's own markers encode as control tokens.
    /// On by default; `false` turns it off, except under a template with
    /// Harmony framing, which always renders this way.
    ///
    /// The spellings come back as ordinary segments of
    /// [`OAIPromptFormatter::render_prompt`](crate::OAIPromptFormatter::render_prompt),
    /// to encode with [`crate::RenderedPrompt::encode_segments`]; `render`
    /// returns the same text without them. A model that lists its markers
    /// only in `tokenizer.json` also needs [`ChatTemplate::with_tokenizer_json`].
    /// In assistant history, a marker the template itself reads stays a
    /// control token.
    pub fn with_request_control_tokens_as_text(mut self, enabled: bool) -> Self {
        self.request_control_tokens_as_text = Some(enabled);
        self
    }

    /// Spellings the tokenizer turns into one added token, each with whether it
    /// is `special`: every `added_tokens_decoder` entry, the `tokenizer.json`
    /// `added_tokens` when given, the `additional_special_tokens`, and the
    /// bos/eos/unk literals. Whitespace-only spellings are dropped; a spelling
    /// listed twice is special if either entry says so. Sorted for a stable
    /// result.
    pub(crate) fn added_token_spellings(&self) -> Vec<(String, bool)> {
        fn insert(spellings: &mut HashMap<String, bool>, spelling: Option<String>, special: bool) {
            if let Some(spelling) = spelling
                && !spelling.trim().is_empty()
            {
                *spellings.entry(spelling).or_insert(false) |= special;
            }
        }
        let mut spellings = HashMap::new();
        for token in self
            .added_tokens_decoder
            .iter()
            .flat_map(HashMap::values)
            .chain(&self.tokenizer_added_tokens)
        {
            insert(
                &mut spellings,
                Some(token.content.clone()),
                token.special.unwrap_or(false),
            );
        }
        for token in self.additional_special_tokens.iter().flatten() {
            insert(&mut spellings, Some(token.clone()), true);
        }
        insert(&mut spellings, self.bos_tok(), true);
        insert(&mut spellings, self.eos_tok(), true);
        insert(&mut spellings, self.unk_tok(), true);
        let mut spellings: Vec<_> = spellings.into_iter().collect();
        spellings.sort();
        spellings
    }
}

#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct GenerationConfig {
    #[serde(with = "either::serde_untagged")]
    bos_token_id: Either<u32, Vec<u32>>,
    #[serde(with = "either::serde_untagged")]
    eos_token_id: Either<u32, Vec<u32>>,
}

/// Python `str` of a non-integer float (`1e-06`, `1e+16`, `nan`); minijinja never uses
/// an exponent (`0.000001`, `10000000000000000.0`) and prints `NaN`. `None` for every
/// other value.
fn python_float_str(value: &Value) -> Option<String> {
    if !value.is_number() || value.is_integer() {
        return None;
    }
    f64::try_from(value.clone()).ok().map(python_float_repr)
}

/// Output formatter for `{{ x }}`. HF renders through Python, which prints a float
/// with `str`; see [`python_float_str`]. Other values are unchanged. `~` concatenation
/// is a VM op that can't be overridden, and filters that stringify values themselves
/// (`join`, `replace`, ...) use minijinja's spelling, so `'x' ~ 1e-6` and
/// `[1e-6] | join` still render `0.000001`.
pub fn python_formatter(out: &mut Output, state: &State, value: &Value) -> Result<(), Error> {
    match python_float_str(value) {
        Some(text) => out.write_str(&text).map_err(Error::from),
        None => escape_formatter(out, state, value),
    }
}

/// The `string` filter with Python `str` floats, matching [`python_formatter`];
/// other values go to the builtin.
pub fn python_string(state: &State, value: &Value) -> Result<Value, Error> {
    match python_float_str(value) {
        Some(text) => Ok(Value::from(text)),
        None => minijinja::filters::string(state, value),
    }
}

/// minijinja-contrib's pycompat string methods, with Python's whitespace where
/// Rust's differs: `strip`, `lstrip`, `rstrip` and `split()` also treat U+001C..U+001F
/// as whitespace, `isspace` is false for `""`, and `splitlines` ends lines at every
/// Python line break. HF renders templates through Python, so `m.content.strip()` must
/// strip what Python strips.
pub fn python_methods(
    state: &State,
    value: &Value,
    method: &str,
    args: &[Value],
) -> Result<Value, Error> {
    if let Some(s) = value.as_str() {
        match method {
            "strip" | "lstrip" | "rstrip" => {
                let (chars,): (Option<&str>,) = from_args(args)?;
                if chars.is_none() {
                    let trimmed = match method {
                        "strip" => s.trim_matches(is_python_space),
                        "lstrip" => s.trim_start_matches(is_python_space),
                        _ => s.trim_end_matches(is_python_space),
                    };
                    return Ok(StringInput::new(state, value)?.preserve_safety(trimmed.to_owned()));
                }
            }
            "split" => {
                let (sep, maxsplit): (Option<&str>, Option<i64>) = from_args(args)?;
                if sep.is_none() {
                    let maxsplit = maxsplit.and_then(|n| usize::try_from(n).ok());
                    return Ok(python_split_whitespace(s, maxsplit)
                        .into_iter()
                        .map(|part| {
                            if value.is_safe() {
                                Value::from_safe_string(part.to_owned())
                            } else {
                                Value::from(part)
                            }
                        })
                        .collect());
                }
            }
            "splitlines" => {
                let (keepends,): (Option<bool>,) = from_args(args)?;
                return Ok(python_splitlines(s, keepends.unwrap_or(false))
                    .into_iter()
                    .map(|part| {
                        if value.is_safe() {
                            Value::from_safe_string(part.to_owned())
                        } else {
                            Value::from(part)
                        }
                    })
                    .collect());
            }
            "isspace" => {
                let () = from_args(args)?;
                return Ok(Value::from(!s.is_empty() && s.chars().all(is_python_space)));
            }
            _ => {}
        }
    }
    minijinja_contrib::pycompat::unknown_method_callback(state, value, method, args)
}

/// Jinja2's `trim` filter, `str.strip(chars)`, with Python's whitespace by default;
/// see [`python_methods`].
pub fn python_trim(value: StringInput<'_>, chars: Option<Cow<'_, str>>) -> Value {
    match chars {
        Some(chars) => minijinja::filters::trim(value, Some(chars)),
        None => {
            let trimmed = value.as_str().trim_matches(is_python_space).to_owned();
            value.preserve_safety(trimmed)
        }
    }
}

/// Mirrors HF transformers' `tojson` filter, not stock Jinja2's. Transformers
/// overrides Jinja's HTML-safe `tojson` with plain
/// `json.dumps(x, ensure_ascii=ensure_ascii, indent=indent, separators=separators,
/// sort_keys=sort_keys)`, `ensure_ascii` defaulting to `False`, in its chat-template
/// environment, and vLLM/SGLang render through that — so chat templates (and the
/// models trained on their output) expect Python separators and **no** HTML
/// escaping (`'`, `<`, `>`, `&` stay literal).
pub fn tojson(value: Value, kwargs: Kwargs) -> Result<Value, Error> {
    // Python takes a space count or the indent string itself.
    let indent = kwargs
        .get::<Option<Value>>("indent")?
        .map(|indent| match indent.as_str() {
            Some(text) => Ok(text.to_owned()),
            None => i64::try_from(indent)
                .map(|n| " ".repeat(usize::try_from(n).unwrap_or(0)))
                .map_err(|_| {
                    Error::new(
                        ErrorKind::InvalidOperation,
                        "tojson indent must be an int or a string",
                    )
                }),
        })
        .transpose()?;
    // Python unpacks any two-item iterable of strings, so `',:'` works too.
    let separators = kwargs
        .get::<Option<Value>>("separators")?
        .map(|pair| {
            let pair: Vec<Value> = pair.try_iter().map(Iterator::collect).unwrap_or_default();
            match pair.as_slice() {
                [item, key] => item.as_str().zip(key.as_str()),
                _ => None,
            }
            .map(|(item, key)| (item.to_owned(), key.to_owned()))
            .ok_or_else(|| {
                Error::new(
                    ErrorKind::InvalidOperation,
                    "tojson separators must be an (item, key) pair of strings",
                )
            })
        })
        .transpose()?;
    // Python defaults to `(", ", ": ")`, or `(",", ": ")` when `indent` is set.
    let (item_sep, key_sep) = match &separators {
        Some((item, key)) => (item.as_str(), key.as_str()),
        None if indent.is_some() => (",", ": "),
        None => (", ", ": "),
    };
    let flag = |key| -> Result<bool, Error> {
        Ok(kwargs
            .get::<Option<Value>>(key)?
            .is_some_and(|v| v.is_true()))
    };
    let value = if flag("sort_keys")? {
        crate::python::template_json(PySortedKeys(value))
    } else {
        crate::python::template_json(value)
    }
    .map_err(|err| {
        Error::new(ErrorKind::BadSerialization, "cannot serialize to JSON").with_source(err)
    })?;
    let formatter =
        PyJsonFormatter::new(item_sep, key_sep, indent.as_deref(), flag("ensure_ascii")?);
    let mut buf = Vec::new();
    let mut serializer = serde_json::Serializer::with_formatter(&mut buf, formatter);
    value.serialize(&mut serializer).map_err(|err| {
        Error::new(ErrorKind::BadSerialization, "cannot serialize to JSON").with_source(err)
    })?;
    String::from_utf8(buf)
        .map_err(|err| {
            Error::new(ErrorKind::BadSerialization, "cannot serialize to JSON").with_source(err)
        })
        .map(Value::from_safe_string)
}

/// A template value whose maps serialize with their keys in Python's `sorted()` order.
/// `json.dumps(sort_keys=True)` sorts the original keys and only then writes them as
/// strings, so `{10: 'a', 2: 'b'}` puts 2 first, where sorting the strings would put
/// `"10"` first.
struct PySortedKeys(Value);

impl Serialize for PySortedKeys {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let Some(object) = self.0.as_object() else {
            return self.0.serialize(serializer);
        };
        match object.repr() {
            ObjectRepr::Map => {
                let mut pairs: Vec<_> = object.try_iter_pairs().into_iter().flatten().collect();
                python_sort_keys(&mut pairs).map_err(S::Error::custom)?;
                serializer.collect_map(
                    pairs
                        .into_iter()
                        .map(|(key, value)| (key, PySortedKeys(value))),
                )
            }
            ObjectRepr::Seq | ObjectRepr::Iterable => {
                serializer.collect_seq(object.try_iter().into_iter().flatten().map(PySortedKeys))
            }
            _ => self.0.serialize(serializer),
        }
    }
}

/// Python's `sorted()` over dict keys: strings order among strings, and numbers, bools
/// included, among numbers. Python raises `TypeError` for any other mix, so it fails
/// the render.
fn python_sort_keys(pairs: &mut [(Value, Value)]) -> Result<(), Error> {
    let number = |key: &Value| match key.kind() {
        ValueKind::Bool => Some(Value::from(i64::from(key.is_true()))),
        ValueKind::Number => Some(key.clone()),
        _ => None,
    };
    if pairs.iter().all(|(key, _)| key.kind() == ValueKind::String) {
        pairs.sort_by(|(a, _), (b, _)| a.as_str().cmp(&b.as_str()));
    } else if pairs.iter().all(|(key, _)| number(key).is_some()) {
        pairs.sort_by_cached_key(|(key, _)| number(key));
    } else if pairs.len() > 1 {
        return Err(Error::new(
            ErrorKind::InvalidOperation,
            "tojson sort_keys cannot order keys of different types",
        ));
    }
    Ok(())
}

/// Parse a JSON string into a structured value.
///
/// HuggingFace/transformers chat-template environments expose this filter, and several
/// published templates depend on it — e.g. Step-3.7-Flash's `tool_use` block applies it to
/// a tool call's `arguments`, which arrive as a JSON *string*, to iterate the decoded
/// object. Without it minijinja aborts the render with `unknown filter: fromjson`, which
/// fails every multi-turn tool-call request.
///
/// Values that are not strings pass through untouched, so templates that apply the filter
/// defensively to an already-decoded value keep rendering.
pub fn fromjson(value: Value) -> Result<Value, Error> {
    let Some(text) = value.as_str() else {
        return Ok(value);
    };
    let parsed: serde_json::Value = serde_json::from_str(text).map_err(|err| {
        Error::new(ErrorKind::InvalidOperation, "cannot parse JSON").with_source(err)
    })?;
    Ok(crate::python::template_value(&parsed))
}

pub fn strftime_now(format_str: &str) -> Result<Value, Error> {
    let local: DateTime<Local> = Local::now();
    Ok(Value::from_safe_string(
        local.format(format_str).to_string(),
    ))
}

#[cfg(test)]
mod fromjson_tests {
    use super::*;

    #[test]
    fn parses_json_object_string() {
        let out = fromjson(Value::from(r#"{"location":"San Francisco","n":3}"#)).unwrap();
        assert_eq!(
            out.get_attr("location").unwrap().as_str(),
            Some("San Francisco")
        );
        assert_eq!(out.get_attr("n").unwrap().to_string(), "3");
    }

    #[test]
    fn parses_json_array_string() {
        let out = fromjson(Value::from(r#"[1,2,3]"#)).unwrap();
        assert_eq!(out.len(), Some(3));
    }

    #[test]
    fn passes_through_non_string() {
        // Already-decoded values must survive a defensive `| fromjson`.
        let already = crate::python::template_value(&serde_json::json!({"a": 1}));
        let out = fromjson(already).unwrap();
        assert_eq!(out.get_attr("a").unwrap().to_string(), "1");
    }

    #[test]
    fn errors_on_malformed_json() {
        assert!(fromjson(Value::from("{not json")).is_err());
    }

    /// Regression: renders the shape of Step-3.7-Flash's `tool_use` block, where a tool
    /// call's `arguments` arrive as a JSON string. Before the filter existed this failed
    /// with `unknown filter: fromjson`, 500-ing every multi-turn tool-call request.
    #[test]
    fn renders_tool_use_block_with_json_string_arguments() {
        let mut env = minijinja::Environment::new();
        env.add_filter("fromjson", fromjson);
        env.add_template(
            "tool_use",
            "{% for tc in tool_calls %}{% set a = tc.function.arguments | fromjson %}\
CALL {{ tc.function.name }} loc={{ a.location }} unit={{ a.unit }}{% endfor %}",
        )
        .unwrap();
        let rendered = env
            .get_template("tool_use")
            .unwrap()
            .render(minijinja::context! { tool_calls => serde_json::json!([{
                "function": {
                    "name": "get_weather",
                    "arguments": "{\"location\":\"San Francisco\",\"unit\":\"F\"}"
                }
            }])})
            .expect("tool_use template must render once `fromjson` is registered");
        assert_eq!(rendered, "CALL get_weather loc=San Francisco unit=F");
    }
}

/// Expected strings are HF transformers' `tojson` output for the same input and
/// kwargs, i.e. Python `json.dumps(x, ensure_ascii=False, ...)`.
#[cfg(test)]
mod tojson_tests {
    use super::*;
    use serde_json::json;

    fn try_render(tmpl: &str, x: serde_json::Value) -> Result<String, Error> {
        let mut env = minijinja::Environment::new();
        env.add_filter("tojson", tojson);
        env.render_str(tmpl, minijinja::context! { x })
    }

    fn render(tmpl: &str, x: serde_json::Value) -> String {
        try_render(tmpl, x).unwrap()
    }

    fn shape() -> serde_json::Value {
        json!({"a": [1, 2], "b": {"c": "é"}, "d": [], "e": {}})
    }

    /// Kimi K2.x `tool_declare` renders `tools | tojson(separators=(',', ':'))`.
    #[test]
    fn honours_separators() {
        let tools = json!([{"type": "function", "function": {"name": "get_weather",
            "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}}}]);
        assert_eq!(
            render("{{ x | tojson(separators=(',', ':')) }}", tools),
            r#"[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object","properties":{"city":{"type":"string"}}}}}]"#
        );
    }

    #[test]
    fn honours_separators_with_indent() {
        assert_eq!(
            render(
                "{{ x | tojson(indent=2, separators=(', ', ':')) }}",
                shape()
            ),
            "{\n  \"a\":[\n    1, \n    2\n  ], \n  \"b\":{\n    \"c\":\"é\"\n  }, \n  \"d\":[], \n  \"e\":{}\n}"
        );
    }

    /// Python takes the indent string itself as well as a space count.
    #[test]
    fn indent_accepts_a_string() {
        assert_eq!(
            render("{{ x | tojson(indent='\t') }}", json!({"a": [1]})),
            "{\n\t\"a\": [\n\t\t1\n\t]\n}"
        );
    }

    /// Python unpacks `separators` from any two-item iterable, a string included.
    #[test]
    fn separators_accept_a_two_character_string() {
        assert_eq!(
            render("{{ x | tojson(separators=',:') }}", shape()),
            r#"{"a":[1,2],"b":{"c":"é"},"d":[],"e":{}}"#
        );
    }

    /// Python's `json.dumps` raises on these too.
    #[test]
    fn rejects_malformed_kwargs() {
        for tmpl in [
            "{{ x | tojson(separators=(',',)) }}",
            "{{ x | tojson(separators=(1, 2)) }}",
            "{{ x | tojson(indent=[2]) }}",
        ] {
            let err = try_render(tmpl, shape()).unwrap_err();
            assert_eq!(err.kind(), ErrorKind::InvalidOperation, "{tmpl}");
        }
    }

    #[test]
    fn sort_keys_sorts_nested_maps() {
        let x = json!({"b": 1, "a": {"d": [{"z": 1, "y": 2}], "c": 3}});
        assert_eq!(
            render("{{ x | tojson(sort_keys=true) }}", x),
            r#"{"a": {"c": 3, "d": [{"y": 2, "z": 1}]}, "b": 1}"#
        );
    }

    /// Python sorts the original keys and only then writes them as strings, so 2
    /// precedes 10, and comparing a string key with a number key raises.
    #[test]
    fn sort_keys_compares_original_keys() {
        assert_eq!(
            render(
                "{{ {10: 'ten', 2: 'two'} | tojson(sort_keys=true) }}",
                json!(null)
            ),
            r#"{"2": "two", "10": "ten"}"#
        );
        assert!(
            try_render(
                "{{ {'2': 'str', 2: 'int'} | tojson(sort_keys=true) }}",
                json!(null)
            )
            .is_err()
        );
    }

    #[test]
    fn ensure_ascii_escapes_like_python() {
        let x = json!({"clé": "café 😀 \u{7f} \n"});
        assert_eq!(
            render("{{ x | tojson(ensure_ascii=true) }}", x),
            r#"{"cl\u00e9": "caf\u00e9 \ud83d\ude00 \u007f \n"}"#
        );
    }

    #[test]
    fn defaults_unchanged() {
        let flat = r#"{"a": [1, 2], "b": {"c": "é"}, "d": [], "e": {}}"#;
        assert_eq!(render("{{ x | tojson }}", shape()), flat);
        assert_eq!(
            render(
                "{{ x | tojson(separators=none, ensure_ascii=false) }}",
                shape()
            ),
            flat
        );
        assert_eq!(
            render("{{ x | tojson(indent=2) }}", shape()),
            "{\n  \"a\": [\n    1,\n    2\n  ],\n  \"b\": {\n    \"c\": \"é\"\n  },\n  \"d\": [],\n  \"e\": {}\n}"
        );
    }

    /// `sort_keys` re-serializes through `serde_json::Value`; floats keep Python spelling.
    #[test]
    fn sort_keys_keeps_python_floats() {
        assert_eq!(
            render(
                "{{ x | tojson(separators=(',', ':'), sort_keys=true) }}",
                json!({"n": 1e16, "lr": 0.000001})
            ),
            r#"{"lr":1e-06,"n":1e+16}"#
        );
    }
}