BamlClientFinishReasonError

The BamlClientFinishReasonError class represents an error that occurs when an LLM terminates with a disallowed finish reason.

You can allow or disallow finish reasons like this:

client<llm> OpenAIWithFinishReasonError {
provider openai
options {
api_key env.OPENAI_API_KEY
model "gpt-4"
// make it very small so model will stop early
max_tokens 10
// throws if the model returns any other finish reason
finish_reason_allow_list ["stop"]
// or allow all finish reasons except length
// finish_reason_deny_list ["length"]
}
}

Type Definition

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

Properties

type
'BamlClientFinishReasonError'

Literal type identifier for the error class.

message
string

Error message describing the specific finish reason that caused the termination.

prompt
string

The original prompt sent to the LLM.

raw_output
string

The partial output received from the LLM before termination.

detailed_message
string

Comprehensive error information that includes the complete history of all failed attempts when using fallback clients or retry policies. When multiple attempts are made (via fallback or retry), this field contains formatted details about each failed attempt, making it invaluable for debugging complex client configurations.

Type Guards

The error can be identified using TypeScript’s instanceof operator:

Type Check
if (error instanceof BamlClientFinishReasonError) {
// Handle finish reason error
}