Agents failing to load Environment Variables #430

Closed
opened 2026-02-20 17:40:05 -05:00 by yindo · 2 comments
Owner

Originally created by @magallardo on GitHub (Jan 31, 2025).

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

import os
from dotenv import load_dotenv


# Load environment variables from a .env file
load_dotenv()

var1 = os.environ.get("VAR1")
var2 = os.environ.get("VAR2")

print("=====================================")
print(f"var1: {var1}")
print(f"var2: {var2}")
print("=====================================")

Error Message and Stack Trace (if applicable)

2025-01-31 13:05:22 =====================================
2025-01-31 13:05:22 var1: None
2025-01-31 13:05:22 var2: None
2025-01-31 13:05:22 =====================================

Description

I have an application deployed to a local langgraph server. The application contains several agents and I have configured the recommended file structure and provided a .env file with the variables and I have also provided a langgraph.json file which has an entry for the env.
When I run and call one of the agents, I added the code above and it seems like the variables are not being loaded.

The structure of my project is as follow:

my-app/
|── my_agent1
│ |── utils
│ │ |── __init__.py
│ │ |── tools.py
│ │ |── nodes.py
│ │ |── state.py
│   |── requirements.txt
│   |── __init__.py
│   |── agent.py
|── my_agent2
│ |── utils
│ │ |── __init__.py
│ │ |── tools.py
│ │ |── nodes.py
│ │ |── state.py
│   |── requirements.txt
│   |── __init__.py
│   |── agent.py
|── my_agent3
│ |── utils
│ │ |── __init__.py
│ │ |── tools.py
│ │ |── nodes.py
│ │ |── state.py
│   |── requirements.txt
│   |── __init__.py
│   |── agent.py
|── .env # environment variables
|── langgraph.json # configuration file for LangGraph

The langgraph.json file is:

{
"dockerfile_lines": [],
"dependencies": [
"./my_agent1",
"./my_agent2",
"./my_agent3"
],
"graphs": {
"agent1": "./my_agent1/agent.py:graph",
"agent2": "./my_agent2/agent.py:graph",
"agent3": "./my_agent3/agent.py:graph"
},
"env": "./.env",
"python_version": "3.11"
}

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64
Python Version: 3.11.7 (main, Dec 4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Package Information

langchain_core: 0.3.31
langchain: 0.3.15
langchain_community: 0.3.15
langsmith: 0.2.10
langchain_openai: 0.3.2
langchain_text_splitters: 0.3.5
langgraph_cli: 0.1.67
langgraph_sdk: 0.1.51

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.11
async-timeout: Installed. No version info available.
click: 8.1.8
dataclasses-json: 0.6.7
httpx: 0.28.1
httpx-sse: 0.4.0
jsonpatch: 1.33
langgraph-api: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
numpy: 1.26.4
openai: 1.59.7
orjson: 3.10.14
packaging: 24.2
pydantic: 2.10.5
pydantic-settings: 2.7.1
python-dotenv: 1.0.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.37
tenacity: 9.0.0
tiktoken: 0.8.0
typing-extensions: 4.12.2
zstandard: Installed. No version info available.

Originally created by @magallardo on GitHub (Jan 31, 2025). ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use GitHub Discussions. - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python import os from dotenv import load_dotenv # Load environment variables from a .env file load_dotenv() var1 = os.environ.get("VAR1") var2 = os.environ.get("VAR2") print("=====================================") print(f"var1: {var1}") print(f"var2: {var2}") print("=====================================") ``` ### Error Message and Stack Trace (if applicable) ```shell 2025-01-31 13:05:22 ===================================== 2025-01-31 13:05:22 var1: None 2025-01-31 13:05:22 var2: None 2025-01-31 13:05:22 ===================================== ``` ### Description I have an application deployed to a local langgraph server. The application contains several agents and I have configured the recommended file structure and provided a .env file with the variables and I have also provided a langgraph.json file which has an entry for the env. When I run and call one of the agents, I added the code above and it seems like the variables are not being loaded. The structure of my project is as follow: my-app/ |── my_agent1 │ |── utils │ │ |── __init__.py │ │ |── tools.py │ │ |── nodes.py │ │ |── state.py │   |── requirements.txt │   |── __init__.py │   |── agent.py |── my_agent2 │ |── utils │ │ |── __init__.py │ │ |── tools.py │ │ |── nodes.py │ │ |── state.py │   |── requirements.txt │   |── __init__.py │   |── agent.py |── my_agent3 │ |── utils │ │ |── __init__.py │ │ |── tools.py │ │ |── nodes.py │ │ |── state.py │   |── requirements.txt │   |── __init__.py │   |── agent.py |── .env # environment variables |── langgraph.json # configuration file for LangGraph The langgraph.json file is: { "dockerfile_lines": [], "dependencies": [ "./my_agent1", "./my_agent2", "./my_agent3" ], "graphs": { "agent1": "./my_agent1/agent.py:graph", "agent2": "./my_agent2/agent.py:graph", "agent3": "./my_agent3/agent.py:graph" }, "env": "./.env", "python_version": "3.11" } ### System Info System Information ------------------ > OS: Darwin > OS Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:00 PDT 2024; root:xnu-10063.141.2~1/RELEASE_X86_64 > Python Version: 3.11.7 (main, Dec 4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] Package Information ------------------- > langchain_core: 0.3.31 > langchain: 0.3.15 > langchain_community: 0.3.15 > langsmith: 0.2.10 > langchain_openai: 0.3.2 > langchain_text_splitters: 0.3.5 > langgraph_cli: 0.1.67 > langgraph_sdk: 0.1.51 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > aiohttp: 3.11.11 > async-timeout: Installed. No version info available. > click: 8.1.8 > dataclasses-json: 0.6.7 > httpx: 0.28.1 > httpx-sse: 0.4.0 > jsonpatch: 1.33 > langgraph-api: Installed. No version info available. > langsmith-pyo3: Installed. No version info available. > numpy: 1.26.4 > openai: 1.59.7 > orjson: 3.10.14 > packaging: 24.2 > pydantic: 2.10.5 > pydantic-settings: 2.7.1 > python-dotenv: 1.0.1 > PyYAML: 6.0.2 > requests: 2.32.3 > requests-toolbelt: 1.0.0 > SQLAlchemy: 2.0.37 > tenacity: 9.0.0 > tiktoken: 0.8.0 > typing-extensions: 4.12.2 > zstandard: Installed. No version info available.
yindo added the question label 2026-02-20 17:40:05 -05:00
yindo closed this issue 2026-02-20 17:40:05 -05:00
Author
Owner

@vbarda commented on GitHub (Feb 3, 2025):

@magallardo you don't need load_dotenv when using langgraph server. the issue might be related to load_dotenv() trying to load from .env in the directory it's called from, so if you're calling it inside the nested agent file, it won't work w/ top-level .env

@vbarda commented on GitHub (Feb 3, 2025): @magallardo you don't need `load_dotenv` when using langgraph server. the issue might be related to `load_dotenv()` trying to load from `.env` in the directory it's called from, so if you're calling it inside the nested agent file, it won't work w/ top-level `.env`
Author
Owner

@magallardo commented on GitHub (Feb 6, 2025):

Thanks.

@magallardo commented on GitHub (Feb 6, 2025): Thanks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#430