Implementation:Langchain ai Langchain Package Init Exports
Overview
Concrete __init__.py pattern for defining the public API surface of a LangChain partner package, provided by the langchain-deepseek package.
Description
The langchain_deepseek/__init__.py file demonstrates the canonical pattern for exporting a LangChain partner package's public API. It is minimal by design, containing only imports, version resolution, and the __all__ declaration.
Structure (17 lines total):
- A module docstring identifying the package.
- Import
metadatafromimportlib. - Import the public class (
ChatDeepSeek) from the internal module. - Resolve
__version__viametadata.version(__package__)with aPackageNotFoundErrorfallback. - Delete the
metadatareference to keep the namespace clean. - Declare
__all__listing every public symbol.
This pattern ensures that:
from langchain_deepseek import ChatDeepSeekworks correctly.langchain_deepseek.__version__returns the installed package version.dir(langchain_deepseek)only shows public symbols.
Usage
Use this file as a template when creating the __init__.py for any new LangChain partner integration. The only changes needed are the module docstring, the import path, the class name, and the __all__ entries.
Code Reference
Source Location: libs/partners/deepseek/langchain_deepseek/__init__.py, Lines 1-17
Full source:
"""LangChain DeepSeek integration."""
from importlib import metadata
from langchain_deepseek.chat_models import ChatDeepSeek
try:
__version__ = metadata.version(__package__)
except metadata.PackageNotFoundError:
# Case where package metadata is not available.
__version__ = ""
del metadata # optional, avoids polluting the results of dir(__package__)
__all__ = [
"ChatDeepSeek",
"__version__",
]
Import:
from langchain_deepseek import ChatDeepSeek
from langchain_deepseek import __version__
I/O Contract
| Input | Type | Description |
|---|---|---|
| Package installation | N/A | The package must be installed (via pip/uv) for __version__ to resolve
|
langchain_deepseek.chat_models module |
Python module | Must export ChatDeepSeek
|
| Output | Type | Description |
|---|---|---|
ChatDeepSeek |
type[BaseChatOpenAI] |
The public chat model class |
__version__ |
str |
Installed package version (e.g., "1.1.0") or "" if metadata unavailable
|
__all__ |
list[str] |
["ChatDeepSeek", "__version__"]
|
Usage Examples
Standard import:
from langchain_deepseek import ChatDeepSeek
model = ChatDeepSeek(model="deepseek-chat")
Checking installed version:
import langchain_deepseek
print(langchain_deepseek.__version__)
# Output: "1.1.0"
Verifying public API surface:
import langchain_deepseek
print(langchain_deepseek.__all__)
# Output: ['ChatDeepSeek', '__version__']
Template for a new package:
"""LangChain MyProvider integration."""
from importlib import metadata
from langchain_myprovider.chat_models import ChatMyProvider
try:
__version__ = metadata.version(__package__)
except metadata.PackageNotFoundError:
__version__ = ""
del metadata
__all__ = [
"ChatMyProvider",
"__version__",
]