Problem importing langchain

With all of the cool LLM things happening, I have wanted to give it a try, but I am having some issues with implementing this. Even on basic “import langchain”, I get the following error (mac M1):

File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 552, in _run_script
exec(code, module.**dict**)
File “/Users/e996w533/Documents/small-projects/LLMs/streamlit/test.py”, line 2, in
from langchain.llms import OpenAI
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/**init**.py”, line 6, in
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/**init**.py”, line 40, in
from langchain.agents.agent_toolkits import (
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/agent_toolkits/**init**.py”, line 12, in
from langchain.agents.agent_toolkits.csv.base import create_csv_agent
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/agent_toolkits/csv/base.py”, line 4, in
from langchain.agents.agent_toolkits.pandas.base import create_pandas_dataframe_agent
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/agent_toolkits/pandas/base.py”, line 18, in
from langchain.agents.types import AgentType
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/types.py”, line 5, in
from langchain.agents.chat.base import ChatAgent
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/chat/base.py”, line 6, in
from langchain.agents.chat.output_parser import ChatOutputParser
File “/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/agents/chat/output_parser.py”, line 12, in
class ChatOutputParser(AgentOutputParser):
File “pydantic/main.py”, line 229, in pydantic.main.ModelMetaclass.**new**
File “pydantic/fields.py”, line 491, in pydantic.fields.ModelField.infer
File “pydantic/fields.py”, line 421, in pydantic.fields.ModelField.**init**
File “pydantic/fields.py”, line 542, in pydantic.fields.ModelField.prepare
File “pydantic/fields.py”, line 804, in pydantic.fields.ModelField.populate_validators
File “pydantic/validators.py”, line 723, in find_validators
RuntimeError: no validator found for <class ‘re.Pattern’>, see `arbitrary_types_allowed` in Config

I am in a clean virtual environment with the following (called by pip install streamlit, openai, langchain)

aiohttp==3.8.5
aiosignal==1.3.1
altair==5.1.1
annotated-types==0.5.0
async-timeout==4.0.3
attrs==23.1.0
backports.zoneinfo==0.2.1
blinker==1.6.2
cachetools==5.3.1
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.7
dataclasses-json==0.5.14
frozenlist==1.4.0
gitdb==4.0.10
GitPython==3.1.35
idna==3.4
importlib-metadata==6.8.0
importlib-resources==6.0.1
Jinja2==3.1.2
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
langchain==0.0.286
langsmith==0.0.35
markdown-it-py==3.0.0
MarkupSafe==2.1.3
marshmallow==3.20.1
mdurl==0.1.2
multidict==6.0.4
mypy-extensions==1.0.0
numexpr==2.8.5
numpy==1.24.4
openai==0.28.0
packaging==23.1
pandas==2.0.3
Pillow==9.5.0
pkgutil_resolve_name==1.3.10
protobuf==4.24.3
pyarrow==13.0.0
pydantic==2.3.0
pydantic_core==2.6.3
pydeck==0.8.0
Pygments==2.16.1
Pympler==1.0.1
python-dateutil==2.8.2
pytz==2023.3.post1
pytz-deprecation-shim==0.1.0.post0
PyYAML==6.0.1
referencing==0.30.2
requests==2.31.0
rich==13.5.2
rpds-py==0.10.2
six==1.16.0
smmap==5.0.0
SQLAlchemy==2.0.20
streamlit==1.26.0
tenacity==8.2.3
toml==0.10.2
toolz==0.12.0
tornado==6.3.3
tqdm==4.66.1
typing-inspect==0.9.0
typing_extensions==4.7.1
tzdata==2023.3
tzlocal==4.3.1
urllib3==2.0.4
validators==0.22.0
yarl==1.9.2
zipp==3.16.2

I’ve done a lot of internet searching, but no luck in solving this. Any ideas from this group?

The error message you’re seeing is related to the Pydantic library, which Langchain uses. I suggest trying to install a specific version of Pydantic that might be compatible with Langchain. You can try installing an older version of Pydantic to see if that resolves the issue. (Make sure to uninstall the current version first)

For instance, you could downgrade the current version to this:

pip install pydantic==1.8

Thanks for the help! I had messed with pydantic versions with no luck, but then I tried also downgrading pydantic-core, and it worked. If it helps anyone else, this is the combination that finally worked for me:

pydantic==1.8
pydantic_core==2.0.0

Thanks again!

Spoke too soon… The above versions got the interface up and running, but when I actually tried to used the chatbot, I got this error:

Traceback:
File "/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/Users/e996w533/Documents/small-projects/LLMs/streamlit/test.py", line 18, in <module>
    generate_response(text)
File "/Users/e996w533/Documents/small-projects/LLMs/streamlit/test.py", line 9, in generate_response
    llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
File "/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/load/serializable.py", line 74, in __init__
    super().__init__(**kwargs)
File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
File "pydantic/main.py", line 1046, in pydantic.main.validate_model
File "pydantic/fields.py", line 424, in pydantic.fields.ModelField.get_default
File "/Users/e996w533/opt/anaconda3/lib/python3.8/site-packages/langchain/llms/base.py", line 68, in _get_verbosity
    return langchain.verbose

Back to troubleshooting…

Did you end up solving this?

Did you end up solving this a few of my apps are not able to run and its really concerning to me the lack of documentation!

Unfortunately, no. I have tried many (many!) approaches found in various forums, but I have not been able to solve it. My only guess is some conflict between my Python and various libraries, or possibly the M1 hardware, but I don’t know.

Hi I was having similar issue.
I did this import and worked:

from langchain.output_parsers import PydanticOutputParser

I found at the documentation: (Pydantic parser | 🦜️🔗 LangChain)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.