Skip to content

Commit da202d1

Browse files
authored
[bug] Correctly parse enums when other json elements may exist in the string (#1913)
Fixes #1888 <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Fixes enum parsing in `field_type.rs` to handle JSON strings with additional elements, verified by new test in `test_enum.rs`. > > - **Behavior**: > - Fixes enum parsing in `coerce()` in `field_type.rs` to handle cases where other JSON elements exist in the string. > - Adds support for `FieldType::Enum(_)` and `FieldType::Literal(LiteralValue::String(_))` in `matches!` condition. > - **Tests**: > - Adds `test_weird_characters` in `test_enum.rs` to verify enum parsing with additional text and special characters. > > <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 bb03cfa. 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 2acb6d7 commit da202d1

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

engine/baml-lib/jsonish/src/deserializer/coercer/field_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use baml_types::{BamlMap, CompletionState, Constraint, ConstraintLevel};
2+
use baml_types::{BamlMap, CompletionState, Constraint, ConstraintLevel, LiteralValue};
33
use internal_baml_core::{ir::FieldType, ir::TypeValue};
44

55
use crate::deserializer::{
@@ -33,7 +33,7 @@ impl TypeCoercer for FieldType {
3333
scope = ctx.display_scope(),
3434
current = value.map(|v| v.r#type()).unwrap_or("<null>".into())
3535
);
36-
if matches!(target, FieldType::Primitive(TypeValue::String)) {
36+
if matches!(target, FieldType::Primitive(TypeValue::String) | FieldType::Enum(_) | FieldType::Literal(LiteralValue::String(_))) {
3737
self.coerce(
3838
ctx,
3939
target,

engine/baml-lib/jsonish/src/tests/test_enum.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,21 @@ test_failing_deserializer!(
293293
"The answer is not car or car-2!",
294294
FieldType::Enum("Car".to_string())
295295
);
296+
297+
test_deserializer!(
298+
test_weird_characters,
299+
r#"
300+
enum MessageType {
301+
SPAM
302+
NOT_SPAM
303+
}
304+
"#,
305+
r#"
306+
The text "Buy cheap watches now! Limited time offer!!!" is typically characterized by unsolicited
307+
offers and urgency ($^{$_{Ω}$rel}$), which are common traits of spam messages. Therefore, it should be classified as:
308+
309+
- **SPAM**
310+
"#,
311+
FieldType::Enum("MessageType".to_string()),
312+
"SPAM"
313+
);

0 commit comments

Comments
 (0)