Back to Baml

BamlClientFinishReasonError

fern/03-reference/baml_client/errors/baml-client-finish-reason-error.mdx

0.222.02.0 KB
Original Source

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:

<CodeBlocks> ```baml 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"] } } ``` </CodeBlocks>

Type Definition

<CodeBlocks> ```typescript Type Definition class BamlClientFinishReasonError extends Error { type: 'BamlClientFinishReasonError' message: string prompt: string raw_output: string detailed_message: string } ``` </CodeBlocks>

Properties

<ParamField path="type" type="'BamlClientFinishReasonError'"

Literal type identifier for the error class. </ParamField>

<ParamField path="message" type="string"

Error message describing the specific finish reason that caused the termination. </ParamField>

<ParamField path="prompt" type="string"

The original prompt sent to the LLM. </ParamField>

<ParamField path="raw_output" type="string"

The partial output received from the LLM before termination. </ParamField>

<ParamField path="detailed_message" type="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. </ParamField>

Type Guards

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

<CodeBlocks> ```typescript Type Check if (error instanceof BamlClientFinishReasonError) { // Handle finish reason error } ``` </CodeBlocks>