Context
When using ChatGoogleGenerativeAI with LangGraph, you may encounter a BlockingError that prevents your application from running properly. This error occurs because LangGraph detects synchronous blocking calls that can degrade performance in ASGI web servers by tying up the event loop. The error typically appears when ChatGoogleGenerativeAI attempts to perform authentication operations that involve disk access.
Answer
To resolve this blocking error with ChatGoogleGenerativeAI, you need to initialize the model in the global context rather than within the active thread runtime. This prevents the authentication process from blocking the event loop during each request.
Solution: Move your ChatGoogleGenerativeAI initialization to the global scope of your application, outside of any function that gets called during request processing.
Instead of initializing the model inside your node function like this:
def my_agent_node(state):
model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", api_key=os.environ.get("GOOGLE_API_KEY"), streaming=True)
# rest of your codeInitialize it globally at the module level:
# At the top of your file, in global scope
model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", api_key=os.environ.get("GOOGLE_API_KEY"), streaming=True)
def my_agent_node(state):
# Use the globally initialized model
# rest of your codeAlternative workarounds if you cannot modify the initialization:
For development: Run your application with
langgraph dev --allow-blockingFor deployment: Set the environment variable
BG_JOB_ISOLATED_LOOPS=true
The global initialization approach is the recommended solution as it addresses the root cause by ensuring the authentication and setup operations happen once during application startup rather than during each request processing cycle.