BAML with Python/TS/Ruby

Set Environment Variables

The generated BAML client will capture all environment variables when you import it, and will not be able to see any environment variables you load after importing the BAML client.

Any of the following strategies are compatible with BAML:

  • set environment variables in your Dockerfile
  • set environment variables in your next.config.js
  • set environment variables in your Kubernetes manifest
  • load environment variables from secrets-store.csi.k8s.io
  • load environment variables from a secrets provider such as Infisical / Doppler
  • dotenv (.env file) cli (e.g. dotenv -e .env python myscript.py)
  • using account credentials for ephemeral token generation (e.g. Vertex AI Auth Tokens)
If BAML doesn’t work for your use case, please contact us!

Loading env variables in your program

If you do anything to load environment variables in-process - e.g. using a .env file - make sure to do it before importing the BAML client.

1import dotenv
2dotenv.load_dotenv()
3
4# Wait to import the BAML client until after loading environment variables
5from baml_client import b

Environment Variables in BAML

Environment variables are primarily used in clients to propagate authorization credentials, such as API keys, like so:

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

We do not currently support any other mechanisms for providing authorization credentials, including but not limited to:

  • fetching credentials from a secret storage service, such as AWS Secrets Manager or HashiCorp Vault

Please contact us if you need support for these use cases.