Search code examples
pythonlangchain

\\langchain\\agents\\agent_toolkits' is not in the subpath of 'site-packages\\langchain_core' OR one path is relative and the other is absolute


I just upgrade LangChain and OpenAi using below conda install. Then I got below error, any idea how to solve it? Thanks

https://anaconda.org/conda-forge/langchain conda install conda-forge::langchain

https://anaconda.org/conda-forge/openai conda install conda-forge::openai

from langchain.agents.agent_toolkits import create_python_agent


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 from langchain.agents.agent_toolkits import create_python_agent
      2 from langchain.tools.python.tool import PythonREPLTool
      3 from langchain.llms.openai import OpenAI

File <frozen importlib._bootstrap>:1075, in _handle_fromlist(module, fromlist, import_, recursive)

File c:\Users\yongn\miniconda3\envs\langchain_ai\lib\site-packages\langchain\agents\agent_toolkits\__init__.py:50, in __getattr__(name)
     48 """Get attr name."""
     49 if name in DEPRECATED_AGENTS:
---> 50     relative_path = as_import_path(Path(__file__).parent, suffix=name)
     51     old_path = "langchain." + relative_path
     52     new_path = "langchain_experimental." + relative_path

File c:\Users\test\miniconda3\envs\langchain_ai\lib\site-packages\langchain_core\_api\path.py:30, in as_import_path(file, suffix, relative_to)
     28 if isinstance(file, str):
     29     file = Path(file)
---> 30 path = get_relative_path(file, relative_to=relative_to)
     31 if file.is_file():
     32     path = path[: -len(file.suffix)]

File c:\Users\test\miniconda3\envs\langchain_ai\lib\site-packages\langchain_core\_api\path.py:18, in get_relative_path(file, relative_to)
     16 if isinstance(file, str):
     17     file = Path(file)
---> 18 return str(file.relative_to(relative_to))

File c:\Users\test\miniconda3\envs\langchain_ai\lib\pathlib.py:818, in PurePath.relative_to(self, *other)
    816 if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
    817     formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
--> 818     raise ValueError("{!r} is not in the subpath of {!r}"
    819             " OR one path is relative and the other is absolute."
    820                      .format(str(self), str(formatted)))
    821 return self._from_parsed_parts('', root if n == 1 else '',
    822                                abs_parts[n:])

ValueError: 'c:\\Users\\test\\miniconda3\\envs\\langchain_ai\\lib\\site-packages\\langchain\\agents\\agent_toolkits' is not in the subpath of 'c:\\Users\\test\\miniconda3\\envs\\langchain_ai\\lib\\site-packages\\langchain_core' OR one path is relative and the other is absolute.

Solution

  • It seems in October 2023, some logic related with agents was moved to a module called "experimental".

    You first need to install this new library:

     pip install langchain_experimental
    

    and then shift the module from where you import certain classes. PythonREPLTool and create_python_agent needs to be imported from the new langchain_experimental module while other classes remain still.

    Classes that need to be imported from the new module:

    from langchain_experimental.agents.agent_toolkits import create_python_agent
    from langchain_experimental.tools.python.tool import PythonREPLTool
    

    Classes that still use the old notation:

    from langchain.python import PythonREPL
    from langchain.agents import load_tools, initialize_agent
    from langchain.agents import AgentType
    

    For more information you can read this thread: https://github.com/langchain-ai/langchain/discussions/11680

    Hope that helps