0

I have code below in ReAct prompt template. I have input_variables like tools, tool_names, etc. But when we are calling, not all input_variables are passed. Then, to my understanding, it should throw an error. But no error is thrown. why?

# Create the ReAct template
react_template = """Answer the following questions as best you can. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: {input}
Thought:{agent_scratchpad}"""

prompt = PromptTemplate(
    template=react_template,
    input_variables=["tools", "tool_names", "input", "agent_scratchpad"]
)
from langchain.agents import AgentExecutor, create_react_agent

# Construct the ReAct agent
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
    agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)
1
  • "tools", "tool_names", and "agent_scratchpad" are filled out by create_react_agent, so you just need to pass in the "input". Commented Sep 25 at 20:41

1 Answer 1

0

You are not seeing any error because you’re not calling prompt.format() directly. It’s being used indirectly inside create_react_agent.

How PromptTemplate normally works:

prompt = PromptTemplate(
    template=react_template,
    input_variables=["tools", "tool_names", "input", "agent_scratchpad"]
)

You’re telling LangChain that this template has these four placeholders, and you are expect all of them to be provided when formatting.

So ideally, when you call:

prompt.format(input="Hi")

should raise a KeyError because tools, tool_names, and agent_scratchpad are missing.

But, you are doing :

agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)

This wraps your prompt inside a custom chain which injects (or overrides) your variables automatically (tools, tool_names, agent_scratchpad).

In order to raise an error, put this inside your script
prompt.format(input="Hi")

It will Raises KeyError: 'tools'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.