Testing BAML Functions
BAML provides powerful testing capabilities built into the language and VSCode extension. Test your functions iteratively to refine prompts and validate outputs.Test Syntax
Tests are defined directly in BAML files using thetest block:
Test Anatomy
- Name:
test Test1- Unique identifier - Functions: Which function(s) to test
- Args: Input arguments matching the function signature
Running Tests
VSCode Playground
The BAML VSCode extension provides an interactive playground:- Open any
.bamlfile with a test - Click “Run Test” above the test block
- View results in the playground panel
- See the rendered prompt and API request
Command Line
Run tests from your terminal:Test Arguments
Simple Arguments
For primitive types:Complex Objects
For classes, use dictionary syntax:Arrays
Test with arrays of values:Multi-line Strings
Use#"..."# for multi-line string arguments:
Testing Multimodal Inputs
BAML supports testing with images, audio, PDFs, and video:Image Inputs
- File
- URL
- Base64
Audio Inputs
PDF Inputs
Video Inputs
Assertions and Checks
Validate test outputs using@@assert and @@check:
Assertions
Hard requirements that must pass:this- The function result_.result- Same asthis_.latency_ms- Time taken in milliseconds_.checks.$NAME- Results of earlier checks
Checks
Soft validations that can fail without stopping the test:Complex Validations
Dynamic Types in Tests
Modify dynamic types for specific tests:Testing Multiple Clients
Test the same function with different models:Test Organization
Organize tests for maintainability:Group Related Tests
Use Descriptive Names
Production Builds
Exclude tests from production builds to reduce bundle size:baml_client while keeping all functions intact.
Best Practices
- Test edge cases: Empty inputs, missing fields, unusual formatting
- Use assertions: Validate outputs programmatically
- Test with real data: Use actual examples from your domain
- Compare models: Test the same input with different LLMs
- Keep tests updated: Update tests when you change prompts
- Use descriptive names: Make it clear what each test validates
- Test multimodal inputs: Verify image/audio/video handling
- Run tests frequently: Catch regressions early
- Use checks for soft requirements: Not all validations need to fail the test
- Version control your tests: Commit
.bamlfiles with tests to Git
Debugging Failed Tests
When a test fails:- Check the Prompt Preview: Verify the rendered prompt looks correct
- View the Raw Response: See what the LLM actually returned
- Check the cURL Request: Ensure API parameters are correct
- Add logging: Use checks to inspect intermediate values
- Simplify the input: Start with a minimal test case
- Try a different model: Some models handle certain tasks better
Example: Comprehensive Test Suite
Next Steps
Functions
Learn about BAML functions
CLI Test Reference
Complete CLI testing documentation
Testing
Advanced validation techniques
Dynamic Types
Test with dynamic types