Error Handling
When BAML raises an exception, it will be an instance of a subclass of BamlError
. This allows you to catch all BAML-specific exceptions with a single except
block.
Example
BamlError
Base class for all BAML exceptions.
A human-readable error message.
BamlInvalidArgumentError
Subclass of BamlError
.
Raised when one or multiple arguments to a function are invalid.
BamlClientError
Subclass of BamlError
.
Raised when a client fails to return a valid response.
In the case of aggregate clients like fallback
or those with retry_policy
, only the last client’s error is raised.
BamlClientHttpError
Subclass of BamlClientError
.
Raised when the HTTP request made by a client fails with a non-200 status code.
The status code of the response.
Common status codes are:
- 1: Other
- 2: Other
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
- 429: Too Many Requests
- 500: Internal Server Error
BamlValidationError
Subclass of BamlError
.
Raised when BAML fails to parse a string from the LLM into the specified object.
The raw text from the LLM that failed to parse into the expected return type of a function.
The parsing-related error message.
The original prompt that was sent to the LLM, formatted as a plain string. Images sent as base64-encoded strings are not serialized into this field.