dynamo-llm 1.7.0

Dynamo LLM Library
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Prompt formatting (lib/llm side).
//!
//! The reusable chat-template / prompt-formatting engine lives in the
//! standalone, runtime-free [`dynamo_renderer`] crate. This module holds only the
//! lib/llm-local glue that can't live there:
//!   * implements [`OAIChatLikeRequest`] for Dynamo's `Nv*` request wrappers,
//!   * keeps media-IO config off the rendering trait via [`MediaRequestExt`]
//!     (so `dynamo_renderer` need not depend on the media module),
//!   * adapts a [`ModelDeploymentCard`] into a [`PromptFormatter`]
//!     ([`prompt_formatter_from_mdc`]).
//!
//! Everything else imports from `dynamo_renderer` directly.

use anyhow::{Context, Result};
use minijinja::value::Value;

use dynamo_renderer::{
    ChatTemplate, ChatTemplateValue, ContextMixins, OAIChatLikeRequest, PromptFormatter,
    PromptInput, RenderedPrompt, RenderedSegment, TextInput, TokenInput, deepseek_formatter_for,
    kimi_k3_formatter_for, may_be_fix_tool_schema,
};

use crate::model_card::{ModelDeploymentCard, PromptFormatterArtifact};
use crate::protocols::openai::{
    chat_completions::NvCreateChatCompletionRequest, completions::NvCreateCompletionRequest,
};

/// lib/llm-local extension carrying multimodal media-IO config. Kept off
/// [`OAIChatLikeRequest`] so `dynamo_renderer` stays free of the media module;
/// the multimodal preprocessing path bounds on `OAIChatLikeRequest + MediaRequestExt`.
///
/// Opaque JSON: the frontend only interprets it when it owns decoding, so options
/// belonging to the worker pass through untouched.
pub trait MediaRequestExt {
    fn media_io_kwargs(&self) -> Option<&serde_json::Value>;
}

/// Parse a JSON object string into a serde_json Map, preserving the exact
/// spelling of numbers that cannot be round-tripped through f64 without loss.
/// Numbers that survive f64 conversion unchanged are kept as `Value::Number`;
/// numbers that would be corrupted (e.g. integers with >15 significant digits)
/// are kept as `Value::String` so the original value is available to templates.
/// Returns `None` when the top-level JSON value is not an object.
fn parse_args_object(
    s: &str,
) -> anyhow::Result<Option<serde_json::Map<String, serde_json::Value>>> {
    // First pass: parse the top-level value to check it is an object.
    let top: serde_json::Value = serde_json::from_str(s)?;
    let top_obj = match top {
        serde_json::Value::Object(m) => m,
        _ => return Ok(None),
    };

    // Second pass: re-parse each entry's value via its RawValue to detect
    // numbers that serde_json would corrupt through f64.
    // We use a raw string-to-rawvalue helper to get the exact bytes.
    let mut out = serde_json::Map::with_capacity(top_obj.len());
    for (k, cooked) in top_obj {
        // For non-numbers, use the already-parsed value directly.
        if !cooked.is_number() {
            out.insert(k, cooked);
            continue;
        }
        // For numbers: find the raw token in the source string so we can
        // verify that serde_json's f64 representation matches the original.
        // We do this by re-serializing the cooked number and comparing.
        let cooked_str = cooked.to_string();
        if let Some(raw_token) = extract_value_token(s, &k) {
            let raw_trimmed = raw_token.trim();
            if raw_trimmed != cooked_str.as_str() {
                // Precision was lost (e.g. 30-digit integer → 1.23e+29).
                // Keep the original spelling as a string.
                out.insert(k, serde_json::Value::String(raw_trimmed.to_string()));
                continue;
            }
        }
        out.insert(k, cooked);
    }
    Ok(Some(out))
}

/// Extract the raw JSON token for `key` from a JSON object string `s`.
/// Returns `None` when the key is not found or parsing fails.
fn extract_value_token<'a>(s: &'a str, key: &str) -> Option<&'a str> {
    // Build the escaped key as it appears in JSON.
    let key_json = serde_json::to_string(key).ok()?;
    // Find `"key":` in the source.
    let needle = format!("{}:", key_json);
    let start = s.find(needle.as_str())?;
    let after_colon = s[start + needle.len()..].trim_start();
    // Measure the extent of the JSON token.
    let end = token_end(after_colon)?;
    let source_offset = s.len() - s[start + needle.len()..].len()
        + (after_colon.as_ptr() as usize - s[start + needle.len()..].as_ptr() as usize);
    Some(&s[source_offset..source_offset + end])
}

