Skip to content

Commit b5f5e05

Browse files
committed
fix(templates): load the project .env in the Temporal worker before mapping the key
The worker is a separate process and agents run does not load the project .env into it today, so the LITELLM_API_KEY -> OPENAI_API_KEY mapping ran against an empty environment. Call load_dotenv() first (as acp.py already does), so the template works on its own; #515 makes the CLI load .env for both processes as well. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
1 parent 8ad88db commit b5f5e05

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎src/agentex/lib/cli/templates/temporal-openai-agents/project/run_worker.py.j2‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import os
22

3-
# LiteLLM proxy auth: the worker process runs the model calls, so copy
3+
# Load the project .env first (the ACP process does the same), then map the
4+
# LiteLLM proxy key: the worker process runs the model calls, so copy
45
# LITELLM_API_KEY to OPENAI_API_KEY before importing project code (some
56
# frameworks build their OpenAI client at import time). Without this the
67
# worker raises "Missing credentials".
8+
from dotenv import load_dotenv
9+
10+
load_dotenv() # the project .env, before any project code runs
11+
712
_litellm_key = os.environ.get("LITELLM_API_KEY")
813
if _litellm_key and not os.environ.get("OPENAI_API_KEY"):
914
os.environ["OPENAI_API_KEY"] = _litellm_key

‎src/agentex/lib/cli/templates/temporal-pydantic-ai/project/run_worker.py.j2‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ so we don't have to enumerate activities by hand here.
1010

1111
import os
1212

13-
# LiteLLM proxy auth: the worker process runs the model calls, so copy
13+
# Load the project .env first (the ACP process does the same), then map the
14+
# LiteLLM proxy key: the worker process runs the model calls, so copy
1415
# LITELLM_API_KEY to OPENAI_API_KEY before importing project code (some
1516
# frameworks build their OpenAI client at import time). Without this the
1617
# worker raises "Missing credentials".
18+
from dotenv import load_dotenv
19+
20+
load_dotenv() # the project .env, before any project code runs
21+
1722
_litellm_key = os.environ.get("LITELLM_API_KEY")
1823
if _litellm_key and not os.environ.get("OPENAI_API_KEY"):
1924
os.environ["OPENAI_API_KEY"] = _litellm_key

‎tests/lib/cli/test_init_templates.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,5 @@ def test_temporal_worker_maps_litellm_key(tmp_path: Path, template_type: Templat
151151
project_dir = _render_project(tmp_path, template_type)
152152
src = (project_dir / "project" / "run_worker.py").read_text()
153153
assert 'os.environ["OPENAI_API_KEY"] = _litellm_key' in src, f"{template_type.value} run_worker.py lacks the LITELLM_API_KEY mapping"
154+
assert "load_dotenv()" in src, f"{template_type.value} run_worker.py does not load the project .env"
155+
assert src.index("load_dotenv()") < src.index('_litellm_key = os.environ.get("LITELLM_API_KEY")'), "mapping must run after load_dotenv()"

0 commit comments

Comments
 (0)