Langchain: Script fails sometimes

Hi,

I am using langchain 4 python to create an agent and run 2 tools.
It is supposed to take input queries about songs and return the API calls to spotify one needs to make.

About half of times, my script works, returns Final answer and completes normally.
But other times, it fails with below error:

File "C:\Users\xxx\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain\agents\mrkl\output_parser.py", line 67, in parse
raise OutputParserException(
langchain_core.exceptions.OutputParserException: Could not parse LLM output: `I am finished executing a plan and have the information the user asked for.`

The problem is rather than returning the full response with {Action, Action Input, Observation, Thought and Final Answer}, it just returns the Thought string and fails in output parser.
And it’s not inability to find answer, cos in the last call where it fails, the Final answer is already in the prompt.

Please help me fix the issue.

Relevant Code:

os.environ['OPENAI_API_VERSION'] = "2023-07-01-preview"
os.environ['AZURE_OPENAI_ENDPOINT'] = "https://xxx.openai.azure.com"
llm = AzureChatOpenAI(deployment_name="azure-gpt4turbo", temperature=0.0)

tools = [
        _create_api_planner_tool(api_spec, llm),
        _create_api_constructor_tool(api_spec, llm),
    ]
    prompt = PromptTemplate(
        template=API_ORCHESTRATOR_PROMPT,
        input_variables=["input", "agent_scratchpad"],
        partial_variables={
            "tool_names": ", ".join([tool.name for tool in tools]),
            "tool_descriptions": "\n".join(
                [f"{tool.name}: {tool.description}" for tool in tools]
            ),
        },
    )
    agent = ZeroShotAgent(
        llm_chain=LLMChain(llm=llm, prompt=prompt, memory=shared_memory),
        allowed_tools=[tool.name for tool in tools],return_direct=True,
        **kwargs,
    )
    return AgentExecutor.from_agent_and_tools(
        agent=agent,
        tools=tools,
        callback_manager=callback_manager,
        verbose=verbose,
        **(agent_executor_kwargs or {}),
    )

Thanks,
Yogesh