/// Return the byte length of the first JSON token in `s`.
fn token_end(s: &str) -> Option<usize> {
    let s = s.trim_start();
    let first = s.chars().next()?;
    match first {
        '"' => {
            let mut i = 1;
            let b = s.as_bytes();
            while i < b.len() {
                if b[i] == b'\\' {
                    i += 2;
                } else if b[i] == b'"' {
                    return Some(i + 1);
                } else {
                    i += 1;
                }
            }
            None
        }
        '{' | '[' => {
            // Skip balanced braces/brackets — good enough for flat objects.
            let (open, close) = if first == '{' {
                (b'{', b'}')
            } else {
                (b'[', b']')
            };
            let mut depth = 0i32;
            let mut in_str = false;
            let mut escape = false;
            for (i, &b) in s.as_bytes().iter().enumerate() {
                if escape {
                    escape = false;
                    continue;
                }
                if in_str {
                    if b == b'\\' {
                        escape = true;
                    } else if b == b'"' {
                        in_str = false;
                    }
                } else {
                    match b {
                        b'"' => in_str = true,
                        b if b == open => depth += 1,
                        b if b == close => {
                            depth -= 1;
                            if depth == 0 {
                                return Some(i + 1);
                            }
                        }
                        _ => {}
                    }
                }
            }
            None
        }
        _ => {
            // Number, bool, null — ends at first delimiter.
            let end = s
                .find(|c: char| c == ',' || c == '}' || c == ']' || c.is_whitespace())
                .unwrap_or(s.len());
            if end == 0 { None } else { Some(end) }
        }
    }
}

/// Parse `tool_calls[*].function.arguments` from JSON string to object before
/// handing messages to MiniJinja. Some chat templates (e.g. GLM-5.2) call
/// `.items()` on arguments and require a dict; the OpenAI wire format carries
/// arguments as a JSON string. This matches TRT-LLM's chat_utils.py behaviour.
///
/// Large integer values in arguments are preserved exactly because `dynamo-llm`
/// enables the `serde_json/arbitrary_precision` feature.
///
/// Returns an error if any tool_call's arguments string contains invalid JSON,
/// so callers can surface a 400-style error instead of fabricating empty args.
pub(crate) fn normalize_tool_call_arguments(
    messages_json: &mut serde_json::Value,
) -> anyhow::Result<()> {
    let Some(messages) = messages_json.as_array_mut() else {
        return Ok(());
    };
    for message in messages {
        let Some(tool_calls) = message
            .get_mut("tool_calls")
            .and_then(serde_json::Value::as_array_mut)
        else {
            continue;
        };
        for tc in tool_calls.iter_mut() {
            let Some(args_str) = tc.pointer("/function/arguments").and_then(|v| v.as_str()) else {
                continue;
            };
            // Empty string is a valid no-argument call; normalise to {}.
            if args_str.is_empty() {
                if let Some(obj) = tc
                    .get_mut("function")
                    .and_then(serde_json::Value::as_object_mut)
                {
                    obj.insert(
                        "arguments".to_string(),
                        serde_json::Value::Object(serde_json::Map::new()),
                    );
                }
                continue;
            }
            let value = match parse_args_object(args_str) {
                Ok(Some(map)) => serde_json::Value::Object(map),
                Ok(None) => {
                    // Scalar or array — GLM's .items() would panic at render time.
                    tracing::warn!(
                        args_len = args_str.len(),
                        "tool_call arguments parsed to a non-object; \
                         substituting {{}} for template safety"
                    );
                    serde_json::Value::Object(serde_json::Map::new())
                }
                Err(e) => {
                    anyhow::bail!(
                        "tool_call arguments are not valid JSON (len={}): {e}",
                        args_str.len()
                    );
                }
            };
            if let Some(obj) = tc
                .get_mut("function")
                .and_then(serde_json::Value::as_object_mut)
            {
                obj.insert("arguments".to_string(), value);
            }
        }
    }
    Ok(())
}

impl OAIChatLikeRequest for NvCreateChatCompletionRequest {
    fn model(&self) -> String {
        self.inner.model.clone()
    }

    fn messages(&self) -> Value {
        let messages_json = self.template_messages_json();
        Value::from_serialize(&messages_json)
    }

    fn template_messages_json_override(&self) -> Option<serde_json::Value> {
        self.responses_tools
            .as_ref()
            .filter(|context| !context.history_phases.is_empty())
            .map(|_| self.template_messages_json())
    }

    fn typed_messages(&self) -> Option<&[dynamo_protocols::types::ChatCompletionRequestMessage]> {
        Some(self.inner.messages.as_slice())
    }

    fn tools(&self) -> Option<Value> {
        if let Some(tools) = self.declared_template_tools() {
            return may_be_fix_tool_schema(tools);
        }
        let tools = self.inner.tools.as_ref()?;
        // Custom tools use the function schema before template-specific repair.
        may_be_fix_tool_schema(
            serde_json::to_value(
                tools
                    .iter()
                    .map(|tool| tool.function_template_tool())
                    .collect::<Vec<_>>(),
            )
            .unwrap(),
        )
    }

    fn template_tools(&self) -> Option<minijinja::value::Value> {
        self.native_template_tools()
    }

    fn tool_choice(&self) -> Option<Value> {
        self.inner
            .tool_choice
            .as_ref()
            .map(|choice| Value::from_serialize(choice.function_template_choice()))
    }

    fn response_format(&self) -> Option<Value> {
        self.inner
            .response_format
            .as_ref()
            .map(Value::from_serialize)
    }

