The error you're seeing indicates that your prompt is missing placeholder variables required by the React agent (such as "tool_names", "agent_scratchpad", and "tools"). In LangChain v0.3, the create_react_agent function expects the prompt to include these variables so that it can pass along tool metadata and the agent’s internal scratchpad information.
To resolve this, you should update your PromptTemplate to include the required placeholders. For example, you can modify your template like this:
from langchain.prompts import PromptTemplate
# Build the tool metadata that will be used in the prompt
tool_names = ", ".join(tool.name for tool in tools)
tools_str = "\n".join(f"{tool.name}: {tool.description}" for tool in tools)
# Updated PromptTemplate with required variables
prompt_template = PromptTemplate(
template="""
You are an intelligent agent. Use the following tools to answer the query.
Tools available: {tools}
Tool names: {tool_names}
Agent scratchpad: {agent_scratchpad}
Query: {input}
""",
input_variables=["input", "tool_names", "agent_scratchpad", "tools"]
)
Then, when you create your React agent, the prompt will have all the required variables for proper functioning:
from langchain_openai import ChatOpenAI
from langchain.agents.react.agent import create_react_agent
from langchain_community.tools import Tool
# Define a mock function to find similar companies
def find_similar_companies(company_name: str) -> str:
mock_data = {
"XYZ Corp": "ABC Inc, DEF Ltd",
"Acme Corp": "GHI LLC, JKL PLC"
}
return f"Similar companies to {company_name}: {mock_data.get(company_name, 'No similar companies found.')}"
# Define the tools using the Tool class
tools = [
Tool(
name="Find Similar Companies",
func=find_similar_companies,
description="Use this tool to find similar companies based on a company name."
)
]
# Initialize the ChatOpenAI model
llm = ChatOpenAI(
model="gpt-3.5-turbo",
openai_api_key="API_KEY"
)
# (See the updated prompt_template from the previous snippet)
# Create a React agent with the create_react_agent function
agent = create_react_agent(
llm=llm,
tools=tools,
prompt=prompt_template
)
# Test the agent
query = "Find similar companies to XYZ Corp"
# When invoking, you need to supply at least the "input" variable.
# The agent will internally fill the other variables.
# For a simple test, you can pass empty strings for them:
response = agent.invoke({
"input": query,
"tool_names": tool_names,
"tools": tools_str,
"agent_scratchpad": ""
})
# Print the response
print("Agent Response:", response)
Explanation
-
Prompt Template Variables:
The React agent expects the prompt to have placeholders for "tool_names", "agent_scratchpad", and "tools". By updating the template to include these, you provide the necessary context for the agent to know which tools are available and how to format its internal reasoning (agent_scratchpad).
-
Filling the Variables:
In the example, tool_names
is generated by joining the names of the tools, and tools_str
is a descriptive summary of each tool. The agent_scratchpad
is initially set to an empty string, but during operation, the agent will update it with its internal chain-of-thought if needed.
With this updated prompt, the agent should work without the missing variables error.