Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function SimpleFunction(input: string) -> string {
client "openai/gpt-3.5-turbo"
prompt #"Return hello {{input}}"#
}

test TestFieldLevelAssert {
functions [SimpleFunction]
args {
input "test"
}
@assert(this == "Hello, test!") // This should error
}

// error: Error parsing attribute "assert": Test assertions must use block-level syntax '@@assert' instead of '@assert'. Example:
// test MyTest {
// functions [MyFunc]
// args {}
// @@assert(label, {{ this == "expected" }})
// }
// --> tests/field_level_assert.baml:11
// |
// 10 | }
// 11 | @assert(this == "Hello, test!") // This should error
// |
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function ComplexFunction(data: string) -> string {
client "openai/gpt-3.5-turbo"
prompt #"Process {{data}}"#
}

test TestFieldLevelCheck {
functions [ComplexFunction]
args {
data "value"
}
@check(this != null) // Should error - use @@check instead
}

// error: Error parsing attribute "check": Test checks must use block-level syntax '@@check' instead of '@check'. Block-level attributes apply to the entire test result.
// --> tests/field_level_check.baml:11
// |
// 10 | }
// 11 | @check(this != null) // Should error - use @@check instead
// |
38 changes: 38 additions & 0 deletions engine/baml-lib/parser-database/src/types/configurations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,44 @@ pub(crate) fn visit_test_case<'db>(
)),
});

// Add validation for field-level attributes that shouldn't be in test blocks
for field in &config.fields {
// Check if the field has any attributes
for attribute in &field.attributes {
let attr_name = attribute.name.name();

// Check for constraint attributes that should be block-level
if matches!(attr_name, "assert" | "check") {
let error_msg = match attr_name {
"assert" => format!(
"Test assertions must use block-level syntax '@@assert' instead of '@assert'. \
Example:\n test MyTest {{\n functions [MyFunc]\n args {{}}\n \
@@assert(label, {{{{ this == \"expected\" }}}})\n }}"
),
"check" => format!(
"Test checks must use block-level syntax '@@check' instead of '@check'. \
Block-level attributes apply to the entire test result."
),
_ => unreachable!(),
};

ctx.push_error(DatamodelError::new_attribute_validation_error(
&error_msg,
attr_name,
attribute.span.clone(),
));
}

// Also check for other field-only attributes that don't make sense in tests
if matches!(attr_name, "description" | "alias" | "skip") {
ctx.push_error(DatamodelError::new_attribute_not_known_error(
attr_name,
attribute.span.clone(),
));
}
}
}

let constraints: Vec<(Constraint, Span, Span)> = config
.attributes
.iter()
Expand Down