    fn should_add_generation_prompt(&self) -> bool {
        // vLLM / HF: continue_final_message leaves the last turn open, which is
        // incompatible with appending a new generation prompt. Validation already
        // rejects the omitted/true add_generation_prompt combination; keep this
        // guard so internal callers that skip ValidateRequest still match HF.
        if self.common.continue_final_message == Some(true) {
            return false;
        }
        self.common.add_generation_prompt.unwrap_or(true)
    }

    fn extract_text(&self) -> Option<TextInput> {
        Some(TextInput::Single(String::new()))
    }

    fn chat_template_args(&self) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
        self.chat_template_args.as_ref()
    }

    fn mm_processor_kwargs(&self) -> Option<&serde_json::Value> {
        self.inner.mm_processor_kwargs.as_ref()
    }
}

impl MediaRequestExt for NvCreateChatCompletionRequest {
    fn media_io_kwargs(&self) -> Option<&serde_json::Value> {
        self.media_io_kwargs.as_ref()
    }
}

impl OAIChatLikeRequest for NvCreateCompletionRequest {
    fn model(&self) -> String {
        self.inner.model.clone()
    }
    fn messages(&self) -> minijinja::value::Value {
        let message = dynamo_protocols::types::ChatCompletionRequestMessage::User(
            dynamo_protocols::types::ChatCompletionRequestUserMessage {
                content: dynamo_protocols::types::ChatCompletionRequestUserMessageContent::Text(
                    crate::protocols::openai::completions::prompt_to_string(&self.inner.prompt),
                ),
                name: None,
            },
        );

        minijinja::value::Value::from_serialize(vec![message])
    }

    fn should_add_generation_prompt(&self) -> bool {
        true
    }

    fn prompt_input_type(&self) -> PromptInput {
        match &self.inner.prompt {
            dynamo_protocols::types::Prompt::IntegerArray(_) => {
                PromptInput::Tokens(TokenInput::Single(vec![]))
            }
            dynamo_protocols::types::Prompt::ArrayOfIntegerArray(_) => {
                PromptInput::Tokens(TokenInput::Batch(vec![]))
            }
            dynamo_protocols::types::Prompt::String(_) => {
                PromptInput::Text(TextInput::Single(String::new()))
            }
            dynamo_protocols::types::Prompt::StringArray(_) => {
                PromptInput::Text(TextInput::Batch(vec![]))
            }
        }
    }

    fn extract_tokens(&self) -> Option<TokenInput> {
        match &self.inner.prompt {
            dynamo_protocols::types::Prompt::IntegerArray(tokens) => {
                Some(TokenInput::Single(tokens.clone()))
            }
            dynamo_protocols::types::Prompt::ArrayOfIntegerArray(arrays) => {
                Some(TokenInput::Batch(arrays.clone()))
            }
            _ => None,
        }
    }

    fn extract_text(&self) -> Option<TextInput> {
        match &self.inner.prompt {
            dynamo_protocols::types::Prompt::String(text) => {
                Some(TextInput::Single(text.to_string()))
            }
            dynamo_protocols::types::Prompt::StringArray(texts) => {
                Some(TextInput::Batch(texts.to_vec()))
            }
            _ => None,
        }
    }
}

impl MediaRequestExt for NvCreateCompletionRequest {
    fn media_io_kwargs(&self) -> Option<&serde_json::Value> {
        None
    }
}

