From 5e9a15d9e48011e3bd224d7b8cf619e8048b4ab0 Mon Sep 17 00:00:00 2001 From: Edward Guy Capriolo Date: Sat, 5 Dec 2026 16:38:59 -0500 Subject: [PATCH] jinjava default filter converts boolean defaults to strings --- .../jinjava/lib/filter/DefaultFilter.java | 6 +-- .../jinjava/SparkChatTemplateTest.java | 54 +++++++++++++++++++ src/test/resources/spark_chat_template.jinja | 1 + 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/hubspot/jinjava/SparkChatTemplateTest.java create mode 100644 src/test/resources/spark_chat_template.jinja diff --git a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java index d93170d6a..6b87bb45c 100644 --- a/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java +++ b/src/main/java/com/hubspot/jinjava/lib/filter/DefaultFilter.java @@ -20,10 +20,8 @@ import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; import com.hubspot.jinjava.interpret.JinjavaInterpreter; import com.hubspot.jinjava.interpret.TemplateSyntaxException; -import com.hubspot.jinjava.objects.PyWrapper; import com.hubspot.jinjava.util.ObjectTruthValue; import java.util.Map; -import java.util.Objects; @JinjavaDoc( value = "If the value is undefined it will return the passed default value, otherwise the value of the variable", @@ -87,9 +85,7 @@ public Object filter( return object; } - return (defaultValue instanceof PyWrapper) || (defaultValue == null) - ? defaultValue - : Objects.toString(defaultValue); + return defaultValue; } @Override diff --git a/src/test/java/com/hubspot/jinjava/SparkChatTemplateTest.java b/src/test/java/com/hubspot/jinjava/SparkChatTemplateTest.java new file mode 100644 index 000000000..41d6a259c --- /dev/null +++ b/src/test/java/com/hubspot/jinjava/SparkChatTemplateTest.java @@ -0,0 +1,54 @@ +package com.hubspot.jinjava; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +public class SparkChatTemplateTest { + + private static final String PROMPT = "What is the capital of New York, USA?"; + + @Test + public void matchesPythonSparkDefaultsAndReasoningEfforts() throws IOException { + String template; + try ( + InputStream stream = getClass().getResourceAsStream("/spark_chat_template.jinja") + ) { + if (stream == null) { + throw new IOException("Missing Spark chat template test resource"); + } + template = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + } + + Map base = new HashMap<>(); + base.put("messages", List.of(Map.of("role", "user", "content", PROMPT))); + base.put("add_generation_prompt", true); + + String pythonHigh = "<|start▁of▁sentence|><|System|>\n" + + "you are a helpful assistant.\n" + + "`thinking_effort` guides on how much to think in your thinking channel " + + "(not including the response channel), supported values include `low` and `high`.\n" + + "Now the system is invoked with `thinking_effort=high`." + + "<|end▁of▁sentence|><|start▁of▁sentence|><|User|>" + + PROMPT + + "<|end▁of▁sentence|><|start▁of▁sentence|><|Bot|>"; + String pythonLow = pythonHigh.replace("thinking_effort=high", "thinking_effort=low"); + + Map high = new HashMap<>(base); + high.put("enable_thinking", true); + high.put("reasoning_effort", "high"); + Map low = new HashMap<>(base); + low.put("enable_thinking", true); + low.put("reasoning_effort", "low"); + + assertThat(new Jinjava().render(template, high)).isEqualTo(pythonHigh); + assertThat(new Jinjava().render(template, low)).isEqualTo(pythonLow); + assertThat(new Jinjava().render(template, base)).isEqualTo(pythonHigh); + } +} diff --git a/src/test/resources/spark_chat_template.jinja b/src/test/resources/spark_chat_template.jinja new file mode 100644 index 000000000..74ead77be --- /dev/null +++ b/src/test/resources/spark_chat_template.jinja @@ -0,0 +1 @@ +{%- if not messages %}{{- raise_exception('No messages provided.') }}{%- endif %}{%- set enable_thinking = enable_thinking | default(true) %}{%- set reasoning_instructions = '' %}{%- if enable_thinking is true %}{%- set resolved_reasoning_effort = reasoning_effort|default('high') %}{%- if resolved_reasoning_effort not in ('high', 'low') %}{{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported values are high (default) and low.') }}{%- endif %}{%- if resolved_reasoning_effort == 'high' %}{%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=high`.' %}{%- elif resolved_reasoning_effort == 'low' %}{%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=low`.' %}{%- endif %}{%- endif %}{%- set default_system = "you are a helpful assistant." + reasoning_instructions %}{{- '<|start▁of▁sentence|><|System|>' + '\n' + default_system }}{{- '<|end▁of▁sentence|>' }}{%- for message in messages %}{%- if message.role == "user" %}{{- '<|start▁of▁sentence|><|User|>' + message.content + '<|end▁of▁sentence|>' }}{%- endif %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|start▁of▁sentence|><|Bot|>' }}{%- if enable_thinking is defined and enable_thinking %}{{- '' }}{%- endif %}{%- if enable_thinking is defined and not enable_thinking %}{{- '' }}{%- endif %}{%- endif -%}