https://www.boundaryml.com logo
🟔 BAML Environment Variable Issue with Convex x T...
# questions
d
BAML Environment Variable isn't loading with Convex x TanStack Start (using Bun) I have
.env
(for Gemini CLI) &
.env.local
(Convex uses this) but get error. API Key is valid since I tested it using CURL too generated by BAML Playground. Both
.env
&
.env.local
are 1:1 clone too. BAML Playground in VSCode works. Minimal repro: https://github.com/deadcoder0904/tanstack-start-convex-baml-bun-hello-world How do I solve this? My hunch is BAML being the issue, not anything else since rest deps work without BAML. I have another Electron project that works but it uses Dynamic LLM Registry. I even tried dotenv but that didn't work either. Altho I dont need dotenv since bun automatically loads it. https://cdn.discordapp.com/attachments/1253172394345107466/1418854001788846090/CleanShot_2025-09-20_at_12.29.062x.png?ex=68cfa243&is=68ce50c3&hm=dc3fd70131a7a9da47977c2492c7c5f77b7498e00d21a972e2dcc69b9be0c4bd&
v
In your b.function(…) baml_options has a parameter for env. You can try explicitly passing it there? Will have to debug why this is happening!
d
Codex came with the solution. I swear it debugs like a 10x engineer:
Copy code
Looks like your BAML config is already pointing at the right variable—baml_src/llm_clients/gemini.baml:4 now reads api_key env.GOOGLE_API_KEY, and the generated baml_client/
  inlinedbaml.ts mirrors that, so the code is fine.

  The 400 is coming from Convex running your action in its remote dev deployment. When you run bunx convex dev, it pushes your functions to Convex Cloud, and the runtime up
  there doesn’t see your local .env.local unless you explicitly copy the secrets over. With no key in the deployment, BAML sends an empty x-goog-api-key header, and Gemini
  answers with ā€œAPI key not validā€.

  Fix it by setting the variable on the Convex deployment:

  1. npx convex env set GOOGLE_API_KEY <your-key> (defaults to the dev deployment).
  2. npx convex env list to double-check it’s there.
  3. Restart bunx convex dev so the local tooling reconnects.

  If you’d rather rely on the local .env.local, switch to a local Convex runtime with bunx convex dev --local, but for the default cloud dev setup you need to push secrets with
  convex env.

  Once the key is registered with Convex, BAML should reach Gemini without the 400.