/// Build a [`PromptFormatter`] from a [`ModelDeploymentCard`].
///
/// Model families whose HF repos ship no Jinja `chat_template` get a native
/// Rust formatter; everything else loads the
/// HF `tokenizer_config.json` template (and any separate chat-template file)
/// and builds via [`PromptFormatter::from_parts`].
pub fn prompt_formatter_from_mdc(mdc: &ModelDeploymentCard) -> Result<PromptFormatter> {
    // Prefer the authoritative `model_type` from config.json — it's set by the
    // model author and survives any `--served-model-name` rename. An empty
    // `model_type` carries no signal — normalize to `None` so the display-name
    // fallback still runs.
    let model_type_lower = mdc
        .model_info
        .as_ref()
        .and_then(|info| info.get_model_info().ok())
        .map(|info| info.model_type().to_lowercase())
        .filter(|s| !s.is_empty());
    let display_name_lower = mdc.display_name.to_lowercase();

    if let Some(formatter) = kimi_k3_formatter_for(
        &model_type_lower,
        &display_name_lower,
        mdc.runtime_config.exclude_tools_when_tool_choice_none,
    ) {
        return Ok(formatter);
    }

    if let Some(formatter) = deepseek_formatter_for(&model_type_lower, &display_name_lower) {
        return Ok(formatter);
    }

    match mdc
        .prompt_formatter
        .as_ref()
        .ok_or(anyhow::anyhow!("MDC does not contain a prompt formatter"))?
    {
        PromptFormatterArtifact::HfTokenizerConfigJson(checked_file) => {
            let Some(file) = checked_file.path() else {
                anyhow::bail!(
                    "HfTokenizerConfigJson for {} is a URL, cannot load",
                    mdc.display_name
                );
            };
            let contents = std::fs::read_to_string(file).with_context(|| {
                format!(
                    "prompt_formatter_from_mdc fs:read_to_string '{}'",
                    file.display()
                )
            })?;
            let mut config: ChatTemplate = serde_json::from_str(&contents).inspect_err(|err| {
                crate::log_json_err(&file.display().to_string(), &contents, err)
            })?;

            // Some HF models (e.g. Llama-4-Maverick) store the chat template in a
            // separate file, or it may be a custom template provided via CLI flag.
            match mdc.chat_template_file.as_ref() {
                Some(PromptFormatterArtifact::HfChatTemplateJinja {
                    file: checked_file, ..
                }) => {
                    let Some(path) = checked_file.path() else {
                        anyhow::bail!(
                            "HfChatTemplateJinja for {} is a URL, cannot load",
                            mdc.display_name
                        );
                    };
                    let chat_template = std::fs::read_to_string(path)
                        .with_context(|| format!("fs:read_to_string '{}'", path.display()))?;
                    config.chat_template = Some(ChatTemplateValue(either::Left(chat_template)));
                }
                Some(PromptFormatterArtifact::HfChatTemplateJson {
                    file: checked_file, ..
                }) => {
                    let Some(path) = checked_file.path() else {
                        anyhow::bail!(
                            "HfChatTemplateJson for {} is a URL, cannot load",
                            mdc.display_name
                        );
                    };
                    let raw = std::fs::read_to_string(path)
                        .with_context(|| format!("fs:read_to_string '{}'", path.display()))?;
                    let wrapper: serde_json::Value = serde_json::from_str(&raw)
                        .with_context(|| format!("Failed to parse '{}' as JSON", path.display()))?;
                    let field = wrapper.get("chat_template").ok_or_else(|| {
                        anyhow::anyhow!(
                            "'{}' does not contain a 'chat_template' field",
                            path.display()
                        )
                    })?;
                    let value = serde_json::from_value::<ChatTemplateValue>(field.clone())
                        .with_context(|| {
                            format!(
                                "Failed to deserialize 'chat_template' in '{}'",
                                path.display()
                            )
                        })?;
                    config.chat_template = Some(value);
                }
                _ => {}
            }
            PromptFormatter::from_parts(
                config,
                mdc.prompt_context
                    .clone()
                    .map_or(ContextMixins::default(), |x| ContextMixins::new(&x)),
                mdc.runtime_config.exclude_tools_when_tool_choice_none,
            )
        }
        PromptFormatterArtifact::HfChatTemplateJinja { .. }
        | PromptFormatterArtifact::HfChatTemplateJson { .. } => Err(anyhow::anyhow!(
            "prompt_formatter should not have type HfChatTemplate*"
        )),
    }
}

const CONTINUE_FINAL_MESSAGE_NOT_FOUND: &str =
    "Unable to continue the final message because it was not found in the rendered chat template.";

/// HuggingFace `render_jinja_template` marker. Appended to last-message content
/// before render so truncation uniquely identifies the final turn even when the
/// template rewrites or repeats earlier text.
pub(crate) const CONTINUE_FINAL_MESSAGE_TAG: &str = "CONTINUE_FINAL_MESSAGE_TAG ";

pub(crate) struct FinalMessageTags {
    pub start: String,
    pub end: String,
}

impl FinalMessageTags {
    pub fn new() -> Self {
        let id = uuid::Uuid::new_v4().simple().to_string();
        Self {
            start: format!("DYNAMO_FINAL_MESSAGE_{id}_BEGIN"),
            end: format!("DYNAMO_FINAL_MESSAGE_{id}_END"),
        }
    }
}

/// Append [`CONTINUE_FINAL_MESSAGE_TAG`] to the last message's text content.
/// Array content uses the last non-empty `text` part, matching Transformers.
pub(crate) fn append_continue_final_message_tag(
    messages: &mut serde_json::Value,
    tags: Option<&FinalMessageTags>,
) -> Result<()> {
    let Some(last) = messages.as_array_mut().and_then(|arr| arr.last_mut()) else {
        anyhow::bail!(CONTINUE_FINAL_MESSAGE_NOT_FOUND);
    };
    match last.get_mut("content") {
        Some(serde_json::Value::String(text)) if !text.is_empty() => {
            if let Some(tags) = tags {
                text.insert_str(text.len() - text.trim_start().len(), &tags.start);
                text.push_str(&tags.end);
            }
            text.push_str(CONTINUE_FINAL_MESSAGE_TAG);
            Ok(())
        }
        Some(serde_json::Value::Array(parts)) => {
            if let Some(tags) = tags {
                for part in parts.iter_mut() {
                    if let Some(serde_json::Value::String(text)) = part.get_mut("text")
                        && !text.is_empty()
                    {
                        text.insert_str(text.len() - text.trim_start().len(), &tags.start);
                        break;
                    }
                }
            }
            for part in parts.iter_mut().rev() {
                if let Some(serde_json::Value::String(text)) = part.get_mut("text") {
                    if text.is_empty() {
                        continue;
                    }
                    if let Some(tags) = tags {
                        text.push_str(&tags.end);
                    }
                    text.push_str(CONTINUE_FINAL_MESSAGE_TAG);
                    return Ok(());
                }
            }
            anyhow::bail!(CONTINUE_FINAL_MESSAGE_NOT_FOUND);
        }
        _ => anyhow::bail!(CONTINUE_FINAL_MESSAGE_NOT_FOUND),
    }
}

