-
Notifications
You must be signed in to change notification settings - Fork 19
Description
Bug Report: IndexError in LangGraph instrumentation when handling variable argument patterns
Environment:
LangTrace Python SDK Version: 3.8.18
LangGraph Version: Latest (used with LangGraph Supervisor)
Python Version: 3.13
Error Location: [langtrace_python_sdk/instrumentation/langgraph/patch.py] in get_atrribute_key_value() function
Problem Description:
The LangTrace SDK's LangGraph instrumentation fails with IndexError: tuple index out of range when [add_node]) is called with different argument patterns than expected. The current implementation assumes [add_node] always receives exactly 2 arguments (name, node_function), but LangGraph's [add_node] method can be called with various patterns.
Root Cause:
In get_atrribute_key_value() function (lines 80-101), the code directly accesses args[0] and args[1] without checking if these indices exist:
def get_atrribute_key_value(method_name, args):
if args is None or len(args) == 0:
return None
if "add_node" in method_name:
return {
"langgraph.node": json.dumps({
"name": args[0], # ❌ Can fail if len(args) < 1
"action": (
args[1].json() # ❌ Can fail if len(args) < 2
if hasattr(args[1], "json")
else (
args[1].__name__
if hasattr(args[1], "__name__")
else str(args[1])
)
),
}),
"langgraph.task.name": "add_node",
}
When the Error Occurs:
Single-argument [add_node] calls: When LangGraph Supervisor calls [add_node(compiled_graph_object) with only one argument
Object-based node addition: When passing CompiledStateGraph objects directly instead of (name, function) pairs
Keyword arguments: When additional parameters like destinations are passed as kwargs (not captured in args)
These cause IndexError:
builder.add_node(supervisor_agent, destinations=tuple(agent_names) + (END,))
builder.add_node(compiled_graph_object)
Debug output showing the issue:
[DEBUG] method_name: langgraph.graph.state.StateGraph.add_node
[DEBUG] args content: (<langgraph.graph.state.CompiledStateGraph object at 0x7f94950b8490>,)
[DEBUG] args length: 1
❌ Crashes when trying to access args[1]
Suggested Fix:
Add bounds checking and handle different argument patterns:
def get_atrribute_key_value(method_name, args):
try:
if args is None or len(args) == 0:
return None
if "add_node" in method_name:
name = "unknown"
action = "unknown"
if len(args) >= 2:
# Normal case: add_node(name, node_function)
name = str(args[0])
if hasattr(args[1], "json"):
action = args[1].json()
elif hasattr(args[1], "__name__"):
action = args[1].__name__
else:
action = str(args[1])
elif len(args) == 1:
# Single argument case: add_node(node_object)
node_obj = args[0]
if hasattr(node_obj, 'name') and node_obj.name:
name = str(node_obj.name)
else:
name = str(type(node_obj).__name__)
action = str(type(node_obj).__name__)
return {
"langgraph.node": json.dumps({
"name": name,
"action": action,
}),
"langgraph.task.name": "add_node",
}
except Exception as e:
# Fallback to prevent crashes
return {
"langgraph.task.name": "add_node",
"langgraph.node": json.dumps({"name": "unknown", "action": "unknown"})
}
Similar Issues:
The same pattern exists in other methods [add_edge], add_conditional_edges, etc.) that also need bounds checking.