langchain/libs/langchain_v1/tests/unit_tests/test_imports.py
Eugene Yurtsev 56dde3ade3
feat(langchain): v1 scaffolding (#32166)
This PR adds scaffolding for langchain 1.0 entry package.

Most contents have been removed. 

Currently remaining entrypoints for:

* chat models
* embedding models
* memory -> trimming messages, filtering messages and counting tokens
[we may remove this]
* prompts -> we may remove some prompts
* storage: primarily to support cache backed embeddings, may remove the
kv store
* tools -> report tool primitives

Things to be added:

* Selected agent implementations
* Selected workflows
* Common primitives: messages, Document
* Primitives for type hinting: BaseChatModel, BaseEmbeddings
* Selected retrievers
* Selected text splitters

Things to be removed:

* Globals needs to be removed (needs an update in langchain core)


Todos: 

* TBD indexing api (requires sqlalchemy which we don't want as a
dependency)
* Be explicit about public/private interfaces (e.g., likely rename
chat_models.base.py to something more internal)
* Remove dockerfiles
* Update module doc-strings and README.md
2025-07-24 09:47:48 -04:00

61 lines
2.1 KiB
Python

import importlib
import warnings
from pathlib import Path
# Attempt to recursively import all modules in langchain
PKG_ROOT = Path(__file__).parent.parent.parent
def test_import_all() -> None:
"""Generate the public API for this package."""
with warnings.catch_warnings():
warnings.filterwarnings(action="ignore", category=UserWarning)
library_code = PKG_ROOT / "langchain"
for path in library_code.rglob("*.py"):
# Calculate the relative path to the module
module_name = (
path.relative_to(PKG_ROOT).with_suffix("").as_posix().replace("/", ".")
)
if module_name.endswith("__init__"):
# Without init
module_name = module_name.rsplit(".", 1)[0]
mod = importlib.import_module(module_name)
all_attrs = getattr(mod, "__all__", [])
for name in all_attrs:
# Attempt to import the name from the module
try:
obj = getattr(mod, name)
assert obj is not None
except Exception as e:
msg = f"Could not import {module_name}.{name}"
raise AssertionError(msg) from e
def test_import_all_using_dir() -> None:
"""Generate the public API for this package."""
library_code = PKG_ROOT / "langchain"
for path in library_code.rglob("*.py"):
# Calculate the relative path to the module
module_name = (
path.relative_to(PKG_ROOT).with_suffix("").as_posix().replace("/", ".")
)
if module_name.endswith("__init__"):
# Without init
module_name = module_name.rsplit(".", 1)[0]
try:
mod = importlib.import_module(module_name)
except ModuleNotFoundError as e:
msg = f"Could not import {module_name}"
raise ModuleNotFoundError(msg) from e
attributes = dir(mod)
for name in attributes:
if name.strip().startswith("_"):
continue
# Attempt to import the name from the module
getattr(mod, name)