0

I am trying to build a simple react agent using langchain which uses a tool to get the weather condition of a location

I tried making my custom react prompt template and used 'create_react_agent' and 'AgentExecutor' from langchain

This was my code, how do i fix the infinite loop problem in which the agent even after getting the correct output from the tool is not stopping and keeps logging the error that "Action" is missing after "Thought"

from langchain_ollama import ChatOllama
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.tools import Tool
from langchain.agents import create_react_agent, AgentExecutor
from langchain_core.prompts import PromptTemplate

llm = ChatOllama(
    model="llama3.2:1b",
    temperature=0.1,
)

react_template = """
Use the following set of rules and instructions to give the best possible answer for the user's questions, based on the tools provided to you

Tools: {tools}

Follow these steps of reasoning and action strictly to find out the correct answer for the user's question
[Question]: The input question asked by the user
[Thought]: Understanding and thinking about the question asked by the user
[Action]: The appropriate action to be taken based on the question asked by the user, use one of the following tools [{tool_names}]
[Observation]: The result of the action
[Thought]: Is this the answer to the question asked by the user ? If yes then give the final answer, else repeat the process (Thought->Action->Observation)
[Final Answer]: I have the final answer to the user's question

Example 1:

[Question]: What is the weather in france ?
[Thought]: I should first search for the weather in france
[Action]: I should use the get_weather tool to get the weather in france
[Observation]: The weather returned from get_weather was cloudy
[Thought]: This was the answer returned from the get_weather tool
[Final Answer]: cloudy

Critical mistakes to avoid:
1. Do not make up answers. The model should only give the correct answer to the user's question returned by the tools provided to it
2. Do not provide justifications to answer produced
3. Keep the final answers short and to the point

Begin !

[Question]: {question}
{agent_scratchpad}
"""

react_prompt = PromptTemplate(
    template=react_template,
    input_variables=["question", "tools", "tool_names"],
)


def get_weather_func(location: str) -> str:
    location = location.lower().strip()
    if location == "france":
        return "cloudy"
    elif location == "italy":
        return "sunny"
    else:
        return "unknown weather"

weather_tool = Tool(
    name= "get_weather",
    func=get_weather_func,
    description="Useful for getting the weather of a location"
)

tools = [weather_tool]

agent = create_react_agent(
    llm=llm,
    tools=tools,
    prompt=react_prompt
)

agent_exexcutor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,
    handle_parsing_errors=True
)

if __name__ == "__main__":
    response = agent_exexcutor.invoke(
        input={
            "question": "What is the weather in france ?",
            "tools": "\n".join([t.name for t in tools]),
            "tool_names": "\n".join([t.name + " : " + t.description for t in tools]),
            "agent_scratchpad": "",
        }
    )
    
    print(response["output"])

These are the errors i am getting on my terminal

[Question]: What is the weather in france ?
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
[Question]: What is the weather in france ?
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
The result of the action is cloudy
Invalid Format: Missing 'Action:' after 'Thought:'
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
Thought: I should first search for the weather in franceInvalid Format: Missing 'Action Input:' after 'Action:'
> Finished chain.
> Finished chain.
Agent stopped due to iteration limit or time limit.

2 Answers 2

1

LangChain’s agent output parser splits the model output by the keywords Thought:, Action:, Action Input:, Observation:, and Final Answer: to know what to do next. If the output deviates from this exact format, the parser fails with errors like the one you see.

Why the error happens with your prompt

Your prompt uses square brackets like [Thought]: and [Action]:, but LangChain’s default ZeroShotAgentOutputParser expects the output in this exact format (without square brackets):

Thought: ...
Action: ...
Action Input: ...
Observation: ...
Thought: ...
Final Answer: ...

Your example shows:

[Thought]: I should first search for the weather in france
[Action]: I should use the get_weather tool to get the weather in france

But the agent parser expects:

Thought: I should first search for the weather in france
Action: get_weather
Action Input: france
Sign up to request clarification or add additional context in comments.

Comments

1

After applying the prompt structure recommended by Langchain, I still had the error.

I had to debug the Langchain library to see that, in my case, it was the final answer which was incorrectly formatted.

Langchain expects the final result to be prefixed by Final Answer: . As I've fine-tuned the Action/Thought cycle to adapt to my case, and produce a structured (JSON) output, I think Langchain lost sight of the core format.

So, I had to include this sentence in my prompt:

IMPORTANT: Always follow this format strictly:

Question: {input}
Thought: your reasoning about what you need to do
Action: the action to take, which must be one of the tools [{tool_names}]
Action Input: the input for the action (see tool field descriptions above)
Observation: the result of the action
... (this Thought/Action/Action Input/Observation cycle can repeat N times)
Thought: I now know the final answer
Final Answer: <YOUR JSON OBJECT HERE - NEVER forget to include "Final Answer: " before the JSON>

CRITICAL REMINDER: Your final response MUST start with exactly "Final Answer: " (including the space after the colon) followed by the JSON object. Omitting this prefix will cause processing to fail.

Begin!

Question: {input}
Thought: {agent_scratchpad}

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.