BAML provides a set of error classes for handling different error scenarios when working with LLMs. Each error type is designed to handle specific failure cases in the BAML runtime.

Error Class Hierarchy

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

Type Hierarchy
1// Base JavaScript Error class
2class Error {
3 message: string
4 name: string
5 stack?: string
6}
7
8// BAML-specific error classes
9class BamlValidationError extends Error {
10 type: 'BamlValidationError'
11 message: string
12 prompt: string
13 raw_output: string
14}
15
16class BamlClientFinishReasonError extends Error {
17 type: 'BamlClientFinishReasonError'
18 message: string
19 prompt: string
20 raw_output: string
21}

Error Types

BamlValidationError

Thrown when BAML fails to parse or validate LLM output. Contains the original prompt and raw output for debugging.

BamlClientFinishReasonError

Thrown when an LLM terminates with a disallowed finish reason. Includes the original prompt and partial output received before termination.

Type Guards

All BAML errors can be identified using TypeScript’s instanceof operator:

Type Checking
1try {
2 // BAML operation
3} catch (error) {
4 if (error instanceof BamlValidationError) {
5 // Handle validation error
6 } else if (error instanceof BamlClientFinishReasonError) {
7 // Handle finish reason error
8 }
9}

Common Properties

All BAML error classes include:

type
string

Literal type identifier specific to each error class.

message
string

Human-readable error message describing the failure.

For detailed information about each error type, refer to their individual reference pages.

Built with