/// HuggingFace `apply_chat_template(continue_final_message=True)`: after render,
/// drop the marker and any tokens after it so the prompt ends at the last
/// message text.
///
/// The renderer must have seen [`CONTINUE_FINAL_MESSAGE_TAG`] in the last
/// message. Searching for the raw last-message string cannot prove the match
/// belongs to the final turn (a template may rewrite or repeat earlier copies).
///
/// When the renderer returned segment boundaries (Kimi K3 XTML), keep them so
/// tokenization still uses `encode_segments` instead of flattening to plain text.
/// Missing marker, empty content, or a template that dropped the marker is an
/// invalid request rather than silently returning the closed prompt.
pub(crate) fn apply_continue_final_message(prompt: RenderedPrompt) -> Result<RenderedPrompt> {
    let rendered = prompt.as_str();
    let tag = CONTINUE_FINAL_MESSAGE_TAG;
    let tag_name = tag.trim_end();
    let Some(tag_loc) = rendered.rfind(tag_name) else {
        anyhow::bail!(CONTINUE_FINAL_MESSAGE_NOT_FOUND);
    };
    // Transformers: if the full tag (including trailing space) survived, cut
    // at the tag. If the template trimmed that space, also rstrip the prefix.
    let end = if rendered[tag_loc..].starts_with(tag) {
        tag_loc
    } else {
        rendered[..tag_loc].trim_end().len()
    };
    Ok(truncate_rendered_prompt(prompt, end))
}

/// Recover the rendered content boundary without interpreting marker-looking body data.
/// Preserve tokenization trust segments when removing the temporary render marker.
pub(crate) fn resolve_final_message_boundary(
    prompt: RenderedPrompt,
    tags: &FinalMessageTags,
    header_marker: &str,
) -> Result<(RenderedPrompt, usize)> {
    let rendered = prompt.as_str();
    let mut bodies = Vec::new();
    let mut removals = Vec::new();
    for (start, _) in rendered.match_indices(&tags.start) {
        let body_start = start + tags.start.len();
        let end = body_start
            + rendered[body_start..]
                .find(&tags.end)
                .ok_or_else(|| anyhow::anyhow!(CONTINUE_FINAL_MESSAGE_NOT_FOUND))?;
        anyhow::ensure!(
            !rendered[body_start..end].contains(&tags.start),
            "Final-message markers were reordered by the chat template"
        );
        bodies.push(start..end + tags.end.len());
        removals.push(start..body_start);
        let mut end_marker_end = end + tags.end.len();
        if rendered[end_marker_end..].starts_with(CONTINUE_FINAL_MESSAGE_TAG) {
            end_marker_end += CONTINUE_FINAL_MESSAGE_TAG.len();
        } else if rendered[end_marker_end..].starts_with(CONTINUE_FINAL_MESSAGE_TAG.trim_end()) {
            end_marker_end += CONTINUE_FINAL_MESSAGE_TAG.trim_end().len();
        }
        removals.push(end..end_marker_end);
    }
    let last_body = bodies
        .last()
        .ok_or_else(|| anyhow::anyhow!(CONTINUE_FINAL_MESSAGE_NOT_FOUND))?;
    // A template may repeat content before or within the final turn. Only controls
    // outside all marked content copies can identify its actual rendered header.
    let header_end = rendered[..last_body.start]
        .rmatch_indices(header_marker)
        .find(|(at, _)| !bodies.iter().any(|body| body.contains(at)))
        .map(|(at, _)| at + header_marker.len())
        .unwrap_or(0);
    let header_end = header_end
        - removals
            .iter()
            .filter(|range| range.end <= header_end)
            .map(|range| range.len())
            .sum::<usize>();
    removals.sort_by_key(|range| range.start);
    let remove_from = |text: &str, offset: usize| {
        let mut text = text.to_string();
        for range in removals.iter().rev() {
            let from = range.start.saturating_sub(offset).min(text.len());
            let to = range.end.saturating_sub(offset).min(text.len());
            if from < to {
                text.replace_range(from..to, "");
            }
        }
        text
    };
    let rendered = if let Some(segments) = prompt.segments() {
        let mut offset = 0;
        let segments = segments
            .iter()
            .filter_map(|segment| {
                let text = remove_from(&segment.text, offset);
                offset += segment.text.len();
                (!text.is_empty()).then_some(RenderedSegment {
                    text,
                    allow_special: segment.allow_special,
                })
            })
            .collect();
        RenderedPrompt::segmented(segments)
    } else {
        RenderedPrompt::text(remove_from(prompt.as_str(), 0))
    };
    Ok((rendered, header_end))
}

