Basically, I have one agent and it takes lot of tools.
When runninng perticular tool, inside the tool, i want to know the state of the langraph state, but I am not able to access it.
My main agenrt code as follows:
#thread.py
#Create chatbot node
tools = [taxes_federal_internal_revenue_code, taxes_federal_court_cases, taxes_federal_treasury_regulations, taxes_international, taxes_federal_forms, taxes_states]
llm = ChatOpenAI(model='gpt-4o')
llm_with_tools = llm.bind_tools(tools)
def chatbot(state: MessagesState):
toolResp = llm_with_tools.invoke(state["messages"])
return MessagesState(
messages=[toolResp],
log_stream_name=state["log_stream_name"], # Corrected access
next_step=state["next_step"], # Ensure next_step is passed
)
graph_builder.add_node("chatbot", chatbot)
In this context, my taxes_states tool looks like following:
@tool()
def taxes_states(query: str) -> Tuple[List[str]]:
"""
Purpose: Retrieve relevant U.S. state tax law documents, including specific state tax codes,
regulations, and guidance (e.g., California tax laws)
"""
print("-----------------taxes_states-----------")
print(query)
print("-----------------taxes_states-----------")
#Perform RAG here and get retrieved_docs and return it
return retrieved_docs
The documentation tells that how we can do that inside the node (/), but it does not mention how we can achive that inside the tool.
Basically, I have one agent and it takes lot of tools.
When runninng perticular tool, inside the tool, i want to know the state of the langraph state, but I am not able to access it.
My main agenrt code as follows:
#thread.py
#Create chatbot node
tools = [taxes_federal_internal_revenue_code, taxes_federal_court_cases, taxes_federal_treasury_regulations, taxes_international, taxes_federal_forms, taxes_states]
llm = ChatOpenAI(model='gpt-4o')
llm_with_tools = llm.bind_tools(tools)
def chatbot(state: MessagesState):
toolResp = llm_with_tools.invoke(state["messages"])
return MessagesState(
messages=[toolResp],
log_stream_name=state["log_stream_name"], # Corrected access
next_step=state["next_step"], # Ensure next_step is passed
)
graph_builder.add_node("chatbot", chatbot)
In this context, my taxes_states tool looks like following:
@tool()
def taxes_states(query: str) -> Tuple[List[str]]:
"""
Purpose: Retrieve relevant U.S. state tax law documents, including specific state tax codes,
regulations, and guidance (e.g., California tax laws)
"""
print("-----------------taxes_states-----------")
print(query)
print("-----------------taxes_states-----------")
#Perform RAG here and get retrieved_docs and return it
return retrieved_docs
The documentation tells that how we can do that inside the node (https://langchain-ai.github.io/langgraph/how-tos/state-reducers/), but it does not mention how we can achive that inside the tool.
Share Improve this question asked Mar 21 at 0:35 KiranKiran 2,4476 gold badges21 silver badges37 bronze badges1 Answer
Reset to default 1Accessing the graph state within a LangGraph tool isn't straightforward due to the framework's design. However, you can pass the state to your tool by annotating the tool's parameters with InjectedState
. Here's how:
from typing_extensions import Annotated
from langchain_core.tools import tool
from langgraph.prebuilt import InjectedState
@tool
def taxes_states(
query: str,
state: Annotated[dict, InjectedState],
) -> Tuple[List[str]]:
"""
Purpose: Retrieve relevant U.S. state tax law documents, including specific state tax codes,
regulations, and guidance (e.g., California tax laws)
"""
# Access state information
user_info = state.get("user_info", {})
# Perform RAG here and get retrieved_docs
return retrieved_docs
By using the InjectedState
annotation, LangGraph injects the current state into the tool when it's called. This approach allows your tools to access and utilize the graph's state effectively.