Skip to main content

BamlClientFinishReasonError

Thrown when an LLM terminates with a disallowed finish reason.

Configuration​

Control allowed finish reasons in your client definition:

client<llm> OpenAIWithFinishReasonError {
provider openai
options {
api_key env.OPENAI_API_KEY
model "gpt-4"
max_tokens 10 // Small limit to trigger early stop
// Throws if model returns any other finish reason
finish_reason_allow_list ["stop"]
// Or deny specific reasons
// finish_reason_deny_list ["length"]
}
}

Type Definition​

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

Properties​

PropertyTypeDescription
type'BamlClientFinishReasonError'Literal type identifier
messagestringFinish reason that caused termination
promptstringOriginal prompt sent to LLM
raw_outputstringPartial output received before termination
detailed_messagestringComplete history of failed attempts (for fallback/retry)

Detection​

import { BamlClientFinishReasonError } from '@boundaryml/baml'

try {
const result = await b.ExtractUser(text)
} catch (error) {
if (error instanceof BamlClientFinishReasonError) {
console.log('Finish reason error:', error.message)
console.log('Partial output:', error.raw_output)
}
}

Common Finish Reasons​

ReasonDescription
stopNatural completion
lengthHit max_tokens limit
content_filterContent blocked by safety filters
tool_callsModel wants to call a tool

See Also​