I’m building data analyst agent using Supervisor architecture. I want to provide multi-step task to supervisor, supervisor wants to call relevant agents and tools to complete the task.
from typing import Annotated
import os, pandas as pd, json, numpy as np
from dotenv import load_dotenv
load_dotenv()
import matplotlib.pyplot as plt
from langchain_openai import AzureChatOpenAI
from langgraph.graph import StateGraph, START, END, MessagesState
from langgraph.graph.message import add_messages
from langgraph.checkpoint.memory import MemorySaver
from langchain_core.tools import InjectedToolCallId, tool
from langchain_core.messages import ToolMessage
from langgraph.prebuilt import create_react_agent
from langchain.core.messages import convert_to_messages
memory = MemorySaver()
class CustomState(MessagesState):
input_data : pd.DataFrame
preprocessed_data : pd.DataFrame
def read_data_func(csv_path: str, tool_call_id: Annotated[str, InjectedToolCallId]):
"""This function read a csv file from a given location."""
df = pd.read_csv(csv_path)
return Command(update={"messages:[ToolMessage(df.to_json(), tool_call_id=tool_call_id)],
"input_data" : df.to_json()
})
def preprocess_func(state: MessagesState, tool_call_id: Annotated[str, InjectedToolCallId]):
"""This function preprocess a given dataframe."""
state_dict = json.loads(state['input_data'])
df = pd.DataFrames(state_dict)
df = df[['col1', 'col2']]
return Command(update={"messages:[ToolMessage(df.to_json(), tool_call_id=tool_call_id)],
"input_data" : df.to_json()
})
preprocess_agent = create_react_agent(
model="openai:gpt-4.1",
tools=[read_data_func, preprocess_func],
prompt=(
"You are a preprocessor agent.\n\n"
"INSTRUCTIONS:\n"
"- Assist ONLY with preprocessing-related tasks\n"
"- After you're done with your tasks, respond to the supervisor directly\n"
"- Respond ONLY with the results of your work, do NOT include ANY other text."
),
name="preprocess_agent",
)
def graph_gen_func(state: MessagesState, tool_call_id: Annotated[str, InjectedToolCallId]):
state_dict = json.loads(state['input_data'])
df = pd.DataFrames(state_dict)
df.set_index('time_stamp', inplace=True)
plt.figure(figsize=(12,6))
plt.plot(df.index, df['data_points'],marker='o')
plt.xlabel('timestamp')
plt.ylabel('data_points')
plt.title('timeseries graph')
plt.grid(True)
plt.tight_layout()
plt.show()
graph_generation_agent = create_react_agent(
model="openai:gpt-4.1",
tools=[graph_gen_func],
prompt=(
"You are a graph generation agent.\n\n"
"INSTRUCTIONS:\n"
"- Assist ONLY with garph plotting-related tasks\n"
"- After you're done with your tasks, respond to the supervisor directly\n"
"- Respond ONLY with the results of your work, do NOT include ANY other text."
),
name="graph_generation_agent ",
)
from langgraph_supervisor import create_supervisor
supervisor = create_superviso(
model="openai:gpt-4.1",
agents=[preprocess_agent, graph_generation_agent ],
prompt=(
"You are a supervisor managing two agent.\n\n"
"- a preprocess agent. Assign preprocessing-related tasks to this agent\n"
"- a graph generation agent. Assign graph plotting-related tasks to this agent\n"
"Assign work to one agent at a time, do not call agents in parallel.\n"
"Do not do any work yourself."
),
add_handoff_back_messages=True,
output_mode = "full_history"
).compile()
for chunk in supervisor.stream(
{
"messages": [
{
"role": "user",
"content": "can you please read the file ../data/income_data.csv?",
}
]
},
):
pretty_print_messages(chunk, last_message=True)
final_message_history = chunk["supervisor"]["messages"]
for chunk in supervisor.stream(
{
"messages": [
{
"role": "user",
"content": "can you please preprocess the given data?",
}
]
},
):
pretty_print_messages(chunk, last_message=True)
final_message_history = chunk["supervisor"]["messages"]
for chunk in supervisor.stream(
{
"messages": [
{
"role": "user",
"content": "can you please plot graph using preprocessed data?",
}
]
},
):
pretty_print_messages(chunk, last_message=True)
final_message_history = chunk["supervisor"]["messages"]
Here, I’m not able to pass information from one tool to other tools and from one agent to other agent. can you please help me? @marco