langchain: AttributeError: module 'langchain' has no attribute 'verbose'

System Info

274 Mac M2

this error appears often and unexpectedly but gets solved temporarily by running a force reinstall

pip install --upgrade --force-reinstall --no-deps --no-cache-dir langchain

full error

2023-08-28 08:16:48.197 Uncaught app exception
Traceback (most recent call last):
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/Users/user/Developer/newfilesystem/pages/chat.py", line 104, in <module>
    llm = ChatOpenAI(
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/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 1066, in pydantic.main.validate_model
  File "pydantic/fields.py", line 439, in pydantic.fields.ModelField.get_default
  File "/Users/user/Developer/newfilesystem/venv/lib/python3.10/site-packages/langchain/chat_models/base.py", line 49, in _get_verbosity
    return langchain.verbose

main librairies in the project requests streamlit pandas colorlog python-dotenv tqdm fastapi uvicorn langchain openai tiktoken chromadb pypdf colorlog logger docx2txt

Who can help?

@hwchase17

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

intermittent - no pattern

Expected behavior

resolution

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 2
  • Comments: 26 (1 by maintainers)

Commits related to this issue

Most upvoted comments

you’re right, all bets are off if a user does import langchain and get their own module instead of langchain-ai/langchain.

however, i ran into this w/ code that was using import langchain_core and tripping over a langchain/ in my working directory.

@obi1kenobi the import langchain in get_verbose and get_debug is pulling in the user’s langchain module.

➜ echo 'print("this is the wrong langchain")' > langchain.py
➜ poetry run python -c 'from langchain_core.globals import get_verbose; print(get_verbose())'
this is the wrong langchain
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../lib/python3.12/site-packages/langchain_core/globals/__init__.py", line 72, in get_verbose
    old_verbose = langchain.verbose
                  ^^^^^^^^^^^^^^^^^
AttributeError: module 'langchain' has no attribute 'verbose'

this will also happen for get_llm_cache.

the langchain_core/globals/__init__.py code could except AttributeError to protect from this. it could also raise a warning to the user that they’re masking the langchain module with their own.

i ran into this because i had a clone of langchain in my current working directory.

Renaming langchain.py to main.py fixed it!

Is it possible that the app is running inside a different virtualenv than where you added the print statements? I think something like that is the only possible answer here because of the printout value you showed earlier:

*** langchain verbose value: False

This was obtained by reading langchain.verbose, which is the value that the crashing code says doesn’t exist. But it clearly exists with value False here, so it has to be the case that the error is coming from some other process which is invoking langchain in a different (probably outdated) virtualenv.

Going back through your previous messages, it seems like there might indeed be two virtualenvs at different paths and with different versions: one in python 3.9 and one in python 3.10.

Could you look through your machine (and your previous messages here) and make sure to run the pip freeze and pipdeptree commands in both virtualenvs? Here are the full commands one more time, for your convenience:

pip freeze | grep langchain
pip install pipdeptree && pipdeptree --reverse