langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
88.76k stars 13.95k forks source link

LangChain throwing parsing error and goes in loop when returning a String response #20341

Open vivekojharhino opened 2 months ago

vivekojharhino commented 2 months ago

Checked other resources

Example Code

from uuid import uuid4

from langchain.agents import AgentExecutor, create_structured_chat_agent
from langchain.prompts import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    MessagesPlaceholder,
    SystemMessagePromptTemplate,
)
from langchain.memory import ConversationBufferWindowMemory

def __handle_parsing_error(self, error):
    self.__logger.error(f"Parsing error encountered: {error}")
    # For now, returning a generic error message.
    return "I'm sorry, there was a problem understanding your request."

def __get_agent(self, session_id):
    return AgentExecutor.from_agent_and_tools(
        agent=self.__agent,
        tools=self.__tools,
        verbose=self.__verbose_mode,
        memory=self.__get_history(session_id),
        handle_parsing_errors=__handle_parsing_error,
        return_intermediate_steps=False,
    )

def send_message(self, session_id, message: str = "") -> str:
    if not message.strip():
        return "You didn't ask a question. How can I assist you further?"
    runner = self.__get_agent(session_id)
    try:
        response = runner.invoke({input: message})
    except Exception as ex:
        self.__logger.exception(str(ex))
        return "Sorry, please try again."
    return response if response else "No response received"

    def __get_history(self, session_id) -> ConversationBufferWindowMemory:
        if session_id not in self.__history:
            self.__history[session_id] = ConversationBufferWindowMemory(k=10, memory_key=chat_history,
                                                                        return_messages=True)

        return self.__history[session_id]

session_id = str(uuid4())
response = send_message(session_id, "Hi")

Error Message and Stack Trace (if applicable)

2024-04-11 08:40:28,242] ERROR RhinoAgent 2 validation errors for AIMessage content str type expected (type=type_error.str) content value is not a valid list (type=type_error.list) Traceback (most recent call last): File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain/chains/base.py", line 163, in invoke raise e File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain/chains/base.py", line 158, in invoke final_outputs: Dict[str, Any] = self.prep_outputs( ^^^^^^^^^^^^^^^^^^ File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain/chains/base.py", line 455, in prep_outputs self.memory.save_context(inputs, outputs) File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain/memory/chat_memory.py", line 40, in save_context [HumanMessage(content=input_str), AIMessage(content=output_str)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain_core/messages/base.py", line 45, in init return super().init(content=content, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/langchain_core/load/serializable.py", line 120, in init super().init(kwargs) File "/Users/vmac/vWork/protoype/venv/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in init raise validation_error pydantic.v1.error_wrappers.ValidationError: 2 validation errors for AIMessage content str type expected (type=type_error.str) content value is not a valid list (type=type_error.list)

Description

I am trying to use langchain to build a basic chatbot. I have the prompt template defined and I am using openAI GPT4. When I ask any simple question that gets a string response from agent, it gives me the error above. Sometimes it also goes into a loop until I run into the "RateLimit" Error . Attached a screenshot for the same

Screenshot 2024-04-10 at 12 34 14

System Info

langchain==0.1.13 langchain-community==0.0.29 langchain-core==0.1.33 langchain-openai==0.1.1 langchain-text-splitters==0.0.1

ccurme commented 2 months ago

Are you able to supply complete code to replicate the issue? Your code runs fine for me (but it looks incomplete).

vivekojharhino commented 2 months ago

Sure, I will add the missing methods, cannot share the code as is since its for my company