/// Truncate `prompt` to the first `end` bytes of `as_str()`, preserving
/// `RenderedSegment` trust boundaries when they are present.
fn truncate_rendered_prompt(prompt: RenderedPrompt, end: usize) -> RenderedPrompt {
    let Some(segments) = prompt.segments() else {
        return RenderedPrompt::text(prompt.as_str()[..end].to_string());
    };
    let mut out = Vec::new();
    let mut offset = 0usize;
    for seg in segments {
        let next = offset + seg.text.len();
        if next <= end {
            if !seg.text.is_empty() {
                out.push(seg.clone());
            }
            offset = next;
            if offset == end {
                break;
            }
            continue;
        }
        if offset < end {
            let keep = end - offset;
            if keep > 0 && keep <= seg.text.len() && seg.text.is_char_boundary(keep) {
                out.push(RenderedSegment {
                    text: seg.text[..keep].to_string(),
                    allow_special: seg.allow_special,
                });
            }
        }
        break;
    }
    if out.is_empty() {
        RenderedPrompt::text(String::new())
    } else {
        RenderedPrompt::segmented(out)
    }
}

#[cfg(test)]
mod tests {
    use super::normalize_tool_call_arguments;

    #[test]
    fn chat_tool_deferral_reaches_prompt_and_python_request() {
        use crate::protocols::openai::chat_completions::NvCreateChatCompletionRequest;
        use dynamo_renderer::{ChatTemplate, ContextMixins, PromptFormatter};
        use serde_json::json;

        let template = include_str!("../../tests/data/muse_spark_open_deferred_tools.jinja");
        let config: ChatTemplate =
            serde_json::from_value(json!({"chat_template": template})).unwrap();
        let PromptFormatter::OAI(formatter) =
            PromptFormatter::from_parts(config, ContextMixins::new(&[]), true).unwrap();

        for stream in [false, true] {
            for nested in [false, true] {
                let mut tool = json!({"type": "function", "function": {
                    "name": "lookup", "description": "Find support tickets",
                    "parameters": {"type": "object", "properties": {
                        "deferred_query": {"type": "string"}
                    }}
                }});
                if nested {
                    tool["function"]["defer_loading"] = json!(true);
                } else {
                    tool["defer_loading"] = json!(true);
                }
                let request: NvCreateChatCompletionRequest = serde_json::from_value(json!({
                    "model": "muse_spark_open", "stream": stream,
                    "messages": [{"role": "user", "content": "Find a ticket"}],
                    "tools": [tool]
                }))
                .unwrap();
                assert!(request.responses_tools.is_none());
                let rendered = formatter.render(&request).unwrap();
                assert!(rendered.contains("[deferred: use tool_search to load full schema]"));
                assert!(!rendered.contains("deferred_query"), "{rendered}");
                let wire = serde_json::to_value(request).unwrap();
                assert_eq!(wire["tools"][0]["function"]["defer_loading"], json!(true));
            }
        }
    }

    fn make_tool_call_messages(arguments: &str) -> serde_json::Value {
        serde_json::json!([{
            "role": "assistant",
            "tool_calls": [{
                "id": "call_1",
                "type": "function",
                "function": { "name": "f", "arguments": arguments }
            }]
        }])
    }

    #[test]
    fn final_message_start_removal_preserves_segments_and_repeated_content() {
        use dynamo_renderer::{RenderedPrompt, RenderedSegment};
        let tags = super::FinalMessageTags {
            start: "BEGIN".into(),
            end: "FINISH".into(),
        };
        let prompt = RenderedPrompt::segmented(vec![
            RenderedSegment {
                text: "éHEADERBE".into(),
                allow_special: true,
            },
            RenderedSegment {
                text: "GINfirstFINISHCONTINUE_FINAL_MESSAGE_TAG BEGINsecondFINISH".into(),
                allow_special: false,
            },
        ]);
        let (prompt, header_end) =
            super::resolve_final_message_boundary(prompt, &tags, "HEADER").unwrap();
        assert_eq!(header_end, "éHEADER".len());
        assert_eq!(prompt.as_str(), "éHEADERfirstsecond");
        let segments = prompt.segments().unwrap();
        assert!(segments[0].allow_special);
        assert!(!segments[1].allow_special);
        assert!(super::resolve_final_message_boundary(prompt, &tags, "HEADER").is_err());
        for malformed in ["HEADERBEGINunfinished", "HEADERBEGINBEGINnestedFINISH"] {
            assert!(
                super::resolve_final_message_boundary(
                    RenderedPrompt::text(malformed.into()),
                    &tags,
                    "HEADER"
                )
                .is_err()
            );
        }
    }

    /// Regression: large integers must survive the string→object conversion
    /// without being silently corrupted to an f64 approximation.
    /// parse_args_object detects precision loss and falls back to Value::String,
    /// so the original spelling is preserved for the MiniJinja template.
    #[test]
    fn normalize_preserves_large_integer() {
        let big_int = "123456789012345678901234567890";
        let mut msgs = make_tool_call_messages(&format!("{{\"id\": {big_int}}}"));
        normalize_tool_call_arguments(&mut msgs).unwrap();
        let id_slot = &msgs[0]["tool_calls"][0]["function"]["arguments"]["id"];
        // parse_args_object stores it as Value::String when f64 would lose precision.
        let preserved = id_slot
            .as_str()
            .map(|s| s.to_string())
            .unwrap_or_else(|| id_slot.to_string());
        assert_eq!(
            preserved, big_int,
            "large integer must not be corrupted by f64 round-trip"
        );
    }

