config (logging / environment variables)

Various settings are configurable via environment variables.

SettingEnvironment VariableDescriptionDefault
Logging LevelBAML_LOGThe logging level to use (INFO, DEBUG, TRACE, WARN, ERROR, OFF)INFO
Text / JSON ModeBAML_LOG_JSONWhether to log in JSON format or human-readable format (1, 0)0
Max Log Chunk SizeBAML_LOG_MAX_MESSAGE_LENGTHHow large of a prompt / response will be logged (0 for no limit)64000
Log Color ModeBAML_LOG_COLOR_MODEWhether to color the log output (auto, always, never)auto

Setting can also be modified via functions in baml_client.config.

1from baml_client.config import set_from_env, set_log_level,
2 set_log_json_mode, set_log_max_message_length,
3 get_log_level, reset_baml_env_vars

set_log_level

Environment variable: BAML_LOG

1def set_log_level(level: "INFO" | "DEBUG" | "TRACE" | "WARN" | "ERROR" | "OFF"):
2 ...

set_log_json_mode

Environment variable: BAML_LOG_JSON

Switches the log output between JSON and human-readable format.

1def set_log_json_mode(enable: bool):

set_log_max_message_length

0 for unlimited

Environment variable: BAML_LOG_MAX_MESSAGE_LENGTH

1def set_log_max_message_length(length: int):

get_log_level

1def get_log_level() -> "INFO" | "DEBUG" | "TRACE" | "WARN" | "ERROR" | "OFF":

reset_baml_env_vars

Resets the environment variables to the values in the provided dictionary. Will also reset any logging related environment variables to those passed in (if set explicitly).

1def reset_baml_env_vars(env: Dict[str, str]):

Setting Environment Variables

In the VSCode Playground

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.

VSCode Code Lens

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

For Your App (Default)

BAML will do its best to load environment variables from your program. 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)
  • python-dotenv package in Python or dotenv package in Node.js
$export MY_SUPER_SECRET_API_KEY="..."
>python my_program_using_baml.py

Warning: If you are using the python-dotenv package, please check the import order for some edge cases.

1import dotenv
2from baml_client import b
3# This will work regardless of import order, since dotenv is imported as a module
4dotenv.load_dotenv()
1from baml_client import b
2from dotenv import load_dotenv
3# This will work since load_dotenv is imported after baml_client
4load_dotenv()
1from dotenv import load_dotenv
2from baml_client import b
3# This will not work as expected since baml_client is imported AFTER load_dotenv
4load_dotenv()

For Your App (Manually)

Requires BAML Version 0.57+

Reset the environment variables to those manually set in your code.

1from baml_client import b
2from baml_client.config import reset_baml_env_vars
3
4# This will load the environment variables from the .env file
5reset_baml_env_vars({ "OPENAI_API_KEY": "sk-super-secret-key" })