baml icon indicating copy to clipboard operation
baml copied to clipboard

Unable to reference BamlValidationError in Python, referenced in docs

Open htxryan opened this issue 1 year ago • 5 comments

I'm unable to figure out how to reference (and therefore catch/handle) BamlValidationError in Python, which is referenced in the docs here: https://docs.boundaryml.com/ref/baml_client/errors/baml-validation-error

How can I catch this specific error in Python?

try:
  return b.MyFunction("input")
except BamlValidationError as e:
  # handling logic

htxryan avatar Apr 22 '25 15:04 htxryan

Looking closer I do see that these docs only make reference to TypeScript, not Python. Is there a reason these specific error types are not available in Python? Are there any specific BAML error types available in Python (to avoid a generic except Exception as e:)?

htxryan avatar Apr 22 '25 15:04 htxryan

Could you try

from baml_py.errors import BamlValidationError

Elijas avatar Apr 23 '25 01:04 Elijas

That worked, thank you! For some reason VS Code was not able to find it to automatically add the import, not quite sure why. But this unblocks me, thank you 😄

htxryan avatar Apr 23 '25 13:04 htxryan

Glad it helped! For me it also doesn’t’autofind with Ctrl+dot, unfortunately i had to manually find it.

Let us know if you figure it out💪

Elijas avatar Apr 23 '25 14:04 Elijas

@htxryan Btw sometimes ctrl+dot issues are solved by restarting the language server, just in case it helps

Elijas avatar May 16 '25 00:05 Elijas