    /// Empty arguments string is a valid no-argument call; must normalise to {}
    /// without emitting a warning or returning an error.
    #[test]
    fn normalize_empty_string_becomes_empty_object() {
        let mut msgs = make_tool_call_messages("");
        normalize_tool_call_arguments(&mut msgs).unwrap();
        assert_eq!(
            msgs[0]["tool_calls"][0]["function"]["arguments"],
            serde_json::json!({}),
            "empty arguments string must normalise to an empty object"
        );
    }

    /// Malformed JSON in arguments must return an error, not substitute {}.
    #[test]
    fn normalize_malformed_json_returns_error() {
        let mut msgs = make_tool_call_messages("not-valid-json{");
        let result = normalize_tool_call_arguments(&mut msgs);
        assert!(
            result.is_err(),
            "malformed arguments JSON must return Err, not substitute {{}}"
        );
    }

    /// Well-formed object arguments pass through unchanged.
    #[test]
    fn normalize_valid_object_passes_through() {
        let mut msgs = make_tool_call_messages(r#"{"city": "Paris", "unit": "celsius"}"#);
        normalize_tool_call_arguments(&mut msgs).unwrap();
        assert_eq!(
            msgs[0]["tool_calls"][0]["function"]["arguments"]["city"],
            serde_json::json!("Paris")
        );
    }

    // --- Multi-turn opt-in behaviour tests ---
    //
    // These guard the boundary between JsonString (default, GPT-OSS / Harmony
    // path) and JsonObject (GLM-5.2 path).  The normalize function is the
    // shared primitive; NormalizedArgsRequest calls it only when the model
    // opts in; other models never call it.

    /// GLM-5.2 (JsonObject mode): historical arguments must be a parsed object
    /// so the chat template can call .items() on them.
    #[test]
    fn glm52_historical_arguments_are_object() {
        let mut msgs =
            make_tool_call_messages(r#"{"location": "San Francisco", "unit": "celsius"}"#);
        normalize_tool_call_arguments(&mut msgs).unwrap();
        let args = &msgs[0]["tool_calls"][0]["function"]["arguments"];
        assert!(
            args.is_object(),
            "GLM-5.2 (JsonObject mode): arguments must be a JSON object, got: {args}"
        );
        assert_eq!(args["location"], serde_json::json!("San Francisco"));
    }

    /// GPT-OSS (JsonString mode, default): historical arguments must stay as
    /// a raw JSON string so the Harmony parser can re-emit a structured call.
    /// This test models the multi-turn scenario where the first tool call is
    /// in conversation history and the second must be parsed correctly.
    #[test]
    fn gptoss_historical_arguments_remain_string() {
        // Without normalization (JsonString default), arguments stay as-is.
        let msgs = make_tool_call_messages(r#"{"location": "San Francisco", "unit": "celsius"}"#);
        let args = &msgs[0]["tool_calls"][0]["function"]["arguments"];
        assert!(
            args.is_string(),
            "GPT-OSS (JsonString mode): arguments must remain a JSON string, got: {args}"
        );
        // The raw string is preserved for the Harmony template to handle.
        assert_eq!(
            args.as_str().unwrap(),
            r#"{"location": "San Francisco", "unit": "celsius"}"#
        );
    }

    fn continue_rendered(
        prompt: dynamo_renderer::RenderedPrompt,
    ) -> dynamo_renderer::RenderedPrompt {
        super::apply_continue_final_message(prompt).unwrap()
    }

    #[test]
    fn continue_final_message_truncates_after_last_assistant_text() {
        use dynamo_renderer::RenderedPrompt;

        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "user text<|im_end|>LLM-Native Interaction{}<|im_end|><|im_start|>assistant",
            super::CONTINUE_FINAL_MESSAGE_TAG
        )));
        assert_eq!(
            rendered.as_str(),
            "user text<|im_end|>LLM-Native Interaction"
        );
    }

