agentops icon indicating copy to clipboard operation
agentops copied to clipboard

fix(langgraph): forward *args/**kwargs through node wrappers to preserve config kwarg

Open IgnazioDS opened this issue 8 months ago • 0 comments

Problem

LangGraph node functions may accept a second argument config: RunnableConfig:

from langchain_core.runnables import RunnableConfig

def agent_node(state: AgentState, config: RunnableConfig):
    response = model.invoke(state["messages"], config=config)
    return {"messages": [response]}

When AgentOps instruments the graph via _wrap_add_node, both wrapped_node_async and wrapped_node_sync use a hard-coded (state) signature:

async def wrapped_node_async(state):      # ← only accepts state
    ...
    result = await original_func(state)   # ← drops config

LangGraph calls the node with config=... as a keyword argument, but the wrapper doesn't accept it, raising:

TypeError: agent_node() got an unexpected keyword argument 'config'

Fix

Change both wrapped_node_async and wrapped_node_sync to (*args, **kwargs) and forward them to the original function:

async def wrapped_node_async(*args, **kwargs):
    state = args[0] if args else kwargs.get("state")
    ...
    result = await original_func(*args, **kwargs)  # forwards config and anything else

This is transparent — nodes that only accept state continue to work exactly as before. Nodes that also accept config (or any future LangGraph-injected arguments) now work correctly.

Fixes #1295

Test plan

  • [ ] Node function with def agent_node(state, config: RunnableConfig) no longer raises TypeError when AgentOps is active
  • [ ] Standard def agent_node(state) nodes continue to work normally
  • [ ] Async node variants work the same way

IgnazioDS avatar Mar 30 '26 18:03 IgnazioDS