TypeError
TypeError: Edge condition function must return a boolean or awaitable resolving to boolean
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
graph.run()
File "langgraph/core.py", line 128, in run
if not await edge.condition():
TypeError: object of type 'NoneType' cannot be used in boolean context Why it happens
LangGraph expects edge condition functions to return a boolean value or an awaitable resolving to boolean. If the function returns None or any non-boolean type, the framework cannot evaluate the condition, causing a TypeError. This often happens when the function lacks an explicit return or returns an incompatible type.
Detection
Add type assertions or logging inside edge condition functions to verify they return booleans or awaitables resolving to booleans before runtime evaluation.
Causes & fixes
Edge condition function lacks an explicit return statement, returning None implicitly.
Add an explicit return statement that returns a boolean value (True or False) in the edge condition function.
Edge condition function returns a non-boolean type such as a string or integer.
Modify the function to return a boolean value or an awaitable resolving to boolean, ensuring correct type.
Edge condition function is asynchronous but does not return an awaitable (missing async keyword or missing return).
Define the function with async def and ensure it returns an awaitable resolving to a boolean.
Code: broken vs fixed
def edge_condition():
print("Checking condition")
# Missing return statement causes None to be returned
# This triggers TypeError when LangGraph evaluates the edge condition
graph.add_edge(condition=edge_condition) # Error here import os
os.environ['LANGGRAPH_API_KEY'] = os.environ.get('LANGGRAPH_API_KEY', '') # Use environment variable for keys
def edge_condition():
print("Checking condition")
return True # Fixed: explicit boolean return
# Add edge with correct condition function
graph.add_edge(condition=edge_condition)
print("Edge condition function fixed and graph running") Workaround
Wrap the edge condition function call in a try/except block to catch TypeError, then coerce or default the return value to a boolean before returning.
Prevention
Use static type checking or runtime type assertions on edge condition functions to guarantee they always return booleans or awaitables resolving to booleans before integrating with LangGraph.