When I tried to add a short term memory to the tool_calling_agent, im getting the below error:
ValidationError: 1 validation error for get_user_name
state
Field required [type=missing, input_value={}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing
Here’s my code:
class CustomState(AgentState):
# The user_name field in short-term state
user_name: NotRequired[str]
@tool
def get_user_name(
state: Annotated[CustomState, InjectedState]
) -> str:
"""Retrieve the current user-name from state."""
# Return stored name or a default if not set
return state.get("user_name", "Arjun")
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant"),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
tools=[get_user_name]
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
response=agent_executor.invoke({"input": "what's my name?"})
It seems like the state is not getting injected properly..
When I tried to use the normal reAct agent, its working fine as expected
class CustomState(AgentState):
# The user_name field in short-term state
user_name: NotRequired[str]
@tool
def get_user_name(
state: Annotated[CustomState, InjectedState]
) -> str:
"""Retrieve the current user-name from state."""
# Return stored name or a default if not set
return state.get("user_name", "Arjun")
# Example agent setup
agent = create_react_agent(
model=llm,
tools=[get_user_name],
# state_schema=CustomState,
)
# Invocation: reads the name from state (initially empty)
agent.invoke({"messages": "what's my name?"})
response==>====
{'messages': [HumanMessage(content="what's my name?", additional_kwargs={}, response_metadata={}, id='81e9293e-f3be-4c91-aadb-ba55baaf57c9'),
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'chatcmpl-tool-e4
AIMessage(content='Your name is Arjun.', additional_kwargs={},
==========================================================
Can someone please tell me why create_tool_calling_agent is not honouring this?