dynamo-renderer 8.1.0

Standalone OpenAI chat-template / prompt formatting (HF chat_template via minijinja).
Documentation
// 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, StringInput, from_args};
use minijinja::{Error, ErrorKind, Output, State, Value, escape_formatter};
use serde::{Deserialize, Serialize};

use crate::python::{
    PyFloats, 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>,
}

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()),
        }
    }
}

#[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=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). serde_json leaves non-ASCII unescaped by
/// default, matching `ensure_ascii=False`.
pub fn tojson(value: Value, kwargs: Kwargs) -> Result<Value, Error> {
    let mut buf = Vec::new();
    let result = if let Ok(indent) = kwargs.get("indent") {
        // Python `json.dumps(indent=n)` separators are `(",", ": ")` with the
        // item separator followed by newline + indent — PrettyFormatter matches.
        let repeat = b" ".repeat(indent);
        let formatter = PyFloats(serde_json::ser::PrettyFormatter::with_indent(&repeat));
        let mut serializer = serde_json::Serializer::with_formatter(&mut buf, formatter);
        value.serialize(&mut serializer)
    } else {
        let mut serializer = serde_json::Serializer::with_formatter(&mut buf, PyJsonFormatter);
        value.serialize(&mut serializer)
    };
    result.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)
}

/// 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(Value::from_serialize(&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 = Value::from_serialize(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");
    }
}