Skip to content

The agent engineering platform.

docstrings `core` update (#14871)

Leonid Ganeline14d04180ebc6
diff --git a/libs/core/langchain_core/beta/runnables/context.py b/libs/core/langchain_core/beta/runnables/context.py
index c29e88b297..235d8f905e 100644
--- a/libs/core/langchain_core/beta/runnables/context.py
+++ b/libs/core/langchain_core/beta/runnables/context.py
@@ -128,6 +128,15 @@ def aconfig_with_context(
config: RunnableConfig,
steps: List[Runnable],
) -> RunnableConfig:
+ """Asynchronously patch a runnable config with context getters and setters.
+
+ Args:
+ config: The runnable config.
+ steps: The runnable steps.
+
+ Returns:
+ The patched runnable config.
+ """
return _config_with_context(config, steps, _asetter, _agetter, asyncio.Event)
@@ -135,10 +144,21 @@ def config_with_context(
config: RunnableConfig,
steps: List[Runnable],
) -> RunnableConfig:
+ """Patch a runnable config with context getters and setters.
+
+ Args:
+ config: The runnable config.
+ steps: The runnable steps.
+
+ Returns:
+ The patched runnable config.
+ """
return _config_with_context(config, steps, _setter, _getter, threading.Event)
class ContextGet(RunnableSerializable):
+ """Get a context value."""
+
prefix: str = ""
key: Union[str, List[str]]
@@ -197,6 +217,8 @@ def _coerce_set_value(value: SetValue) -> Runnable[Input, Output]:
class ContextSet(RunnableSerializable):
+ """Set a context value."""
+
prefix: str = ""
keys: Mapping[str, Optional[Runnable]]
@@ -276,8 +298,18 @@ class ContextSet(RunnableSerializable):
class Context:
+ """Context for a runnable."""
+
@staticmethod
def create_scope(scope: str, /) -> "PrefixContext":
+ """Create a context scope.
+
+ Args:
+ scope: The scope.
+
+ Returns:
+ The context scope.
+ """
return PrefixContext(prefix=scope)
@staticmethod
@@ -295,6 +327,8 @@ class Context:
class PrefixContext:
+ """Context for a runnable with a prefix."""
+
prefix: str = ""
def __init__(self, prefix: str = ""):
diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py
index 24bb4114cb..b913782b89 100644
--- a/libs/core/langchain_core/language_models/chat_models.py
+++ b/libs/core/langchain_core/language_models/chat_models.py
@@ -57,6 +57,8 @@ def _get_verbosity() -> bool:
def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:
+ """Generate from a stream."""
+
generation: Optional[ChatGenerationChunk] = None
for chunk in stream:
if generation is None:
@@ -77,6 +79,8 @@ def generate_from_stream(stream: Iterator[ChatGenerationChunk]) -> ChatResult:
async def agenerate_from_stream(
stream: AsyncIterator[ChatGenerationChunk],
) -> ChatResult:
+ """Async generate from a stream."""
+
generation: Optional[ChatGenerationChunk] = None
async for chunk in stream:
if generation is None:
diff --git a/libs/core/langchain_core/load/serializable.py b/libs/core/langchain_core/load/serializable.py
index e7733d5a0d..0b54dc7d8a 100644
--- a/libs/core/langchain_core/load/serializable.py
+++ b/libs/core/langchain_core/load/serializable.py
@@ -32,6 +32,16 @@ class SerializedNotImplemented(BaseSerialized):
def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
+ """Try to determine if a value is different from the default.
+
+ Args:
+ value: The value.
+ key: The key.
+ model: The model.
+
+ Returns:
+ Whether the value is different from the default.
+ """
try:
return model.__fields__[key].get_default() != value
except Exception:
diff --git a/libs/core/langchain_core/messages/base.py b/libs/core/langchain_core/messages/base.py
index 96c7665ec7..2076d34060 100644
--- a/libs/core/langchain_core/messages/base.py
+++ b/libs/core/langchain_core/messages/base.py
@@ -47,6 +47,15 @@ def merge_content(
first_content: Union[str, List[Union[str, Dict]]],
second_content: Union[str, List[Union[str, Dict]]],
) -> Union[str, List[Union[str, Dict]]]:
+ """Merge two message contents.
+
+ Args:
+ first_content: The first content.
+ second_content: The second content.
+
+ Returns:
+ The merged content.
+ """
# If first chunk is a string
if isinstance(first_content, str):
# If the second chunk is also a string, then merge them naively
@@ -146,6 +155,14 @@ class BaseMessageChunk(BaseMessage):
def message_to_dict(message: BaseMessage) -> dict:
+ """Convert a Message to a dictionary.
+
+ Args:
+ message: Message to convert.
+
+ Returns:
+ Message as a dict.
+ """
return {"type": message.type, "data": message.dict()}
diff --git a/libs/core/langchain_core/runnables/configurable.py b/libs/core/langchain_core/runnables/configurable.py
index 005b013b3f..d4a55946fb 100644
--- a/libs/core/langchain_core/runnables/configurable.py
+++ b/libs/core/langchain_core/runnables/configurable.py
@@ -422,6 +422,18 @@ def _strremoveprefix(s: str, prefix: str) -> str:
def prefix_config_spec(
spec: ConfigurableFieldSpec, prefix: str
) -> ConfigurableFieldSpec:
+ """Prefix the id of a ConfigurableFieldSpec.
+
+ This is useful when a RunnableConfigurableAlternatives is used as a
+ ConfigurableField of another RunnableConfigurableAlternatives.
+
+ Args:
+ spec: The ConfigurableFieldSpec to prefix.
+ prefix: The prefix to add.
+
+ Returns:
+
+ """
return (
ConfigurableFieldSpec(
id=f"{prefix}/{spec.id}",
diff --git a/libs/core/langchain_core/tracers/context.py b/libs/core/langchain_core/tracers/context.py
index c4d74a955d..4ce3006ae1 100644
--- a/libs/core/langchain_core/tracers/context.py
+++ b/libs/core/langchain_core/tracers/context.py
@@ -205,6 +205,19 @@ def register_configure_hook(
handle_class: Optional[Type[BaseCallbackHandler]] = None,
env_var: Optional[str] = None,
) -> None:
+ """Register a configure hook.
+
+ Args:
+ context_var (ContextVar[Optional[Any]]): The context variable.
+ inheritable (bool): Whether the context variable is inheritable.
+ handle_class (Optional[Type[BaseCallbackHandler]], optional):
+ The callback handler class. Defaults to None.
+ env_var (Optional[str], optional): The environment variable. Defaults to None.
+
+ Raises:
+ ValueError: If env_var is set, handle_class must also be set
+ to a non-None value.
+ """
if env_var is not None and handle_class is None:
raise ValueError(
"If env_var is set, handle_class must also be set to a non-None value."
diff --git a/libs/core/langchain_core/tracers/root_listeners.py b/libs/core/langchain_core/tracers/root_listeners.py
index 63dc5a2b7b..a81db8af8f 100644
--- a/libs/core/langchain_core/tracers/root_listeners.py
+++ b/libs/core/langchain_core/tracers/root_listeners.py
@@ -12,6 +12,8 @@ Listener = Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
class RootListenersTracer(BaseTracer):
+ """A tracer that calls listeners on run start, end, and error."""
+
def __init__(
self,
*,