    #[test]
    fn continue_final_message_rstrips_when_template_trims_tag_space() {
        use dynamo_renderer::RenderedPrompt;

        // Transformers: if the template drops the marker's trailing space, also
        // rstrip the prefix (the template likely trimmed message trailing space).
        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "hello world{}extra",
            super::CONTINUE_FINAL_MESSAGE_TAG.trim_end()
        )));
        assert_eq!(rendered.as_str(), "hello world");
    }

    #[test]
    fn continue_final_message_marker_survives_repeated_last_text() {
        use dynamo_renderer::RenderedPrompt;

        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "LLM-Native Interaction in the user turn. LLM-Native Interaction{}<|im_end|>",
            super::CONTINUE_FINAL_MESSAGE_TAG
        )));
        assert_eq!(
            rendered.as_str(),
            "LLM-Native Interaction in the user turn. LLM-Native Interaction"
        );
    }

    #[test]
    fn continue_final_message_appends_tag_to_last_array_text_part() {
        let mut messages = serde_json::json!([{
            "role": "assistant",
            "content": [
                {"type": "text", "text": "ignored"},
                {"type": "text", "text": "Design"}
            ]
        }]);
        super::append_continue_final_message_tag(&mut messages, None).unwrap();
        assert_eq!(
            messages[0]["content"][1]["text"].as_str().unwrap(),
            format!("Design{}", super::CONTINUE_FINAL_MESSAGE_TAG)
        );
        assert_eq!(messages[0]["content"][0]["text"], "ignored");
    }

    #[test]
    fn continue_final_message_appends_tag_to_developer_array_text_part() {
        let mut messages = serde_json::json!([{
            "role": "developer",
            "content": [
                {"type": "text", "text": "ignored"},
                {"type": "text", "text": "Design"}
            ]
        }]);
        super::append_continue_final_message_tag(&mut messages, None).unwrap();
        assert_eq!(
            messages[0]["content"][1]["text"].as_str().unwrap(),
            format!("Design{}", super::CONTINUE_FINAL_MESSAGE_TAG)
        );
    }

    #[test]
    fn continue_final_message_continues_final_user_message() {
        use dynamo_renderer::RenderedPrompt;

        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "hello world{}extra",
            super::CONTINUE_FINAL_MESSAGE_TAG
        )));
        assert_eq!(rendered.as_str(), "hello world");
    }

    /// Raw turns `same / previous / same`; rendered last turn is `SAME`.
    /// Searching for `same` would cut at the first copy.
    #[test]
    fn continue_final_message_marker_survives_rewritten_final_turn() {
        use dynamo_renderer::RenderedPrompt;

        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "same|PREVIOUS|SAME{}|closed",
            super::CONTINUE_FINAL_MESSAGE_TAG
        )));
        assert_eq!(rendered.as_str(), "same|PREVIOUS|SAME");
    }

    /// Whitespace-only last content must not match earlier structural spaces.
    #[test]
    fn continue_final_message_whitespace_only_does_not_match_structural_spaces() {
        use dynamo_renderer::RenderedPrompt;

        let mut messages = serde_json::json!([
            {"role": "user", "content": "hello"},
            {"role": "assistant", "content": "   "}
        ]);
        super::append_continue_final_message_tag(&mut messages, None).unwrap();
        assert_eq!(
            messages[1]["content"].as_str().unwrap(),
            format!("   {}", super::CONTINUE_FINAL_MESSAGE_TAG)
        );

        let rendered = continue_rendered(RenderedPrompt::text(format!(
            "hello   world   {}extra",
            super::CONTINUE_FINAL_MESSAGE_TAG
        )));
        assert_eq!(rendered.as_str(), "hello   world   ");
    }

    #[test]
    fn continue_final_message_errors_when_marker_is_missing() {
        use dynamo_renderer::RenderedPrompt;

        let err =
            super::apply_continue_final_message(RenderedPrompt::text("unchanged".to_string()))
                .unwrap_err();
        assert!(
            err.to_string()
                .contains("not found in the rendered chat template"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn continue_final_message_errors_when_final_content_is_empty() {
        let mut messages = serde_json::json!([{"role": "assistant", "content": ""}]);
        let err = super::append_continue_final_message_tag(&mut messages, None).unwrap_err();
        assert!(
            err.to_string()
                .contains("not found in the rendered chat template"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn continue_final_message_preserves_segment_boundaries() {
        use dynamo_renderer::{RenderedPrompt, RenderedSegment};

        let rendered = continue_rendered(RenderedPrompt::segmented(vec![
            RenderedSegment {
                text: "<|im_start|>assistant\n".to_string(),
                allow_special: true,
            },
            RenderedSegment {
                text: format!(
                    "LLM-Native Interaction{}",
                    super::CONTINUE_FINAL_MESSAGE_TAG
                ),
                allow_special: false,
            },
            RenderedSegment {
                text: "<|im_end|>".to_string(),
                allow_special: true,
            },
            RenderedSegment {
                text: "<|im_start|>assistant\n".to_string(),
                allow_special: true,
            },
        ]));
        assert_eq!(
            rendered.as_str(),
            "<|im_start|>assistant\nLLM-Native Interaction"
        );
        let segments = rendered
            .segments()
            .expect("Kimi-style prompts must keep segment boundaries");
        assert_eq!(segments.len(), 2);
        assert!(segments[0].allow_special);
        assert_eq!(segments[0].text, "<|im_start|>assistant\n");
        assert!(!segments[1].allow_special);
        assert_eq!(segments[1].text, "LLM-Native Interaction");
    }

    #[test]
    fn continue_final_message_truncates_inside_an_ordinary_segment() {
        use dynamo_renderer::{RenderedPrompt, RenderedSegment};

        let rendered = continue_rendered(RenderedPrompt::segmented(vec![
            RenderedSegment {
                text: "<ctrl>".to_string(),
                allow_special: true,
            },
            RenderedSegment {
                text: format!("hello world{}extra", super::CONTINUE_FINAL_MESSAGE_TAG),
                allow_special: false,
            },
        ]));
        assert_eq!(rendered.as_str(), "<ctrl>hello world");
        let segments = rendered
            .segments()
            .expect("truncated prompt stays segmented");
        assert_eq!(segments.len(), 2);
        assert!(segments[0].allow_special);
        assert_eq!(segments[1].text, "hello world");
        assert!(!segments[1].allow_special);
    }
}