I have been working through a LangChain tutorial and have hit this problem when trying to import langchain into python in vscode on macos 13.4.1.
`from dotenv import load_dotenv
import os
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
def main():
load_dotenv()
# print(os.getenv("OPENAI_API_KEY"))
st.set_page_config(page_title="Select the Data PDF")
st.header("Load your PDF below: ⚡︎")
pdf = st.file_uploader("Upload your PDF", type="pdf")
if pdf is not None:
pdf_reader = PdfReader(pdf)
# the PdfReader reads in chunks of one page each
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
st.write(text)
# split text into chunks (inside outer if not None)
# the CharacterTextSplitter class has properties that need to be set...
# so pass in property parameters to the class inititializer
text_splitter = CharacterTextSplitter(
separator="n",
chunk_size=1000,
chunk_overlap=200,
length_function=len #python's basic length function
)
chunks = text_splitter.split_text(text)
st.write(chunks)
if __name__ == '__main__' :
main()`
I see a suggested fix, but I do not understand how to implement it.
I get the following error on the Stringlit web page and the VSCode terminal:
PydanticUserError: If you use @root_validator
with pre=False (the default) you MUST specify skip_on_failure=True
. Note that @root_validator
is deprecated and should be replaced with @model_validator
. For further information visit https://errors.pydantic.dev/2.0/u/root-validator-pre-skip
Traceback:
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
exec(code, module.dict)
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/app.py", line 6, in
from langchain.embeddings import ImportError
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/init.py", line 8, in
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/agents/init.py", line 2, in
from langchain.agents.agent import Agent
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 11, in
from langchain.chains.llm import LLMChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/init.py", line 2, in
from langchain.chains.conversation.base import ConversationChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/base.py", line 7, in
from langchain.chains.conversation.memory import ConversationBufferMemory
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/memory.py", line 7, in
from langchain.chains.conversation.prompt import SUMMARY_PROMPT
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/prompt.py", line 2, in
from langchain.prompts.prompt import PromptTemplate
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/init.py", line 2, in
from langchain.prompts.base import BasePromptTemplate
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/base.py", line 35, in
class BasePromptTemplate(BaseModel, ABC):
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/base.py", line 41, in BasePromptTemplate
@root_validator()
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/pydantic/deprecated/class_validators.py", line 228, in root_validator
raise PydanticUserError(
2
Answers
What is your Python and Langchain version? I was using 0.0.20 and I got the same issue. Upgrading to Python3.9 and langchain 0.0.224 fixed this issue for me.
Hope not breaking protocol to post my note in this thread as it has been helpful.
Still failing with Pydantic 1.10.9 and Langchain 0.0.231 with Python 3.9.17. If anyone else has compatible versions it would be good to know. Going to Python 3.11 now.