Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support parsing primitive values from single-key objects #1224

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions engine/baml-lib/jsonish/src/deserializer/coercer/coerce_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use internal_baml_core::ir::FieldType;
use crate::{
deserializer::{
coercer::{coerce_primitive::coerce_bool, match_string::match_string, TypeCoercer},
deserialize_flags::{DeserializerConditions, Flag},
types::BamlValueWithFlags,
},
jsonish,
Expand Down Expand Up @@ -46,6 +47,22 @@ impl TypeCoercer for LiteralValue {
Some(v) => v,
};

// If we get an object with a single key-value pair, try to extract the value
if let jsonish::Value::Object(obj) = value {
if obj.len() == 1 {
let (key, inner_value) = obj.iter().next().unwrap();
// only extract value if it's a primitive (not an object or array, hoping to god its fixed)
match inner_value {
jsonish::Value::Number(_) | jsonish::Value::Boolean(_) | jsonish::Value::String(_) => {
let mut result = self.coerce(ctx, target, Some(inner_value))?;
result.add_flag(Flag::ImpliedKey(key.clone()));
return Ok(result);
hellovai marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {}
}
}
}

match literal {
LiteralValue::Int(literal_int) => {
let BamlValueWithFlags::Int(coerced_int) = coerce_int(ctx, target, Some(value))?
Expand Down
134 changes: 134 additions & 0 deletions engine/baml-lib/jsonish/src/tests/test_literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,137 @@ test_deserializer!(
]),
"TWO"
);

test_deserializer!(
test_union_literal_with_multiple_types_from_object,
EMPTY_FILE,
r#"{
"status": 1
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
1
);

// Test with integer value
test_deserializer!(
test_union_literal_with_multiple_types_from_object_int,
EMPTY_FILE,
r#"{
"status": 1
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
1
);

// Test with boolean value
test_deserializer!(
test_union_literal_with_multiple_types_from_object_bool,
EMPTY_FILE,
r#"{
"result": true
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
true
);

// Test with string value
test_deserializer!(
test_union_literal_with_multiple_types_from_object_string,
EMPTY_FILE,
r#"{
"value": "THREE"
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
"THREE"
);

// Test with object that has multiple keys (should fail)
test_failing_deserializer!(
test_union_literal_with_multiple_types_from_multi_key_object,
EMPTY_FILE,
r#"{
"status": 1,
"message": "success"
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
])
);

// Test with nested object (should fail)
test_failing_deserializer!(
test_union_literal_with_multiple_types_from_nested_object,
EMPTY_FILE,
r#"{
"status": {
"code": 1
}
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
])
);

// Test with quoted string value
test_deserializer!(
test_union_literal_with_multiple_types_from_object_quoted_string,
EMPTY_FILE,
r#"{
"value": "\"THREE\""
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
"THREE"
);

// Test with string value and extra text
test_deserializer!(
test_union_literal_with_multiple_types_from_object_string_extra,
EMPTY_FILE,
r#"{
"value": "The answer is THREE"
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
]),
"THREE"
);

// Test with array value (should fail)
test_failing_deserializer!(
test_union_literal_with_multiple_types_from_object_array,
EMPTY_FILE,
r#"{
"values": [1]
}"#,
FieldType::Union(vec![
FieldType::Literal(LiteralValue::Int(1)),
FieldType::Literal(LiteralValue::Bool(true)),
FieldType::Literal(LiteralValue::String("THREE".into())),
])
);
Loading