AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Langchain-Classic: Importing Moved Agent From Agent_toolkits Raises ValueError Instead Of ImportError

When importing a moved agent (e.g., create_csv_agent) from langchain_classic.agents.agent_toolkits, the __getattr__ helper incorrectly calls as_import_path without specifying relative_to, causing a ValueError from langchain_core instead of the intended ImportError with migration guidance.

mediumConfidence 95%Langchain-ClassicAffected V1.0.8

Origin Analysis

In langchain_classic/agents/agent_toolkits/__init__.py, the __getattr__ function calls as_import_path(Path(__file__).parent, suffix=name) without providing the relative_to argument. The helper defaults to the langchain_core package directory, and since the path is not a subpath of langchain_core, it raises ValueError instead of returning a relative import path for the ImportError message.
1. Install langchain-classic 1.0.8 and langchain-core 1.6.6. 2. Execute: from langchain_classic.agents.agent_toolkits import create_csv_agent 3. Observe ValueError: '.../langchain_classic/agents/agent_toolkits' is not in the subpath of '.../langchain_core'

Fixing Code Block

def __getattr__(name: str) -> Any: """Handle moved agents and raise a helpful ImportError.""" from pathlib import Path from langchain_core._api import as_import_path if name in _MOVED_AGENTS: # langchain_classic package root is three levels up from this file: # agent_toolkits -> agents -> langchain_classic package_root = Path(__file__).resolve().parents[2] relative_path = as_import_path( Path(__file__).parent, suffix=name, relative_to=package_root, ) raise ImportError( f"{name} has been moved to langchain_experimental. " f"Please import it from {relative_path} instead." ) raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
The fix adds the missing relative_to argument to as_import_path, setting it to the langchain_classic package root (obtained via Path(__file__).resolve().parents[2]). This ensures the helper computes a proper relative import path within the langchain_classic package, allowing the intended ImportError to be raised. The original _MOVED_AGENTS set is preserved unchanged.

Edge Case Audit

Using a hardcoded parent traversal (parents[2]) assumes the standard package layout and may fail if the package is installed as a zip, symlinked, or placed in a non-standard directory hierarchy. In such environments, the computed package_root could be incorrect, leading to a different path or even another ValueError. To mitigate, consider deriving the package root from __package__ or using importlib.resources, and add a fallback to the previous behavior if as_import_path fails. Rollback: revert to the original code (without relative_to) if import path resolution breaks unexpectedly in custom installations.

Ecosystem Topology