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.

Import path

1from baml_py.errors import BamlError, BamlInvalidArgumentError, BamlClientError, BamlClientHttpError, BamlValidationError

BamlError

Base class for all BAML exceptions.

BamlInvalidArgumentError

Subclass of BamlError.

Raised when one or multiple arguments to a function are invalid.

When using BAML-over-HTTP a.k.a. OpenAPI, this is 400 Bad Request.

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.

When using BAML-over-HTTP a.k.a. OpenAPI, this is 502 Bad Gateway.

BamlClientHttpError

Subclass of BamlClientError.

Raised when BAML successfully makes an HTTP request to an LLM provider, but the provider returns a non-200 status code.

BamlValidationError

Subclass of BamlError.

Raised when BAML fails to parse a string from the LLM into the specified object.

When using BAML-over-HTTP a.k.a. OpenAPI, this is 500 Internal Server Error (we expect to use a more specific status code here soon).