diff --git a/libs/core/langchain_core/indexing/api.py b/libs/core/langchain_core/indexing/api.py
index 7e938355fb1..fdf7273bad1 100644
--- a/libs/core/langchain_core/indexing/api.py
+++ b/libs/core/langchain_core/indexing/api.py
@@ -294,21 +294,21 @@ def index(
documents were deleted, which documents should be skipped.
For the time being, documents are indexed using their hashes, and users
- are not able to specify the uid of the document.
+ are not able to specify the uid of the document.
Important:
- * In full mode, the loader should be returning
- the entire dataset, and not just a subset of the dataset.
- Otherwise, the auto_cleanup will remove documents that it is not
- supposed to.
- * In incremental mode, if documents associated with a particular
- source id appear across different batches, the indexing API
- will do some redundant work. This will still result in the
- correct end state of the index, but will unfortunately not be
- 100% efficient. For example, if a given document is split into 15
- chunks, and we index them using a batch size of 5, we'll have 3 batches
- all with the same source id. In general, to avoid doing too much
- redundant work select as big a batch size as possible.
+ * In full mode, the loader should be returning
+ the entire dataset, and not just a subset of the dataset.
+ Otherwise, the auto_cleanup will remove documents that it is not
+ supposed to.
+ * In incremental mode, if documents associated with a particular
+ source id appear across different batches, the indexing API
+ will do some redundant work. This will still result in the
+ correct end state of the index, but will unfortunately not be
+ 100% efficient. For example, if a given document is split into 15
+ chunks, and we index them using a batch size of 5, we'll have 3 batches
+ all with the same source id. In general, to avoid doing too much
+ redundant work select as big a batch size as possible.
* The `scoped_full` mode is suitable if determining an appropriate batch size
is challenging or if your data loader cannot return the entire dataset at
once. This mode keeps track of source IDs in memory, which should be fine
@@ -318,23 +318,22 @@ def index(
Args:
docs_source: Data loader or iterable of documents to index.
record_manager: Timestamped set to keep track of which documents were
- updated.
+ updated.
vector_store: VectorStore or DocumentIndex to index the documents into.
batch_size: Batch size to use when indexing. Default is 100.
cleanup: How to handle clean up of documents. Default is None.
+
- incremental: Cleans up all documents that haven't been updated AND
- that are associated with source ids that were seen
- during indexing.
- Clean up is done continuously during indexing helping
- to minimize the probability of users seeing duplicated
- content.
+ that are associated with source ids that were seen during indexing.
+ Clean up is done continuously during indexing helping to minimize the
+ probability of users seeing duplicated content.
- full: Delete all documents that have not been returned by the loader
- during this run of indexing.
- Clean up runs after all documents have been indexed.
- This means that users may see duplicated content during indexing.
+ during this run of indexing.
+ Clean up runs after all documents have been indexed.
+ This means that users may see duplicated content during indexing.
- scoped_full: Similar to Full, but only deletes all documents
- that haven't been updated AND that are associated with
- source ids that were seen during indexing.
+ that haven't been updated AND that are associated with
+ source ids that were seen during indexing.
- None: Do not delete any documents.
source_id_key: Optional key that helps identify the original source
of the document. Default is None.
@@ -361,10 +360,9 @@ def index(
When changing the key encoder, you must change the
index as well to avoid duplicated documents in the cache.
upsert_kwargs: Additional keyword arguments to pass to the add_documents
- method of the VectorStore or the upsert method of the
- DocumentIndex. For example, you can use this to
- specify a custom vector_field:
- upsert_kwargs={"vector_field": "embedding"}
+ method of the VectorStore or the upsert method of the DocumentIndex.
+ For example, you can use this to specify a custom vector_field:
+ upsert_kwargs={"vector_field": "embedding"}
.. versionadded:: 0.3.10
Returns:
@@ -662,22 +660,22 @@ async def aindex(
Args:
docs_source: Data loader or iterable of documents to index.
record_manager: Timestamped set to keep track of which documents were
- updated.
+ updated.
vector_store: VectorStore or DocumentIndex to index the documents into.
batch_size: Batch size to use when indexing. Default is 100.
cleanup: How to handle clean up of documents. Default is None.
+
- incremental: Cleans up all documents that haven't been updated AND
- that are associated with source ids that were seen
- during indexing.
- Clean up is done continuously during indexing helping
- to minimize the probability of users seeing duplicated
- content.
- - full: Delete all documents that haven to been returned by the loader.
- Clean up runs after all documents have been indexed.
- This means that users may see duplicated content during indexing.
+ that are associated with source ids that were seen during indexing.
+ Clean up is done continuously during indexing helping to minimize the
+ probability of users seeing duplicated content.
+ - full: Delete all documents that have not been returned by the loader
+ during this run of indexing.
+ Clean up runs after all documents have been indexed.
+ This means that users may see duplicated content during indexing.
- scoped_full: Similar to Full, but only deletes all documents
- that haven't been updated AND that are associated with
- source ids that were seen during indexing.
+ that haven't been updated AND that are associated with
+ source ids that were seen during indexing.
- None: Do not delete any documents.
source_id_key: Optional key that helps identify the original source
of the document. Default is None.
@@ -686,6 +684,12 @@ async def aindex(
force_update: Force update documents even if they are present in the
record manager. Useful if you are re-indexing with updated embeddings.
Default is False.
+ key_encoder: Hashing algorithm to use for hashing the document content and
+ metadata. Default is "sha1".
+ Other options include "blake2b", "sha256", and "sha512".
+
+ .. versionadded:: 0.3.66
+
key_encoder: Hashing algorithm to use for hashing the document.
If not provided, a default encoder using SHA-1 will be used.
SHA-1 is not collision-resistant, and a motivated attacker
@@ -697,11 +701,10 @@ async def aindex(
When changing the key encoder, you must change the
index as well to avoid duplicated documents in the cache.
- upsert_kwargs: Additional keyword arguments to pass to the aadd_documents
- method of the VectorStore or the aupsert method of the
- DocumentIndex. For example, you can use this to
- specify a custom vector_field:
- upsert_kwargs={"vector_field": "embedding"}
+ upsert_kwargs: Additional keyword arguments to pass to the add_documents
+ method of the VectorStore or the upsert method of the DocumentIndex.
+ For example, you can use this to specify a custom vector_field:
+ upsert_kwargs={"vector_field": "embedding"}
.. versionadded:: 0.3.10
Returns:
diff --git a/libs/core/langchain_core/language_models/base.py b/libs/core/langchain_core/language_models/base.py
index 80be6ff7305..46bb954e95b 100644
--- a/libs/core/langchain_core/language_models/base.py
+++ b/libs/core/langchain_core/language_models/base.py
@@ -186,10 +186,11 @@ class BaseLanguageModel(
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
prompts: List of PromptValues. A PromptValue is an object that can be
@@ -222,10 +223,11 @@ class BaseLanguageModel(
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
prompts: List of PromptValues. A PromptValue is an object that can be
diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py
index a07115196bd..1d931689a3d 100644
--- a/libs/core/langchain_core/language_models/chat_models.py
+++ b/libs/core/langchain_core/language_models/chat_models.py
@@ -767,10 +767,11 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
messages: List of list of messages.
@@ -882,10 +883,11 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
messages: List of list of messages.
diff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.py
index c274e02310d..9516b115d81 100644
--- a/libs/core/langchain_core/language_models/llms.py
+++ b/libs/core/langchain_core/language_models/llms.py
@@ -853,10 +853,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
prompts: List of string prompts.
@@ -1118,10 +1119,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
API.
Use this method when you want to:
- 1. take advantage of batched calls,
- 2. need more output from the model than just the top generated value,
- 3. are building chains that are agnostic to the underlying language model
- type (e.g., pure text completion models vs chat models).
+
+ 1. Take advantage of batched calls,
+ 2. Need more output from the model than just the top generated value,
+ 3. Are building chains that are agnostic to the underlying language model
+ type (e.g., pure text completion models vs chat models).
Args:
prompts: List of string prompts.
diff --git a/libs/core/langchain_core/load/serializable.py b/libs/core/langchain_core/load/serializable.py
index b9d612be45c..d1523bfed53 100644
--- a/libs/core/langchain_core/load/serializable.py
+++ b/libs/core/langchain_core/load/serializable.py
@@ -20,53 +20,41 @@ logger = logging.getLogger(__name__)
class BaseSerialized(TypedDict):
- """Base class for serialized objects.
-
- Parameters:
- lc: The version of the serialization format.
- id: The unique identifier of the object.
- name: The name of the object. Optional.
- graph: The graph of the object. Optional.
- """
+ """Base class for serialized objects."""
lc: int
+ """The version of the serialization format."""
id: list[str]
+ """The unique identifier of the object."""
name: NotRequired[str]
+ """The name of the object. Optional."""
graph: NotRequired[dict[str, Any]]
+ """The graph of the object. Optional."""
class SerializedConstructor(BaseSerialized):
- """Serialized constructor.
-
- Parameters:
- type: The type of the object. Must be "constructor".
- kwargs: The constructor arguments.
- """
+ """Serialized constructor."""
type: Literal["constructor"]
+ """The type of the object. Must be ``'constructor'``."""
kwargs: dict[str, Any]
+ """The constructor arguments."""
class SerializedSecret(BaseSerialized):
- """Serialized secret.
-
- Parameters:
- type: The type of the object. Must be "secret".
- """
+ """Serialized secret."""
type: Literal["secret"]
+ """The type of the object. Must be ``'secret'``."""
class SerializedNotImplemented(BaseSerialized):
- """Serialized not implemented.
-
- Parameters:
- type: The type of the object. Must be "not_implemented".
- repr: The representation of the object. Optional.
- """
+ """Serialized not implemented."""
type: Literal["not_implemented"]
+ """The type of the object. Must be ``'not_implemented'``."""
repr: Optional[str]
+ """The representation of the object. Optional."""
def try_neq_default(value: Any, key: str, model: BaseModel) -> bool:
@@ -106,19 +94,19 @@ class Serializable(BaseModel, ABC):
It relies on the following methods and properties:
- - `is_lc_serializable`: Is this class serializable?
- By design, even if a class inherits from Serializable, it is not serializable by
- default. This is to prevent accidental serialization of objects that should not
- be serialized.
- - `get_lc_namespace`: Get the namespace of the langchain object.
- During deserialization, this namespace is used to identify
- the correct class to instantiate.
- Please see the `Reviver` class in `langchain_core.load.load` for more details.
- During deserialization an additional mapping is handle
- classes that have moved or been renamed across package versions.
- - `lc_secrets`: A map of constructor argument names to secret ids.
- - `lc_attributes`: List of additional attribute names that should be included
- as part of the serialized representation.
+ - ``is_lc_serializable``: Is this class serializable?
+ By design, even if a class inherits from Serializable, it is not serializable by
+ default. This is to prevent accidental serialization of objects that should not
+ be serialized.
+ - ``get_lc_namespace``: Get the namespace of the langchain object.
+ During deserialization, this namespace is used to identify
+ the correct class to instantiate.
+ Please see the ``Reviver`` class in ``langchain_core.load.load`` for more details.
+ During deserialization an additional mapping is handle
+ classes that have moved or been renamed across package versions.
+ - ``lc_secrets``: A map of constructor argument names to secret ids.
+ - ``lc_attributes``: List of additional attribute names that should be included
+ as part of the serialized representation.
"""
# Remove default BaseModel init docstring.
diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.py
index c63f5b1c421..65723ddc3a9 100644
--- a/libs/core/langchain_core/messages/utils.py
+++ b/libs/core/langchain_core/messages/utils.py
@@ -425,8 +425,8 @@ def filter_messages(
exclude_tool_calls: Tool call IDs to exclude. Default is None.
Can be one of the following:
- - ``True``: all AIMessages with tool calls and all ToolMessages will be
- excluded.
+ - ``True``: Each ``AIMessages`` with tool calls and all ``ToolMessages``
+ will be excluded.
- a sequence of tool call IDs to exclude:
- ToolMessages with the corresponding tool call ID will be excluded.
@@ -736,9 +736,11 @@ def trim_messages(
exact token counting is not necessary.
strategy: Strategy for trimming.
+
- "first": Keep the first <= n_count tokens of the messages.
- "last": Keep the last <= n_count tokens of the messages.
- Default is "last".
+
+ Default is ``'last'``.
allow_partial: Whether to split a message if only part of the message can be
included. If ``strategy="last"`` then the last partial contents of a message
are included. If ``strategy="first"`` then the first partial contents of a
@@ -1035,16 +1037,16 @@ def convert_to_openai_messages(
in OpenAI, Anthropic, Bedrock Converse, or VertexAI formats.
text_format: How to format string or text block contents:
- - "string":
- If a message has a string content, this is left as a string. If
- a message has content blocks that are all of type 'text', these are
- joined with a newline to make a single string. If a message has
- content blocks and at least one isn't of type 'text', then
- all blocks are left as dicts.
- - "block":
- If a message has a string content, this is turned into a list
- with a single content block of type 'text'. If a message has content
- blocks these are left as is.
+ - ``'string'``:
+ If a message has a string content, this is left as a string. If
+ a message has content blocks that are all of type 'text', these are
+ joined with a newline to make a single string. If a message has
+ content blocks and at least one isn't of type 'text', then
+ all blocks are left as dicts.
+ - ``'block'``:
+ If a message has a string content, this is turned into a list
+ with a single content block of type 'text'. If a message has content
+ blocks these are left as is.
Raises:
ValueError: if an unrecognized ``text_format`` is specified, or if a message
@@ -1052,12 +1054,13 @@ def convert_to_openai_messages(
Returns:
The return type depends on the input type:
- - dict:
- If a single message-like object is passed in, a single OpenAI message
- dict is returned.
- - list[dict]:
- If a sequence of message-like objects are passed in, a list of OpenAI
- message dicts is returned.
+
+ - dict:
+ If a single message-like object is passed in, a single OpenAI message
+ dict is returned.
+ - list[dict]:
+ If a sequence of message-like objects are passed in, a list of OpenAI
+ message dicts is returned.
Example:
diff --git a/libs/core/langchain_core/output_parsers/xml.py b/libs/core/langchain_core/output_parsers/xml.py
index fcbd68a89d0..0d7ef8b1e79 100644
--- a/libs/core/langchain_core/output_parsers/xml.py
+++ b/libs/core/langchain_core/output_parsers/xml.py
@@ -151,14 +151,15 @@ class XMLOutputParser(BaseTransformOutputParser):
Note this may not be perfect depending on the LLM implementation.
For example, with tags=["foo", "bar", "baz"]:
- 1. A well-formatted XML instance:
- "\n \n \n \n"
- 2. A badly-formatted XML instance (missing closing tag for 'bar'):
- "\n \n "
+ 1. A well-formatted XML instance:
+ "\n \n \n \n"
- 3. A badly-formatted XML instance (unexpected 'tag' element):
- "\n \n \n"
+ 2. A badly-formatted XML instance (missing closing tag for 'bar'):
+ "\n \n "
+
+ 3. A badly-formatted XML instance (unexpected 'tag' element):
+ "\n \n \n"
"""
encoding_matcher: re.Pattern = re.compile(
r"<([^>]*encoding[^>]*)>\n(.*)", re.MULTILINE | re.DOTALL
diff --git a/libs/core/langchain_core/prompts/pipeline.py b/libs/core/langchain_core/prompts/pipeline.py
index ee4ea4b0dca..194979fcfee 100644
--- a/libs/core/langchain_core/prompts/pipeline.py
+++ b/libs/core/langchain_core/prompts/pipeline.py
@@ -39,19 +39,20 @@ class PipelinePromptTemplate(BasePromptTemplate):
This can be useful when you want to reuse parts of prompts.
A PipelinePrompt consists of two main parts:
- - final_prompt: This is the final prompt that is returned
- - pipeline_prompts: This is a list of tuples, consisting
- of a string (`name`) and a Prompt Template.
- Each PromptTemplate will be formatted and then passed
- to future prompt templates as a variable with
- the same name as `name`
+
+ - final_prompt: This is the final prompt that is returned
+ - pipeline_prompts: This is a list of tuples, consisting
+ of a string (``name``) and a Prompt Template.
+ Each PromptTemplate will be formatted and then passed
+ to future prompt templates as a variable with
+ the same name as ``name``
"""
final_prompt: BasePromptTemplate
"""The final prompt that is returned."""
pipeline_prompts: list[tuple[str, BasePromptTemplate]]
- """A list of tuples, consisting of a string (`name`) and a Prompt Template."""
+ """A list of tuples, consisting of a string (``name``) and a Prompt Template."""
@classmethod
def get_lc_namespace(cls) -> list[str]:
diff --git a/libs/core/langchain_core/runnables/branch.py b/libs/core/langchain_core/runnables/branch.py
index bd8e8c64a76..ed8d0ed0cdd 100644
--- a/libs/core/langchain_core/runnables/branch.py
+++ b/libs/core/langchain_core/runnables/branch.py
@@ -44,10 +44,6 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
If no condition evaluates to True, the default branch is run on the input.
- Parameters:
- branches: A list of (condition, Runnable) pairs.
- default: A Runnable to run if no condition is met.
-
Examples:
.. code-block:: python
@@ -67,7 +63,9 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
"""
branches: Sequence[tuple[Runnable[Input, bool], Runnable[Input, Output]]]
+ """A list of (condition, Runnable) pairs."""
default: Runnable[Input, Output]
+ """A Runnable to run if no condition is met."""
def __init__(
self,
diff --git a/libs/core/langchain_core/runnables/configurable.py b/libs/core/langchain_core/runnables/configurable.py
index 46138b64adf..763a10303a6 100644
--- a/libs/core/langchain_core/runnables/configurable.py
+++ b/libs/core/langchain_core/runnables/configurable.py
@@ -51,17 +51,15 @@ if TYPE_CHECKING:
class DynamicRunnable(RunnableSerializable[Input, Output]):
"""Serializable Runnable that can be dynamically configured.
- A DynamicRunnable should be initiated using the `configurable_fields` or
- `configurable_alternatives` method of a Runnable.
-
- Parameters:
- default: The default Runnable to use.
- config: The configuration to use.
+ A DynamicRunnable should be initiated using the ``configurable_fields`` or
+ ``configurable_alternatives`` method of a Runnable.
"""
default: RunnableSerializable[Input, Output]
+ """The default Runnable to use."""
config: Optional[RunnableConfig] = None
+ """The configuration to use."""
model_config = ConfigDict(
arbitrary_types_allowed=True,
@@ -328,9 +326,6 @@ class RunnableConfigurableFields(DynamicRunnable[Input, Output]):
A RunnableConfigurableFields should be initiated using the
`configurable_fields` method of a Runnable.
- Parameters:
- fields: The configurable fields to use.
-
Here is an example of using a RunnableConfigurableFields with LLMs:
.. code-block:: python
@@ -388,6 +383,7 @@ class RunnableConfigurableFields(DynamicRunnable[Input, Output]):
"""
fields: dict[str, AnyConfigurableField]
+ """The configurable fields to use."""
@property
def config_specs(self) -> list[ConfigurableFieldSpec]:
diff --git a/libs/core/langchain_core/runnables/graph.py b/libs/core/langchain_core/runnables/graph.py
index 7dc5ffc2329..389572a23b3 100644
--- a/libs/core/langchain_core/runnables/graph.py
+++ b/libs/core/langchain_core/runnables/graph.py
@@ -62,19 +62,16 @@ def is_uuid(value: str) -> bool:
class Edge(NamedTuple):
- """Edge in a graph.
-
- Parameters:
- source: The source node id.
- target: The target node id.
- data: Optional data associated with the edge. Defaults to None.
- conditional: Whether the edge is conditional. Defaults to False.
- """
+ """Edge in a graph."""
source: str
+ """The source node id."""
target: str
+ """The target node id."""
data: Optional[Stringifiable] = None
+ """Optional data associated with the edge. Defaults to None."""
conditional: bool = False
+ """Whether the edge is conditional. Defaults to False."""
def copy(
self, *, source: Optional[str] = None, target: Optional[str] = None
@@ -97,19 +94,16 @@ class Edge(NamedTuple):
class Node(NamedTuple):
- """Node in a graph.
-
- Parameters:
- id: The unique identifier of the node.
- name: The name of the node.
- data: The data of the node.
- metadata: Optional metadata for the node. Defaults to None.
- """
+ """Node in a graph."""
id: str
+ """The unique identifier of the node."""
name: str
+ """The name of the node."""
data: Union[type[BaseModel], RunnableType, None]
+ """The data of the node."""
metadata: Optional[dict[str, Any]]
+ """Optional metadata for the node. Defaults to None."""
def copy(
self,
@@ -135,16 +129,12 @@ class Node(NamedTuple):
class Branch(NamedTuple):
- """Branch in a graph.
-
- Parameters:
- condition: A callable that returns a string representation of the condition.
- ends: Optional dictionary of end node ids for the branches. Defaults
- to None.
- """
+ """Branch in a graph."""
condition: Callable[..., str]
+ """A callable that returns a string representation of the condition."""
ends: Optional[dict[str, str]]
+ """Optional dictionary of end node ids for the branches. Defaults to None."""
class CurveStyle(Enum):
@@ -168,7 +158,7 @@ class CurveStyle(Enum):
class NodeStyles:
"""Schema for Hexadecimal color codes for different node types.
- Parameters:
+ Args:
default: The default color code. Defaults to "fill:#f2f0ff,line-height:1.2".
first: The color code for the first node. Defaults to "fill-opacity:0".
last: The color code for the last node. Defaults to "fill:#bfb6fc".
@@ -182,8 +172,10 @@ class NodeStyles:
class MermaidDrawMethod(Enum):
"""Enum for different draw methods supported by Mermaid."""
- PYPPETEER = "pyppeteer" # Uses Pyppeteer to render the graph
- API = "api" # Uses Mermaid.INK API to render the graph
+ PYPPETEER = "pyppeteer"
+ """Uses Pyppeteer to render the graph"""
+ API = "api"
+ """Uses Mermaid.INK API to render the graph"""
def node_data_str(
@@ -269,7 +261,7 @@ def node_data_json(
class Graph:
"""Graph of nodes and edges.
- Parameters:
+ Args:
nodes: Dictionary of nodes in the graph. Defaults to an empty dictionary.
edges: List of edges in the graph. Defaults to an empty list.
"""
diff --git a/libs/core/langchain_core/runnables/graph_png.py b/libs/core/langchain_core/runnables/graph_png.py
index c7d15c85127..27162907843 100644
--- a/libs/core/langchain_core/runnables/graph_png.py
+++ b/libs/core/langchain_core/runnables/graph_png.py
@@ -8,25 +8,14 @@ from langchain_core.runnables.graph import Graph, LabelsDict
class PngDrawer:
"""Helper class to draw a state graph into a PNG file.
- It requires `graphviz` and `pygraphviz` to be installed.
- :param fontname: The font to use for the labels
- :param labels: A dictionary of label overrides. The dictionary
- should have the following format:
- {
- "nodes": {
- "node1": "CustomLabel1",
- "node2": "CustomLabel2",
- "__end__": "End Node"
- },
- "edges": {
- "continue": "ContinueLabel",
- "end": "EndLabel"
- }
- }
- The keys are the original labels, and the values are the new labels.
- Usage:
- drawer = PngDrawer()
- drawer.draw(state_graph, 'graph.png')
+ It requires ``graphviz`` and ``pygraphviz`` to be installed.
+
+ Example:
+
+ .. code-block:: python
+
+ drawer = PngDrawer()
+ drawer.draw(state_graph, "graph.png")
"""
def __init__(
diff --git a/libs/core/langchain_core/runnables/history.py b/libs/core/langchain_core/runnables/history.py
index e838ee1e16f..24bfe22df0e 100644
--- a/libs/core/langchain_core/runnables/history.py
+++ b/libs/core/langchain_core/runnables/history.py
@@ -72,20 +72,6 @@ class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef]
For production use cases, you will want to use a persistent implementation
of chat message history, such as ``RedisChatMessageHistory``.
- Parameters:
- get_session_history: Function that returns a new BaseChatMessageHistory.
- This function should either take a single positional argument
- `session_id` of type string and return a corresponding
- chat message history instance.
- input_messages_key: Must be specified if the base runnable accepts a dict
- as input. The key in the input dict that contains the messages.
- output_messages_key: Must be specified if the base Runnable returns a dict
- as output. The key in the output dict that contains the messages.
- history_messages_key: Must be specified if the base runnable accepts a dict
- as input and expects a separate key for historical messages.
- history_factory_config: Configure fields that should be passed to the
- chat history factory. See ``ConfigurableFieldSpec`` for more details.
-
Example: Chat message history with an in-memory implementation for testing.
.. code-block:: python
@@ -242,10 +228,21 @@ class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef]
"""
get_session_history: GetSessionHistoryCallable
+ """Function that returns a new BaseChatMessageHistory.
+ This function should either take a single positional argument ``session_id`` of type
+ string and return a corresponding chat message history instance"""
input_messages_key: Optional[str] = None
+ """Must be specified if the base runnable accepts a dict as input.
+ The key in the input dict that contains the messages."""
output_messages_key: Optional[str] = None
+ """Must be specified if the base Runnable returns a dict as output.
+ The key in the output dict that contains the messages."""
history_messages_key: Optional[str] = None
+ """Must be specified if the base runnable accepts a dict as input and expects a
+ separate key for historical messages."""
history_factory_config: Sequence[ConfigurableFieldSpec]
+ """Configure fields that should be passed to the chat history factory.
+ See ``ConfigurableFieldSpec`` for more details."""
def __init__(
self,
@@ -271,17 +268,21 @@ class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef]
"""Initialize RunnableWithMessageHistory.
Args:
- runnable: The base Runnable to be wrapped. Must take as input one of:
- 1. A list of BaseMessages
+ runnable: The base Runnable to be wrapped.
+ Must take as input one of:
+
+ 1. A list of ``BaseMessage``
2. A dict with one key for all messages
3. A dict with one key for the current input string/message(s) and
- a separate key for historical messages. If the input key points
- to a string, it will be treated as a HumanMessage in history.
+ a separate key for historical messages. If the input key points
+ to a string, it will be treated as a ``HumanMessage`` in history.
Must return as output one of:
- 1. A string which can be treated as an AIMessage
- 2. A BaseMessage or sequence of BaseMessages
- 3. A dict with a key for a BaseMessage or sequence of BaseMessages
+
+ 1. A string which can be treated as an ``AIMessage``
+ 2. A ``BaseMessage`` or sequence of ``BaseMessage``
+ 3. A dict with a key for a ``BaseMessage`` or sequence of
+ ``BaseMessage``
get_session_history: Function that returns a new BaseChatMessageHistory.
This function should either take a single positional argument
diff --git a/libs/core/langchain_core/runnables/router.py b/libs/core/langchain_core/runnables/router.py
index 827e75c6624..0515db43ac8 100644
--- a/libs/core/langchain_core/runnables/router.py
+++ b/libs/core/langchain_core/runnables/router.py
@@ -38,15 +38,12 @@ if TYPE_CHECKING:
class RouterInput(TypedDict):
- """Router input.
-
- Attributes:
- key: The key to route on.
- input: The input to pass to the selected Runnable.
- """
+ """Router input."""
key: str
+ """The key to route on."""
input: Any
+ """The input to pass to the selected Runnable."""
class RouterRunnable(RunnableSerializable[RouterInput, Output]):
diff --git a/libs/core/langchain_core/runnables/utils.py b/libs/core/langchain_core/runnables/utils.py
index eb4c0cd43d5..cebb81dfe50 100644
--- a/libs/core/langchain_core/runnables/utils.py
+++ b/libs/core/langchain_core/runnables/utils.py
@@ -551,22 +551,18 @@ async def aadd(addables: AsyncIterable[Addable]) -> Optional[Addable]:
class ConfigurableField(NamedTuple):
- """Field that can be configured by the user.
-
- Parameters:
- id: The unique identifier of the field.
- name: The name of the field. Defaults to None.
- description: The description of the field. Defaults to None.
- annotation: The annotation of the field. Defaults to None.
- is_shared: Whether the field is shared. Defaults to False.
- """
+ """Field that can be configured by the user."""
id: str
-
+ """The unique identifier of the field."""
name: Optional[str] = None
+ """The name of the field. Defaults to None."""
description: Optional[str] = None
+ """The description of the field. Defaults to None."""
annotation: Optional[Any] = None
+ """The annotation of the field. Defaults to None."""
is_shared: bool = False
+ """Whether the field is shared. Defaults to False."""
@override
def __hash__(self) -> int:
@@ -574,24 +570,20 @@ class ConfigurableField(NamedTuple):
class ConfigurableFieldSingleOption(NamedTuple):
- """Field that can be configured by the user with a default value.
-
- Parameters:
- id: The unique identifier of the field.
- options: The options for the field.
- default: The default value for the field.
- name: The name of the field. Defaults to None.
- description: The description of the field. Defaults to None.
- is_shared: Whether the field is shared. Defaults to False.
- """
+ """Field that can be configured by the user with a default value."""
id: str
+ """The unique identifier of the field."""
options: Mapping[str, Any]
+ """The options for the field."""
default: str
-
+ """The default value for the field."""
name: Optional[str] = None
+ """The name of the field. Defaults to None."""
description: Optional[str] = None
+ """The description of the field. Defaults to None."""
is_shared: bool = False
+ """Whether the field is shared. Defaults to False."""
@override
def __hash__(self) -> int:
@@ -599,24 +591,20 @@ class ConfigurableFieldSingleOption(NamedTuple):
class ConfigurableFieldMultiOption(NamedTuple):
- """Field that can be configured by the user with multiple default values.
-
- Parameters:
- id: The unique identifier of the field.
- options: The options for the field.
- default: The default values for the field.
- name: The name of the field. Defaults to None.
- description: The description of the field. Defaults to None.
- is_shared: Whether the field is shared. Defaults to False.
- """
+ """Field that can be configured by the user with multiple default values."""
id: str
+ """The unique identifier of the field."""
options: Mapping[str, Any]
+ """The options for the field."""
default: Sequence[str]
-
+ """The default values for the field."""
name: Optional[str] = None
+ """The name of the field. Defaults to None."""
description: Optional[str] = None
+ """The description of the field. Defaults to None."""
is_shared: bool = False
+ """Whether the field is shared. Defaults to False."""
@override
def __hash__(self) -> int:
@@ -629,26 +617,22 @@ AnyConfigurableField = Union[
class ConfigurableFieldSpec(NamedTuple):
- """Field that can be configured by the user. It is a specification of a field.
-
- Parameters:
- id: The unique identifier of the field.
- annotation: The annotation of the field.
- name: The name of the field. Defaults to None.
- description: The description of the field. Defaults to None.
- default: The default value for the field. Defaults to None.
- is_shared: Whether the field is shared. Defaults to False.
- dependencies: The dependencies of the field. Defaults to None.
- """
+ """Field that can be configured by the user. It is a specification of a field."""
id: str
+ """The unique identifier of the field."""
annotation: Any
-
+ """The annotation of the field."""
name: Optional[str] = None
+ """The name of the field. Defaults to None."""
description: Optional[str] = None
+ """The description of the field. Defaults to None."""
default: Any = None
+ """The default value for the field. Defaults to None."""
is_shared: bool = False
+ """Whether the field is shared. Defaults to False."""
dependencies: Optional[list[str]] = None
+ """The dependencies of the field. Defaults to None."""
def get_unique_config_specs(
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index d1b18a3aa42..c4de10ddeb7 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -271,15 +271,12 @@ def _function_annotations_are_pydantic_v1(
class _SchemaConfig:
- """Configuration for Pydantic models generated from function signatures.
-
- Attributes:
- extra: Whether to allow extra fields in the model.
- arbitrary_types_allowed: Whether to allow arbitrary types in the model.
- """
+ """Configuration for Pydantic models generated from function signatures."""
extra: str = "forbid"
+ """Whether to allow extra fields in the model."""
arbitrary_types_allowed: bool = True
+ """Whether to allow arbitrary types in the model."""
def create_schema_from_function(
diff --git a/libs/core/langchain_core/tools/convert.py b/libs/core/langchain_core/tools/convert.py
index 84b328f0bed..691550439c1 100644
--- a/libs/core/langchain_core/tools/convert.py
+++ b/libs/core/langchain_core/tools/convert.py
@@ -92,12 +92,13 @@ def tool(
positional argument.
description: Optional description for the tool.
Precedence for the tool description value is as follows:
- - `description` argument
- (used even if docstring and/or `args_schema` are provided)
- - tool function docstring
- (used even if `args_schema` is provided)
- - `args_schema` description
- (used only if `description` / docstring are not provided)
+
+ - ``description`` argument
+ (used even if docstring and/or ``args_schema`` are provided)
+ - tool function docstring
+ (used even if ``args_schema`` is provided)
+ - ``args_schema`` description
+ (used only if `description` / docstring are not provided)
*args: Extra positional arguments. Must be empty.
return_direct: Whether to return directly from the tool rather
than continuing the agent loop. Defaults to False.
diff --git a/libs/core/langchain_core/tracers/evaluation.py b/libs/core/langchain_core/tracers/evaluation.py
index c7447ad4d81..a1e9ffb4785 100644
--- a/libs/core/langchain_core/tracers/evaluation.py
+++ b/libs/core/langchain_core/tracers/evaluation.py
@@ -38,24 +38,27 @@ class EvaluatorCallbackHandler(BaseTracer):
"""Tracer that runs a run evaluator whenever a run is persisted.
Attributes:
- example_id : Union[UUID, None]
- The example ID associated with the runs.
client : Client
The LangSmith client instance used for evaluating the runs.
- evaluators : Sequence[RunEvaluator]
- The sequence of run evaluators to be executed.
- executor : ThreadPoolExecutor
- The thread pool executor used for running the evaluators.
- futures : set[Future]
- The set of futures representing the running evaluators.
- skip_unfinished : bool
- Whether to skip runs that are not finished or raised
- an error.
- project_name : Optional[str]
- The LangSmith project name to be organize eval chain runs under.
"""
name: str = "evaluator_callback_handler"
+ example_id: Optional[UUID] = None
+ """The example ID associated with the runs."""
+ client: langsmith.Client
+ """The LangSmith client instance used for evaluating the runs."""
+ evaluators: Sequence[langsmith.RunEvaluator] = ()
+ """The sequence of run evaluators to be executed."""
+ executor: Optional[ThreadPoolExecutor] = None
+ """The thread pool executor used for running the evaluators."""
+ futures: weakref.WeakSet[Future] = weakref.WeakSet()
+ """The set of futures representing the running evaluators."""
+ skip_unfinished: bool = True
+ """Whether to skip runs that are not finished or raised an error."""
+ project_name: Optional[str] = None
+ """The LangSmith project name to be organize eval chain runs under."""
+ logged_eval_results: dict[tuple[str, str], list[EvaluationResult]]
+ lock: threading.Lock
def __init__(
self,
@@ -91,7 +94,7 @@ class EvaluatorCallbackHandler(BaseTracer):
self.client = client or langchain_tracer.get_client()
self.evaluators = evaluators
if max_concurrency is None:
- self.executor: Optional[ThreadPoolExecutor] = _get_executor()
+ self.executor = _get_executor()
elif max_concurrency > 0:
self.executor = ThreadPoolExecutor(max_workers=max_concurrency)
weakref.finalize(
@@ -100,10 +103,10 @@ class EvaluatorCallbackHandler(BaseTracer):
)
else:
self.executor = None
- self.futures: weakref.WeakSet[Future] = weakref.WeakSet()
+ self.futures = weakref.WeakSet()
self.skip_unfinished = skip_unfinished
self.project_name = project_name
- self.logged_eval_results: dict[tuple[str, str], list[EvaluationResult]] = {}
+ self.logged_eval_results = {}
self.lock = threading.Lock()
_TRACERS.add(self)
@@ -111,12 +114,8 @@ class EvaluatorCallbackHandler(BaseTracer):
"""Evaluate the run in the project.
Args:
- ----------
- run : Run
- The run to be evaluated.
- evaluator : RunEvaluator
- The evaluator to use for evaluating the run.
-
+ run: The run to be evaluated.
+ evaluator: The evaluator to use for evaluating the run.
"""
try:
if self.project_name is None:
@@ -202,10 +201,7 @@ class EvaluatorCallbackHandler(BaseTracer):
"""Run the evaluator on the run.
Args:
- ----------
- run : Run
- The run to be evaluated.
-
+ run: The run to be evaluated.
"""
if self.skip_unfinished and not run.outputs:
logger.debug("Skipping unfinished run %s", run.id)
diff --git a/libs/core/langchain_core/tracers/event_stream.py b/libs/core/langchain_core/tracers/event_stream.py
index b0e44d4eca1..5b150604516 100644
--- a/libs/core/langchain_core/tracers/event_stream.py
+++ b/libs/core/langchain_core/tracers/event_stream.py
@@ -9,13 +9,14 @@ from typing import (
TYPE_CHECKING,
Any,
Optional,
+ TypedDict,
TypeVar,
Union,
cast,
)
from uuid import UUID, uuid4
-from typing_extensions import NotRequired, TypedDict, override
+from typing_extensions import NotRequired, override
from langchain_core.callbacks.base import AsyncCallbackHandler
from langchain_core.messages import AIMessageChunk, BaseMessage, BaseMessageChunk
@@ -53,22 +54,20 @@ class RunInfo(TypedDict):
"""Information about a run.
This is used to keep track of the metadata associated with a run.
-
- Parameters:
- name: The name of the run.
- tags: The tags associated with the run.
- metadata: The metadata associated with the run.
- run_type: The type of the run.
- inputs: The inputs to the run.
- parent_run_id: The ID of the parent run.
"""
name: str
+ """The name of the run."""
tags: list[str]
+ """The tags associated with the run."""
metadata: dict[str, Any]
+ """The metadata associated with the run."""
run_type: str
+ """The type of the run."""
inputs: NotRequired[Any]
+ """The inputs to the run."""
parent_run_id: Optional[UUID]
+ """The ID of the parent run."""
def _assign_name(name: Optional[str], serialized: Optional[dict[str, Any]]) -> str:
diff --git a/libs/core/langchain_core/tracers/root_listeners.py b/libs/core/langchain_core/tracers/root_listeners.py
index efe1a56e24d..5b07553631a 100644
--- a/libs/core/langchain_core/tracers/root_listeners.py
+++ b/libs/core/langchain_core/tracers/root_listeners.py
@@ -21,18 +21,10 @@ AsyncListener = Union[
class RootListenersTracer(BaseTracer):
- """Tracer that calls listeners on run start, end, and error.
-
- Parameters:
- log_missing_parent: Whether to log a warning if the parent is missing.
- Default is False.
- config: The runnable config.
- on_start: The listener to call on run start.
- on_end: The listener to call on run end.
- on_error: The listener to call on run error.
- """
+ """Tracer that calls listeners on run start, end, and error."""
log_missing_parent = False
+ """Whether to log a warning if the parent is missing. Default is False."""
def __init__(
self,
@@ -84,18 +76,10 @@ class RootListenersTracer(BaseTracer):
class AsyncRootListenersTracer(AsyncBaseTracer):
- """Async Tracer that calls listeners on run start, end, and error.
-
- Parameters:
- log_missing_parent: Whether to log a warning if the parent is missing.
- Default is False.
- config: The runnable config.
- on_start: The listener to call on run start.
- on_end: The listener to call on run end.
- on_error: The listener to call on run error.
- """
+ """Async Tracer that calls listeners on run start, end, and error."""
log_missing_parent = False
+ """Whether to log a warning if the parent is missing. Default is False."""
def __init__(
self,
diff --git a/libs/core/langchain_core/tracers/run_collector.py b/libs/core/langchain_core/tracers/run_collector.py
index e7a7dfc7add..afac1ad3267 100644
--- a/libs/core/langchain_core/tracers/run_collector.py
+++ b/libs/core/langchain_core/tracers/run_collector.py
@@ -11,12 +11,6 @@ class RunCollectorCallbackHandler(BaseTracer):
"""Tracer that collects all nested runs in a list.
This tracer is useful for inspection and evaluation purposes.
-
- Parameters
- ----------
- name : str, default="run-collector_callback_handler"
- example_id : Optional[Union[UUID, str]], default=None
- The ID of the example being traced. It can be either a UUID or a string.
"""
name: str = "run-collector_callback_handler"
@@ -26,12 +20,10 @@ class RunCollectorCallbackHandler(BaseTracer):
) -> None:
"""Initialize the RunCollectorCallbackHandler.
- Parameters
- ----------
- example_id : Optional[Union[UUID, str]], default=None
- The ID of the example being traced. It can be either a UUID or a string.
- **kwargs : Any
- Additional keyword arguments
+ Args:
+ example_id: The ID of the example being traced. (default: None).
+ It can be either a UUID or a string.
+ **kwargs: Additional keyword arguments.
"""
super().__init__(**kwargs)
self.example_id = (
@@ -42,10 +34,8 @@ class RunCollectorCallbackHandler(BaseTracer):
def _persist_run(self, run: Run) -> None:
"""Persist a run by adding it to the traced_runs list.
- Parameters
- ----------
- run : Run
- The run to be persisted.
+ Args:
+ run: The run to be persisted.
"""
run_ = run.copy()
run_.reference_example_id = self.example_id
diff --git a/libs/core/langchain_core/utils/json_schema.py b/libs/core/langchain_core/utils/json_schema.py
index 2c0672ab645..54c9a922d34 100644
--- a/libs/core/langchain_core/utils/json_schema.py
+++ b/libs/core/langchain_core/utils/json_schema.py
@@ -119,9 +119,9 @@ def dereference_refs(
full_schema: The complete schema (defaults to schema_obj).
skip_keys:
- If None (the default), we skip recursion under '$defs' *and* only
- shallow-inline refs.
+ shallow-inline refs.
- If provided (even as an empty list), we will recurse under every key and
- deep-inline all refs.
+ deep-inline all refs.
Returns:
The schema with refs dereferenced.
diff --git a/libs/core/langchain_core/vectorstores/base.py b/libs/core/langchain_core/vectorstores/base.py
index 2c4425c9c5a..888136b8929 100644
--- a/libs/core/langchain_core/vectorstores/base.py
+++ b/libs/core/langchain_core/vectorstores/base.py
@@ -429,6 +429,7 @@ class VectorStore(ABC):
"""The 'correct' relevance function.
may differ depending on a few things, including:
+
- the distance / similarity metric used by the VectorStore
- the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
- embedding dimensionality
diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py
index 530ce185743..14a5334a844 100644
--- a/libs/core/tests/unit_tests/runnables/test_runnable.py
+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py
@@ -432,13 +432,7 @@ def test_schemas(snapshot: SnapshotAssertion) -> None:
"$ref": "#/definitions/RouterInput",
"definitions": {
"RouterInput": {
- "description": "Router input.\n"
- "\n"
- "Attributes:\n"
- " key: The key to route "
- "on.\n"
- " input: The input to pass "
- "to the selected Runnable.",
+ "description": "Router input.",
"properties": {
"input": {"title": "Input"},
"key": {"title": "Key", "type": "string"},