Error with "Build a Neo4j-backed Chatbot using Python"

Hey everyone,

I'm new to Neo4j and I'm following along with a tutorial on building a Neo4j-backed chatbot using Python. However, I've hit a snag that's been giving me trouble. I keep encountering this error:

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)

Traceback:
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
exec(code, module.dict)
File "/workspace/llm-chatbot-python/bot.py", line 40, in
handle_submit(prompt)
File "/workspace/llm-chatbot-python/bot.py", line 24, in handle_submit
response = generate_response(message)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/llm-chatbot-python/agent.py", line 39, in generate_response
response = agent_executor.invoke({"input": prompt})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/langchain/chains/base.py", line 164, in invoke
final_outputs: Dict[str, Any] = self.prep_outputs(
^^^^^^^^^^^^^^^^^^
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/langchain/chains/base.py", line 440, in prep_outputs
self.memory.save_context(inputs, outputs)
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/langchain/memory/chat_memory.py", line 40, in save_context
[HumanMessage(content=input_str), AIMessage(content=output_str)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/langchain_core/messages/base.py", line 35, in init
return super().init(content=content, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/langchain_core/load/serializable.py", line 107, in init
super().init(**kwargs)
File "/workspace/.pyenv_mirror/user/3.11.7/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in init
raise validation_error

This error is popping up when I run the code and start the conversation with the bot, basically the bot responde sometimes normally other times it give me that error , and I'm having trouble understanding and resolving it.

I'm currently stuck at the fifth step of the tutorial, "Creating an Agent," and I've been trying to troubleshoot this issue for two days now. Any insights or guidance would be greatly appreciated! :thinking:

Thanks in advance for your help!

3 Likes

I'm getting the same error. Found a solution?

Hey there! I encountered the same issue before. I found a solution that worked for me. Here's a link to the solution: link. Basically, the fix involved changing the variable return_direct from false to true . Give it a try and see if it resolves your problem too!

Mine is already True
I've only done till creating an Agent. I haven't defined the scope. But this validation error keeps on coming.

It was True for me as well. I set it to False and now everything resolved.

1 Like

Thanks a lot ! it's working beautifully even though it's taking time

There are two options to fix this:

Option 1: Change return_direct to False

Option 2: Use a wrapper function to handle the response:

Define a wrapper function to handle the response

def general_tool_function(query: str) -> str:
response = llm.invoke(query)
# Ensure we return the content attribute
return response.content

Create the tool using Tool.from_function

tools = [
Tool.from_function(
name="General Chat",
description="For general chat not covered by other tools",
func=general_tool_function,
return_direct=True
)
]

It would be nice if the tutorial was updated to fix this.

1 Like