Skip to main content

BAML Error Types

BAML provides error classes for handling different error scenarios when working with LLMs.

Error Class Hierarchy​

All BAML errors extend the base Error class and include a literal type field.

// BAML-specific error classes
class BamlValidationError extends Error {
type: 'BamlValidationError'
message: string
prompt: string
raw_output: string
detailed_message: string
}

class BamlClientFinishReasonError extends Error {
type: 'BamlClientFinishReasonError'
message: string
prompt: string
raw_output: string
detailed_message: string
}

class BamlAbortError extends Error {
type: 'BamlAbortError'
message: string
reason?: any
detailed_message: string
}

Error Types​

ErrorDescription
BamlValidationErrorFailed to parse or validate LLM output
BamlClientFinishReasonErrorLLM terminated with disallowed finish reason
BamlAbortErrorOperation cancelled via abort controller

Fallback Error Aggregation​

When using fallback clients or clients with retry policies, BAML attempts multiple client calls before failing:

  • The error type corresponds to the final (last) failed attempt
  • The message field contains the error message from the final attempt
  • The detailed_message field contains the complete history of all failed attempts

Type Guards​

import { BamlAbortError, BamlValidationError, BamlClientFinishReasonError } from '@boundaryml/baml'

try {
const result = await b.MyFunction(input)
} catch (error) {
if (error instanceof BamlAbortError) {
// Handle cancellation
} else if (error instanceof BamlValidationError) {
// Handle validation error
} else if (error instanceof BamlClientFinishReasonError) {
// Handle finish reason error
}
}

Common Properties​

PropertyTypeDescription
typestringLiteral type identifier for each error class
messagestringHuman-readable error message
detailed_messagestringComplete history of all failed attempts when using fallback/retry