To set a value to an environment variable, use the following syntax:

1env.YOUR_VARIABLE_NAME
Environment variables with spaces in their names are not supported.

Example

Using an environment variable for API key:

1client<llm> MyCustomClient {
2 provider "openai"
3 options {
4 model "gpt-4o-mini"
5 // Set the API key using an environment variable
6 api_key env.MY_SUPER_SECRET_API_KEY
7 }
8}

Setting Environment Variables

To set environment variables:

Once you open a .baml file, in VSCode, you should see a small button over every BAML function: Open Playground.

Then you should be able to set environment variables in the settings tab.

Or type BAML Playground in the VSCode Command Bar (CMD + Shift + P or CTRL + Shift + P) to open the playground.

BAML will expect these to be set already in your program before you import the baml_client in Python/ TS / etc.

Any of the following strategies for setting env vars are compatible with BAML:

  • setting them in your shell before running your program
  • in your Dockerfile
  • in your next.config.js
  • in your Kubernetes manifest
  • from secrets-store.csi.k8s.io
  • from a secrets provider such as Infisical / Doppler
  • from a .env file (using dotenv cli)
  • using account credentials for ephemeral token generation (e.g. Vertex AI Auth Tokens)
$export MY_SUPER_SECRET_API_KEY="..."
>python my_program_using_baml.py

Requires BAML Version 0.57+

If you don’t want BAML to try to auto-load your env vars, you can call manually reset_baml_env_vars with the current environment variables.

1from baml_client import b
2from baml_client import reset_baml_env_vars
3import os
4import dotenv
5
6dotenv.load_dotenv()
7reset_baml_env_vars(dict(os.environ))

Error Handling

Errors for unset environment variables are only thrown when the variable is accessed. If your BAML project has 15 environment variables and 1 is used for the function you are calling, only that one environment variable will be checked for existence.

Built with