BamlValidationError
Thrown when BAML fails to parse or validate LLM output against the expected schema.
Type Definition
class BamlValidationError extends Error {
type: 'BamlValidationError'
message: string
prompt: string
raw_output: string
detailed_message: string
}
Properties
| Property | Type | Description |
|---|---|---|
type | 'BamlValidationError' | Literal type identifier |
message | string | Specific validation failure description |
prompt | string | Original prompt sent to LLM |
raw_output | string | Raw LLM output that failed validation |
detailed_message | string | Complete history of failed attempts (for fallback/retry) |
Detection
- TypeScript
- Python
import { BamlValidationError } from '@boundaryml/baml'
try {
const result = await b.ExtractUser(text)
} catch (error) {
if (error instanceof BamlValidationError) {
console.log('Validation failed:', error.message)
console.log('Raw output:', error.raw_output)
console.log('Prompt used:', error.prompt)
}
}
from baml_py import BamlValidationError
try:
result = await b.ExtractUser(text)
except BamlValidationError as e:
print(f"Validation failed: {e}")
print(f"Raw output: {e.raw_output}")
print(f"Prompt used: {e.prompt}")
Common Causes
- LLM output doesn't match expected JSON structure
- Missing required fields in output
- Type mismatches (e.g., string instead of number)
- Enum values not in allowed list
- Array/object structure doesn't match schema
Debugging Tips
- Check
raw_outputto see what the LLM actually returned - Review
promptto ensure output format instructions are clear - Use
detailed_messagewhen debugging fallback chains