Home / Function/ create_importer() — langchain Function Reference

create_importer() — langchain Function Reference

Architecture documentation for the create_importer() function in module_import.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  cab9d6c1_b626_a774_f867_f39088b84ba0["create_importer()"]
  5452df38_8eed_a0b0_1cd3_c0a4614139e8["module_import.py"]
  cab9d6c1_b626_a774_f867_f39088b84ba0 -->|defined in| 5452df38_8eed_a0b0_1cd3_c0a4614139e8
  style cab9d6c1_b626_a774_f867_f39088b84ba0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/_api/module_import.py lines 16–156

def create_importer(
    package: str,
    *,
    module_lookup: dict[str, str] | None = None,
    deprecated_lookups: dict[str, str] | None = None,
    fallback_module: str | None = None,
) -> Callable[[str], Any]:
    """Create a function that helps retrieve objects from their new locations.

    The goal of this function is to help users transition from deprecated
    imports to new imports.

    The function will raise deprecation warning on loops using
    `deprecated_lookups` or `fallback_module`.

    Module lookups will import without deprecation warnings (used to speed
    up imports from large namespaces like llms or chat models).

    This function should ideally only be used with deprecated imports not with
    existing imports that are valid, as in addition to raising deprecation warnings
    the dynamic imports can create other issues for developers (e.g.,
    loss of type information, IDE support for going to definition etc).

    Args:
        package: Current package. Use `__package__`
        module_lookup: Maps name of object to the module where it is defined.
            e.g.,
            ```json
            {
                "MyDocumentLoader": (
                    "langchain_community.document_loaders.my_document_loader"
                )
            }
            ```
        deprecated_lookups: Same as module look up, but will raise
            deprecation warnings.
        fallback_module: Module to import from if the object is not found in
            `module_lookup` or if `module_lookup` is not provided.

    Returns:
        A function that imports objects from the specified modules.
    """
    all_module_lookup = {**(deprecated_lookups or {}), **(module_lookup or {})}

    def import_by_name(name: str) -> Any:
        """Import stores from `langchain_community`."""
        # If not in interactive env, raise warning.
        if all_module_lookup and name in all_module_lookup:
            new_module = all_module_lookup[name]
            if new_module.split(".")[0] not in ALLOWED_TOP_LEVEL_PKGS:
                msg = (
                    f"Importing from {new_module} is not allowed. "
                    f"Allowed top-level packages are: {ALLOWED_TOP_LEVEL_PKGS}"
                )
                raise AssertionError(msg)

            try:
                module = importlib.import_module(new_module)
            except ModuleNotFoundError as e:
                if new_module.startswith("langchain_community"):
                    msg = (
                        f"Module {new_module} not found. "
                        "Please install langchain-community to access this module. "
                        "You can install it using `pip install -U langchain-community`"
                    )
                    raise ModuleNotFoundError(msg) from e
                raise

            try:
                result = getattr(module, name)
                if (
                    not is_interactive_env()
                    and deprecated_lookups
                    and name in deprecated_lookups
                    # Depth 3:
                    # -> internal.py
                    # |-> module_import.py
                    #  |-> Module in langchain that uses this function
                    #   |-> [calling code] whose frame we want to inspect.
                    and not internal.is_caller_internal(depth=3)
                ):

Domain

Subdomains

Frequently Asked Questions

What does create_importer() do?
create_importer() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/_api/module_import.py.
Where is create_importer() defined?
create_importer() is defined in libs/langchain/langchain_classic/_api/module_import.py at line 16.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free