Skip to content

Commit d4730f7

Browse files
xdaveranst91
andauthored
fix(langgraph): handle Command tool output (#639)
- fixes #431 Co-authored-by: Ran Shemtov <[email protected]>
1 parent fc74be5 commit d4730f7

File tree

1 file changed

+47
-2
lines changed
  • integrations/langgraph/python/ag_ui_langgraph

1 file changed

+47
-2
lines changed

integrations/langgraph/python/ag_ui_langgraph/agent.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from langgraph.graph.state import CompiledStateGraph
77

88
try:
9-
from langchain.schema import BaseMessage, SystemMessage
9+
from langchain.schema import BaseMessage, SystemMessage, ToolMessage
1010
except ImportError:
1111
# Langchain >= 1.0.0
12-
from langchain_core.messages import BaseMessage, SystemMessage
12+
from langchain_core.messages import BaseMessage, SystemMessage, ToolMessage
1313

1414
from langchain_core.runnables import RunnableConfig, ensure_config
1515
from langchain_core.messages import HumanMessage
@@ -682,6 +682,51 @@ async def _handle_single_event(self, event: Any, state: State) -> AsyncGenerator
682682

683683
elif event_type == LangGraphEventTypes.OnToolEnd:
684684
tool_call_output = event["data"]["output"]
685+
686+
if isinstance(tool_call_output, Command):
687+
# Extract ToolMessages from Command.update
688+
messages = tool_call_output.update.get('messages', [])
689+
tool_messages = [m for m in messages if isinstance(m, ToolMessage)]
690+
691+
# Process each tool message
692+
for tool_msg in tool_messages:
693+
if not self.active_run["has_function_streaming"]:
694+
yield self._dispatch_event(
695+
ToolCallStartEvent(
696+
type=EventType.TOOL_CALL_START,
697+
tool_call_id=tool_msg.tool_call_id,
698+
tool_call_name=tool_msg.name,
699+
parent_message_id=tool_msg.id,
700+
raw_event=event,
701+
)
702+
)
703+
yield self._dispatch_event(
704+
ToolCallArgsEvent(
705+
type=EventType.TOOL_CALL_ARGS,
706+
tool_call_id=tool_msg.tool_call_id,
707+
delta=json.dumps(event["data"].get("input", {})),
708+
raw_event=event
709+
)
710+
)
711+
yield self._dispatch_event(
712+
ToolCallEndEvent(
713+
type=EventType.TOOL_CALL_END,
714+
tool_call_id=tool_msg.tool_call_id,
715+
raw_event=event
716+
)
717+
)
718+
719+
yield self._dispatch_event(
720+
ToolCallResultEvent(
721+
type=EventType.TOOL_CALL_RESULT,
722+
tool_call_id=tool_msg.tool_call_id,
723+
message_id=str(uuid.uuid4()),
724+
content=tool_msg.content,
725+
role="tool"
726+
)
727+
)
728+
return
729+
685730
if not self.active_run["has_function_streaming"]:
686731
yield self._dispatch_event(
687732
ToolCallStartEvent(

0 commit comments

Comments
 (0)