Set Environment Variables

Environment Variables in BAML

Sometimes you’ll see environment variables used in BAML, like in clients:

1client<llm> GPT4o {
2 provider baml-openai-chat
3 options {
4 model gpt-4o
5 api_key env.OPENAI_API_KEY
6 }
7}

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
1from dotenv import load_dotenv
2from baml_client import b
3
4load_dotenv()

Setting LLM API Keys per Request

You can set the API key for an LLM dynamically by passing in the key as a header or as a parameter (depending on the provider), using the ClientRegistry.