Skip to content

Commit d25d3eb

Browse files
authored
bugfix: media types in jinja should evaluate to true in jinja bool conversions (#2384)
- **bugfix: media types in jinja should evaluate to true for comparisons** - **cleaner tests** <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Fixes media type evaluation in Jinja templates to always be true and adds tests for media object behavior. > > - **Behavior**: > - Fixes bug where media types in Jinja templates did not evaluate to true in comparisons by implementing `is_true` method in `MinijinjaBamlMedia`. > - **Tests**: > - Adds `test_media.rs` to test media object evaluation in Jinja templates. > - Includes tests for media object string representation and boolean evaluation. > - **Misc**: > - Makes `MinijinjaBamlMedia` struct `pub(crate)` in `baml_value_to_jinja_value.rs`. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for 02f8f33. You can [customize](https://app.ellipsis.dev/BoundaryML/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 52bdbf0 commit d25d3eb

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎engine/baml-lib/jinja-runtime/src/baml_value_to_jinja_value.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl IntoMiniJinjaValue for BamlValue {
8484
}
8585
}
8686

87-
struct MinijinjaBamlMedia {
87+
pub(crate) struct MinijinjaBamlMedia {
8888
media: BamlMedia,
8989
}
9090

@@ -136,6 +136,10 @@ impl minijinja::value::Object for MinijinjaBamlMedia {
136136
))
137137
}
138138

139+
fn is_true(self: &Arc<Self>) -> bool {
140+
true
141+
}
142+
139143
fn render(self: &Arc<Self>, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140144
std::fmt::Display::fmt(self, f)
141145
}

‎engine/baml-lib/jinja-runtime/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod output_format;
77
mod test_enum_comparison;
88
#[cfg(test)]
99
mod test_enum_template;
10+
#[cfg(test)]
11+
mod test_media;
1012
use indexmap::IndexMap;
1113
use internal_baml_core::ir::{jinja_helpers::get_env, repr::IntermediateRepr};
1214
pub use output_format::types;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use baml_types::BamlMedia;
2+
use minijinja::{context, value::Value, Environment};
3+
4+
use crate::baml_value_to_jinja_value::MinijinjaBamlMedia;
5+
6+
#[test]
7+
fn test_media_value() {
8+
// Test that a media object evaluates to the raw string representation
9+
let media_val = Value::from_object(MinijinjaBamlMedia::from(BamlMedia::url(
10+
baml_types::BamlMediaType::Image,
11+
"https://example.com/image.png".to_string(),
12+
None,
13+
)));
14+
15+
let template = r#"
16+
{{ media }}
17+
"#;
18+
let mut env = Environment::new();
19+
20+
env.add_template("test1", template).unwrap();
21+
let tmpl = env.get_template("test1").unwrap();
22+
let output = tmpl.render(context! { media => media_val }).unwrap();
23+
assert_eq!(output.trim(), "BAML_MEDIA_MAGIC_STRING_DELIMITER:baml-start-media:{\"media_type\":\"Image\",\"mime_type\":null,\"content\":{\"Url\":{\"url\":\"https://example.com/image.png\"}}}:baml-end-media:BAML_MEDIA_MAGIC_STRING_DELIMITER");
24+
}
25+
26+
#[test]
27+
fn test_media_comparison() {
28+
// Test that a media object evaluates to true in boolean context
29+
let media_val = Value::from_object(MinijinjaBamlMedia::from(BamlMedia::url(
30+
baml_types::BamlMediaType::Image,
31+
"https://example.com/image.png".to_string(),
32+
None,
33+
)));
34+
35+
let template = r#"
36+
{{ media|bool }}
37+
"#;
38+
let mut env = Environment::new();
39+
40+
env.add_template("test1", template).unwrap();
41+
let tmpl = env.get_template("test1").unwrap();
42+
let output = tmpl.render(context! { media => media_val }).unwrap();
43+
assert_eq!(output.trim(), "true");
44+
}

0 commit comments

Comments
 (0)