From 26e0a00c4c5ffb1fd9a53bd71ce489b4664a8583 Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 15 Oct 2025 18:46:55 -0400 Subject: [PATCH] style: more work for refs (#33508) Largely: - Remove explicit `"Default is x"` since new refs show default inferred from sig - Inline code (useful for eventual parsing) - Fix code block rendering (indentations) --- AGENTS.md | 2 + CLAUDE.md | 2 + libs/core/langchain_core/agents.py | 6 +- libs/core/langchain_core/caches.py | 92 +++++---- libs/core/langchain_core/callbacks/base.py | 10 +- libs/core/langchain_core/callbacks/file.py | 4 +- libs/core/langchain_core/callbacks/stdout.py | 2 +- libs/core/langchain_core/chat_history.py | 4 +- .../langchain_core/document_loaders/base.py | 42 ++--- .../document_loaders/langsmith.py | 4 +- libs/core/langchain_core/documents/base.py | 80 ++++---- libs/core/langchain_core/embeddings/fake.py | 6 +- .../example_selectors/semantic_similarity.py | 10 +- libs/core/langchain_core/exceptions.py | 7 +- libs/core/langchain_core/indexing/api.py | 22 +-- .../langchain_core/language_models/_utils.py | 3 +- .../language_models/chat_models.py | 4 +- .../language_models/fake_chat_models.py | 6 +- .../langchain_core/language_models/llms.py | 8 +- libs/core/langchain_core/load/dump.py | 7 +- libs/core/langchain_core/load/load.py | 15 +- libs/core/langchain_core/load/serializable.py | 6 +- libs/core/langchain_core/messages/ai.py | 17 +- libs/core/langchain_core/messages/base.py | 10 +- libs/core/langchain_core/messages/chat.py | 8 +- libs/core/langchain_core/messages/content.py | 13 +- libs/core/langchain_core/messages/function.py | 10 +- libs/core/langchain_core/messages/human.py | 9 +- libs/core/langchain_core/messages/modifier.py | 2 +- libs/core/langchain_core/messages/system.py | 12 +- libs/core/langchain_core/messages/tool.py | 10 +- libs/core/langchain_core/messages/utils.py | 46 ++--- .../langchain_core/output_parsers/base.py | 48 ++--- .../langchain_core/output_parsers/json.py | 1 - .../langchain_core/output_parsers/list.py | 2 +- .../output_parsers/openai_tools.py | 13 +- .../langchain_core/output_parsers/string.py | 2 +- .../core/langchain_core/outputs/generation.py | 2 +- libs/core/langchain_core/prompt_values.py | 14 +- libs/core/langchain_core/prompts/base.py | 8 +- libs/core/langchain_core/prompts/chat.py | 25 ++- libs/core/langchain_core/prompts/dict.py | 4 +- .../prompts/few_shot_with_templates.py | 2 +- libs/core/langchain_core/prompts/image.py | 2 +- libs/core/langchain_core/prompts/message.py | 4 +- libs/core/langchain_core/prompts/prompt.py | 19 +- libs/core/langchain_core/prompts/string.py | 2 +- .../core/langchain_core/prompts/structured.py | 6 +- libs/core/langchain_core/rate_limiters.py | 46 ++--- libs/core/langchain_core/retrievers.py | 58 +++--- libs/core/langchain_core/runnables/base.py | 27 +-- libs/core/langchain_core/runnables/branch.py | 2 +- libs/core/langchain_core/runnables/config.py | 2 +- .../langchain_core/runnables/configurable.py | 4 +- .../langchain_core/runnables/fallbacks.py | 2 +- libs/core/langchain_core/runnables/graph.py | 51 +++-- .../langchain_core/runnables/graph_mermaid.py | 18 +- .../langchain_core/runnables/graph_png.py | 2 +- libs/core/langchain_core/runnables/history.py | 4 +- .../langchain_core/runnables/passthrough.py | 6 +- libs/core/langchain_core/runnables/router.py | 2 +- libs/core/langchain_core/runnables/utils.py | 10 +- libs/core/langchain_core/tools/base.py | 21 +-- libs/core/langchain_core/tools/convert.py | 30 ++- libs/core/langchain_core/tools/retriever.py | 12 +- libs/core/langchain_core/tools/simple.py | 2 +- libs/core/langchain_core/tools/structured.py | 15 +- .../langchain_core/tracers/memory_stream.py | 2 +- .../langchain_core/tracers/root_listeners.py | 4 +- libs/core/langchain_core/tracers/stdout.py | 3 +- libs/core/langchain_core/utils/__init__.py | 2 +- libs/core/langchain_core/utils/aiter.py | 2 +- .../langchain_core/utils/function_calling.py | 53 ++---- libs/core/langchain_core/utils/input.py | 2 +- libs/core/langchain_core/utils/iter.py | 2 +- libs/core/langchain_core/utils/json.py | 2 +- libs/core/langchain_core/utils/strings.py | 2 +- libs/core/langchain_core/vectorstores/base.py | 39 ++-- .../core/langchain_core/vectorstores/utils.py | 4 +- .../language_models/chat_models/test_cache.py | 4 +- .../language_models/llms/test_cache.py | 8 +- .../output_parsers/test_base_parsers.py | 4 +- .../prompts/__snapshots__/test_chat.ambr | 12 +- .../unit_tests/prompts/test_structured.py | 2 +- .../runnables/__snapshots__/test_graph.ambr | 6 +- .../__snapshots__/test_runnable.ambr | 48 ++--- .../langchain_classic/agents/agent.py | 10 +- .../openai_functions.py | 8 +- .../agents/agent_toolkits/vectorstore/base.py | 16 +- .../langchain_classic/agents/chat/base.py | 18 +- .../agents/conversational/base.py | 26 ++- .../agents/conversational_chat/base.py | 14 +- .../agents/format_scratchpad/log.py | 2 - .../format_scratchpad/log_to_messages.py | 2 +- .../agents/json_chat/base.py | 7 +- .../langchain_classic/agents/mrkl/base.py | 10 +- .../agent_token_buffer_memory.py | 15 +- .../agents/openai_functions_agent/base.py | 11 +- .../openai_functions_multi_agent/base.py | 14 +- .../langchain_classic/agents/react/agent.py | 4 +- .../agents/structured_chat/base.py | 4 +- .../langchain_classic/agents/xml/base.py | 4 +- .../callbacks/streamlit/__init__.py | 5 +- .../langchain_classic/chains/base.py | 4 +- .../chains/combine_documents/stuff.py | 12 +- .../chains/elasticsearch_database/base.py | 2 +- .../langchain/langchain_classic/chains/llm.py | 4 +- .../chains/openai_functions/extraction.py | 6 +- .../openai_functions/qa_with_structure.py | 3 +- .../chains/query_constructor/base.py | 16 +- .../langchain_classic/chains/router/base.py | 3 +- .../chains/sql_database/query.py | 3 +- .../chains/summarize/chain.py | 15 +- .../langchain_classic/chat_models/base.py | 61 +++--- .../langchain_classic/embeddings/base.py | 2 +- .../evaluation/exact_match/base.py | 5 +- .../evaluation/regex_match/base.py | 2 +- .../indexes/_sql_record_manager.py | 16 +- .../memory/vectorstore_token_buffer_memory.py | 2 +- .../output_parsers/structured.py | 2 +- .../document_compressors/cohere_rerank.py | 1 - .../document_compressors/embeddings_filter.py | 4 +- .../langchain_classic/retrievers/ensemble.py | 1 - .../smith/evaluation/runner_utils.py | 4 +- .../langchain_classic/utils/__init__.py | 2 +- .../langchain_v1/langchain/agents/__init__.py | 5 +- libs/langchain_v1/langchain/agents/factory.py | 19 +- .../langchain/agents/middleware/__init__.py | 6 +- .../agents/middleware/context_editing.py | 2 +- .../agents/middleware/human_in_the_loop.py | 18 +- .../agents/middleware/model_call_limit.py | 9 +- .../agents/middleware/tool_call_limit.py | 2 +- .../langchain/agents/middleware/types.py | 125 +++++++------ .../langchain/agents/structured_output.py | 2 +- .../langchain/chat_models/__init__.py | 6 +- .../langchain/chat_models/base.py | 174 ++++++++---------- .../langchain_v1/langchain/embeddings/base.py | 2 +- .../langchain_v1/langchain/tools/tool_node.py | 22 ++- .../agents/test_handler_composition.py | 6 +- .../langchain_anthropic/chat_models.py | 2 +- .../langchain_anthropic/output_parsers.py | 2 +- .../tests/integration_tests/test_standard.py | 2 +- .../tests/unit_tests/test_standard.py | 2 +- .../chroma/langchain_chroma/vectorstores.py | 45 ++--- .../langchain_mistralai/chat_models.py | 2 +- .../nomic/langchain_nomic/embeddings.py | 2 +- .../langchain_openai/chat_models/azure.py | 2 +- .../langchain_openai/chat_models/base.py | 7 +- .../langchain_openai/embeddings/base.py | 3 +- .../openai/langchain_openai/llms/azure.py | 2 +- .../openai/langchain_openai/llms/base.py | 2 +- .../langchain_perplexity/chat_models.py | 8 +- .../prompty/langchain_prompty/utils.py | 16 +- .../langchain_qdrant/fastembed_sparse.py | 20 +- .../qdrant/langchain_qdrant/vectorstores.py | 114 ++++++------ .../partners/xai/langchain_xai/chat_models.py | 2 +- libs/standard-tests/README.md | 2 +- .../integration_tests/chat_models.py | 12 +- .../langchain_tests/unit_tests/chat_models.py | 12 +- .../langchain_text_splitters/html.py | 2 +- .../langchain_text_splitters/json.py | 4 +- .../langchain_text_splitters/markdown.py | 1 - 162 files changed, 1017 insertions(+), 1135 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 7fb2a21350f..1e2b420aeff 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -163,9 +163,11 @@ def send_email(to: str, msg: str, *, priority: str = "normal") -> bool: **Documentation Guidelines:** - Types go in function signatures, NOT in docstrings + - If a default is present, DO NOT repeat it in the docstring unless there is post-processing or it is set conditionally. - Focus on "why" rather than "what" in descriptions - Document all parameters, return values, and exceptions - Keep descriptions concise but clear +- Ensure American English spelling (e.g., "behavior", not "behaviour") 📌 *Tip:* Keep descriptions concise but clear. Only document return values if non-obvious. diff --git a/CLAUDE.md b/CLAUDE.md index 7fb2a21350f..1e2b420aeff 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -163,9 +163,11 @@ def send_email(to: str, msg: str, *, priority: str = "normal") -> bool: **Documentation Guidelines:** - Types go in function signatures, NOT in docstrings + - If a default is present, DO NOT repeat it in the docstring unless there is post-processing or it is set conditionally. - Focus on "why" rather than "what" in descriptions - Document all parameters, return values, and exceptions - Keep descriptions concise but clear +- Ensure American English spelling (e.g., "behavior", not "behaviour") 📌 *Tip:* Keep descriptions concise but clear. Only document return values if non-obvious. diff --git a/libs/core/langchain_core/agents.py b/libs/core/langchain_core/agents.py index 62a44623972..fc4e3eaa126 100644 --- a/libs/core/langchain_core/agents.py +++ b/libs/core/langchain_core/agents.py @@ -84,7 +84,7 @@ class AgentAction(Serializable): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "agent"]` @@ -112,7 +112,7 @@ class AgentActionMessageLog(AgentAction): if (tool, tool_input) cannot be used to fully recreate the LLM prediction, and you need that LLM prediction (for future agent iteration). Compared to `log`, this is useful when the underlying LLM is a - ChatModel (and therefore returns messages rather than a string).""" + chat model (and therefore returns messages rather than a string).""" # Ignoring type because we're overriding the type from AgentAction. # And this is the correct thing to do in this case. # The type literal is used for serialization purposes. @@ -161,7 +161,7 @@ class AgentFinish(Serializable): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "agent"]` diff --git a/libs/core/langchain_core/caches.py b/libs/core/langchain_core/caches.py index 9db037e913b..16878faba09 100644 --- a/libs/core/langchain_core/caches.py +++ b/libs/core/langchain_core/caches.py @@ -1,18 +1,15 @@ -"""Cache classes. +"""`caches` provides an optional caching layer for language models. !!! warning - Beta Feature! + This is a beta feature! Please be wary of deploying experimental code to production + unless you've taken appropriate precautions. -**Cache** provides an optional caching layer for LLMs. +A cache is useful for two reasons: -Cache is useful for two reasons: - -- It can save you money by reducing the number of API calls you make to the LLM +1. It can save you money by reducing the number of API calls you make to the LLM provider if you're often requesting the same completion multiple times. -- It can speed up your application by reducing the number of API calls you make - to the LLM provider. - -Cache directly competes with Memory. See documentation for Pros and Cons. +2. It can speed up your application by reducing the number of API calls you make to the + LLM provider. """ from __future__ import annotations @@ -34,8 +31,8 @@ class BaseCache(ABC): The cache interface consists of the following methods: - - lookup: Look up a value based on a prompt and llm_string. - - update: Update the cache based on a prompt and llm_string. + - lookup: Look up a value based on a prompt and `llm_string`. + - update: Update the cache based on a prompt and `llm_string`. - clear: Clear the cache. In addition, the cache interface provides an async version of each method. @@ -47,14 +44,14 @@ class BaseCache(ABC): @abstractmethod def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Look up based on prompt and llm_string. + """Look up based on `prompt` and `llm_string`. A cache implementation is expected to generate a key from the 2-tuple of prompt and llm_string (e.g., by concatenating them with a delimiter). Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM @@ -63,27 +60,27 @@ class BaseCache(ABC): representation. Returns: - On a cache miss, return None. On a cache hit, return the cached value. - The cached value is a list of Generations (or subclasses). + On a cache miss, return `None`. On a cache hit, return the cached value. + The cached value is a list of `Generation` (or subclasses). """ @abstractmethod def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: - """Update cache based on prompt and llm_string. + """Update cache based on `prompt` and `llm_string`. The prompt and llm_string are used to generate a key for the cache. The key should match that of the lookup method. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation. - return_val: The value to be cached. The value is a list of Generations + return_val: The value to be cached. The value is a list of `Generation` (or subclasses). """ @@ -92,14 +89,14 @@ class BaseCache(ABC): """Clear cache that can take additional keyword arguments.""" async def alookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Async look up based on prompt and llm_string. + """Async look up based on `prompt` and `llm_string`. A cache implementation is expected to generate a key from the 2-tuple of prompt and llm_string (e.g., by concatenating them with a delimiter). Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM @@ -108,29 +105,29 @@ class BaseCache(ABC): representation. Returns: - On a cache miss, return None. On a cache hit, return the cached value. - The cached value is a list of Generations (or subclasses). + On a cache miss, return `None`. On a cache hit, return the cached value. + The cached value is a list of `Generation` (or subclasses). """ return await run_in_executor(None, self.lookup, prompt, llm_string) async def aupdate( self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE ) -> None: - """Async update cache based on prompt and llm_string. + """Async update cache based on `prompt` and `llm_string`. The prompt and llm_string are used to generate a key for the cache. The key should match that of the look up method. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation. - return_val: The value to be cached. The value is a list of Generations + return_val: The value to be cached. The value is a list of `Generation` (or subclasses). """ return await run_in_executor(None, self.update, prompt, llm_string, return_val) @@ -150,10 +147,9 @@ class InMemoryCache(BaseCache): maxsize: The maximum number of items to store in the cache. If `None`, the cache has no maximum size. If the cache exceeds the maximum size, the oldest items are removed. - Default is None. Raises: - ValueError: If maxsize is less than or equal to 0. + ValueError: If `maxsize` is less than or equal to `0`. """ self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} if maxsize is not None and maxsize <= 0: @@ -162,28 +158,28 @@ class InMemoryCache(BaseCache): self._maxsize = maxsize def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Look up based on prompt and llm_string. + """Look up based on `prompt` and `llm_string`. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. Returns: - On a cache miss, return None. On a cache hit, return the cached value. + On a cache miss, return `None`. On a cache hit, return the cached value. """ return self._cache.get((prompt, llm_string), None) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: - """Update cache based on prompt and llm_string. + """Update cache based on `prompt` and `llm_string`. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. - return_val: The value to be cached. The value is a list of Generations + return_val: The value to be cached. The value is a list of `Generation` (or subclasses). """ if self._maxsize is not None and len(self._cache) == self._maxsize: @@ -196,30 +192,30 @@ class InMemoryCache(BaseCache): self._cache = {} async def alookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Async look up based on prompt and llm_string. + """Async look up based on `prompt` and `llm_string`. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. Returns: - On a cache miss, return None. On a cache hit, return the cached value. + On a cache miss, return `None`. On a cache hit, return the cached value. """ return self.lookup(prompt, llm_string) async def aupdate( self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE ) -> None: - """Async update cache based on prompt and llm_string. + """Async update cache based on `prompt` and `llm_string`. Args: - prompt: a string representation of the prompt. - In the case of a Chat model, the prompt is a non-trivial + prompt: A string representation of the prompt. + In the case of a chat model, the prompt is a non-trivial serialization of the prompt into the language model. llm_string: A string representation of the LLM configuration. - return_val: The value to be cached. The value is a list of Generations + return_val: The value to be cached. The value is a list of `Generation` (or subclasses). """ self.update(prompt, llm_string, return_val) diff --git a/libs/core/langchain_core/callbacks/base.py b/libs/core/langchain_core/callbacks/base.py index 4265902d0c3..f8877360571 100644 --- a/libs/core/langchain_core/callbacks/base.py +++ b/libs/core/langchain_core/callbacks/base.py @@ -1001,7 +1001,7 @@ class BaseCallbackManager(CallbackManagerMixin): Args: handler: The handler to add. - inherit: Whether to inherit the handler. Default is True. + inherit: Whether to inherit the handler. """ if handler not in self.handlers: self.handlers.append(handler) @@ -1028,7 +1028,7 @@ class BaseCallbackManager(CallbackManagerMixin): Args: handlers: The handlers to set. - inherit: Whether to inherit the handlers. Default is True. + inherit: Whether to inherit the handlers. """ self.handlers = [] self.inheritable_handlers = [] @@ -1044,7 +1044,7 @@ class BaseCallbackManager(CallbackManagerMixin): Args: handler: The handler to set. - inherit: Whether to inherit the handler. Default is True. + inherit: Whether to inherit the handler. """ self.set_handlers([handler], inherit=inherit) @@ -1057,7 +1057,7 @@ class BaseCallbackManager(CallbackManagerMixin): Args: tags: The tags to add. - inherit: Whether to inherit the tags. Default is True. + inherit: Whether to inherit the tags. """ for tag in tags: if tag in self.tags: @@ -1087,7 +1087,7 @@ class BaseCallbackManager(CallbackManagerMixin): Args: metadata: The metadata to add. - inherit: Whether to inherit the metadata. Default is True. + inherit: Whether to inherit the metadata. """ self.metadata.update(metadata) if inherit: diff --git a/libs/core/langchain_core/callbacks/file.py b/libs/core/langchain_core/callbacks/file.py index 8dc9e663ed5..7f0c82531c1 100644 --- a/libs/core/langchain_core/callbacks/file.py +++ b/libs/core/langchain_core/callbacks/file.py @@ -132,7 +132,7 @@ class FileCallbackHandler(BaseCallbackHandler): Args: text: The text to write to the file. color: Optional color for the text. Defaults to `self.color`. - end: String appended after the text. Defaults to `""`. + end: String appended after the text. file: Optional file to write to. Defaults to `self.file`. Raises: @@ -239,7 +239,7 @@ class FileCallbackHandler(BaseCallbackHandler): text: The text to write. color: Color override for this specific output. If `None`, uses `self.color`. - end: String appended after the text. Defaults to `""`. + end: String appended after the text. **kwargs: Additional keyword arguments. """ diff --git a/libs/core/langchain_core/callbacks/stdout.py b/libs/core/langchain_core/callbacks/stdout.py index 003b65a9f93..95259cfb38a 100644 --- a/libs/core/langchain_core/callbacks/stdout.py +++ b/libs/core/langchain_core/callbacks/stdout.py @@ -104,7 +104,7 @@ class StdOutCallbackHandler(BaseCallbackHandler): Args: text: The text to print. color: The color to use for the text. - end: The end character to use. Defaults to "". + end: The end character to use. **kwargs: Additional keyword arguments. """ print_text(text, color=color or self.color, end=end) diff --git a/libs/core/langchain_core/chat_history.py b/libs/core/langchain_core/chat_history.py index a4f135b8dfc..2dbd170f49a 100644 --- a/libs/core/langchain_core/chat_history.py +++ b/libs/core/langchain_core/chat_history.py @@ -152,8 +152,8 @@ class BaseChatMessageHistory(ABC): message: A BaseMessage object to store. Raises: - NotImplementedError: If the sub-class has not implemented an efficient - add_messages method. + `NotImplementedError`: If the sub-class has not implemented an efficient + `add_messages` method. """ if type(self).add_messages != BaseChatMessageHistory.add_messages: # This means that the sub-class has implemented an efficient add_messages diff --git a/libs/core/langchain_core/document_loaders/base.py b/libs/core/langchain_core/document_loaders/base.py index 6f46b99b71c..6da1e4336f9 100644 --- a/libs/core/langchain_core/document_loaders/base.py +++ b/libs/core/langchain_core/document_loaders/base.py @@ -35,38 +35,38 @@ class BaseLoader(ABC): # noqa: B024 # Sub-classes should not implement this method directly. Instead, they # should implement the lazy load method. def load(self) -> list[Document]: - """Load data into Document objects. + """Load data into `Document` objects. Returns: - the documents. + The documents. """ return list(self.lazy_load()) async def aload(self) -> list[Document]: - """Load data into Document objects. + """Load data into `Document` objects. Returns: - the documents. + The documents. """ return [document async for document in self.alazy_load()] def load_and_split( self, text_splitter: TextSplitter | None = None ) -> list[Document]: - """Load Documents and split into chunks. Chunks are returned as Documents. + """Load Documents and split into chunks. Chunks are returned as `Document`. Do not override this method. It should be considered to be deprecated! Args: - text_splitter: TextSplitter instance to use for splitting documents. - Defaults to RecursiveCharacterTextSplitter. + text_splitter: `TextSplitter` instance to use for splitting documents. + Defaults to `RecursiveCharacterTextSplitter`. Raises: - ImportError: If langchain-text-splitters is not installed - and no text_splitter is provided. + ImportError: If `langchain-text-splitters` is not installed + and no `text_splitter` is provided. Returns: - List of Documents. + List of `Document`. """ if text_splitter is None: if not _HAS_TEXT_SPLITTERS: @@ -86,10 +86,10 @@ class BaseLoader(ABC): # noqa: B024 # Attention: This method will be upgraded into an abstractmethod once it's # implemented in all the existing subclasses. def lazy_load(self) -> Iterator[Document]: - """A lazy loader for Documents. + """A lazy loader for `Document`. Yields: - the documents. + The `Document` objects. """ if type(self).load != BaseLoader.load: return iter(self.load()) @@ -97,10 +97,10 @@ class BaseLoader(ABC): # noqa: B024 raise NotImplementedError(msg) async def alazy_load(self) -> AsyncIterator[Document]: - """A lazy loader for Documents. + """A lazy loader for `Document`. Yields: - the documents. + The `Document` objects. """ iterator = await run_in_executor(None, self.lazy_load) done = object() @@ -115,7 +115,7 @@ class BaseBlobParser(ABC): """Abstract interface for blob parsers. A blob parser provides a way to parse raw data stored in a blob into one - or more documents. + or more `Document` objects. The parser can be composed with blob loaders, making it easy to reuse a parser independent of how the blob was originally loaded. @@ -128,25 +128,25 @@ class BaseBlobParser(ABC): Subclasses are required to implement this method. Args: - blob: Blob instance + blob: `Blob` instance Returns: - Generator of documents + Generator of `Document` objects """ def parse(self, blob: Blob) -> list[Document]: - """Eagerly parse the blob into a document or documents. + """Eagerly parse the blob into a `Document` or `Document` objects. This is a convenience method for interactive development environment. - Production applications should favor the lazy_parse method instead. + Production applications should favor the `lazy_parse` method instead. Subclasses should generally not over-ride this parse method. Args: - blob: Blob instance + blob: `Blob` instance Returns: - List of documents + List of `Document` objects """ return list(self.lazy_parse(blob)) diff --git a/libs/core/langchain_core/document_loaders/langsmith.py b/libs/core/langchain_core/document_loaders/langsmith.py index ef604b95cf0..724e743ca49 100644 --- a/libs/core/langchain_core/document_loaders/langsmith.py +++ b/libs/core/langchain_core/document_loaders/langsmith.py @@ -76,8 +76,8 @@ class LangSmithLoader(BaseLoader): splits: A list of dataset splits, which are divisions of your dataset such as 'train', 'test', or 'validation'. Returns examples only from the specified splits. - inline_s3_urls: Whether to inline S3 URLs. Defaults to `True`. - offset: The offset to start from. Defaults to 0. + inline_s3_urls: Whether to inline S3 URLs. + offset: The offset to start from. limit: The maximum number of examples to return. metadata: Metadata to filter by. filter: A structured filter string to apply to the examples. diff --git a/libs/core/langchain_core/documents/base.py b/libs/core/langchain_core/documents/base.py index 8631392ac36..ea367aa43ad 100644 --- a/libs/core/langchain_core/documents/base.py +++ b/libs/core/langchain_core/documents/base.py @@ -57,51 +57,51 @@ class Blob(BaseMedia): Example: Initialize a blob from in-memory data - ```python - from langchain_core.documents import Blob + ```python + from langchain_core.documents import Blob - blob = Blob.from_data("Hello, world!") + blob = Blob.from_data("Hello, world!") - # Read the blob as a string - print(blob.as_string()) + # Read the blob as a string + print(blob.as_string()) - # Read the blob as bytes - print(blob.as_bytes()) + # Read the blob as bytes + print(blob.as_bytes()) - # Read the blob as a byte stream - with blob.as_bytes_io() as f: - print(f.read()) - ``` + # Read the blob as a byte stream + with blob.as_bytes_io() as f: + print(f.read()) + ``` Example: Load from memory and specify mime-type and metadata - ```python - from langchain_core.documents import Blob + ```python + from langchain_core.documents import Blob - blob = Blob.from_data( - data="Hello, world!", - mime_type="text/plain", - metadata={"source": "https://example.com"}, - ) - ``` + blob = Blob.from_data( + data="Hello, world!", + mime_type="text/plain", + metadata={"source": "https://example.com"}, + ) + ``` Example: Load the blob from a file - ```python - from langchain_core.documents import Blob + ```python + from langchain_core.documents import Blob - blob = Blob.from_path("path/to/file.txt") + blob = Blob.from_path("path/to/file.txt") - # Read the blob as a string - print(blob.as_string()) + # Read the blob as a string + print(blob.as_string()) - # Read the blob as bytes - print(blob.as_bytes()) + # Read the blob as bytes + print(blob.as_bytes()) - # Read the blob as a byte stream - with blob.as_bytes_io() as f: - print(f.read()) - ``` + # Read the blob as a byte stream + with blob.as_bytes_io() as f: + print(f.read()) + ``` """ data: bytes | str | None = None @@ -111,7 +111,7 @@ class Blob(BaseMedia): encoding: str = "utf-8" """Encoding to use if decoding the bytes into a string. - Use utf-8 as default encoding, if decoding to string. + Use `utf-8` as default encoding, if decoding to string. """ path: PathLike | None = None """Location where the original content was found.""" @@ -127,7 +127,7 @@ class Blob(BaseMedia): If a path is associated with the blob, it will default to the path location. - Unless explicitly set via a metadata field called "source", in which + Unless explicitly set via a metadata field called `"source"`, in which case that value will be used instead. """ if self.metadata and "source" in self.metadata: @@ -184,7 +184,7 @@ class Blob(BaseMedia): """Read data as a byte stream. Raises: - NotImplementedError: If the blob cannot be represented as a byte stream. + `NotImplementedError`: If the blob cannot be represented as a byte stream. Yields: The data as a byte stream. @@ -211,11 +211,11 @@ class Blob(BaseMedia): """Load the blob from a path like object. Args: - path: path like object to file to be read + path: Path-like object to file to be read encoding: Encoding to use if decoding the bytes into a string - mime_type: if provided, will be set as the mime-type of the data + mime_type: If provided, will be set as the mime-type of the data guess_type: If `True`, the mimetype will be guessed from the file extension, - if a mime-type was not provided + if a mime-type was not provided metadata: Metadata to associate with the blob Returns: @@ -248,10 +248,10 @@ class Blob(BaseMedia): """Initialize the blob from in-memory data. Args: - data: the in-memory data associated with the blob + data: The in-memory data associated with the blob encoding: Encoding to use if decoding the bytes into a string - mime_type: if provided, will be set as the mime-type of the data - path: if provided, will be set as the source from which the data came + mime_type: If provided, will be set as the mime-type of the data + path: If provided, will be set as the source from which the data came metadata: Metadata to associate with the blob Returns: @@ -303,7 +303,7 @@ class Document(BaseMedia): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: ["langchain", "schema", "document"] diff --git a/libs/core/langchain_core/embeddings/fake.py b/libs/core/langchain_core/embeddings/fake.py index 885366d2e62..37260ac7113 100644 --- a/libs/core/langchain_core/embeddings/fake.py +++ b/libs/core/langchain_core/embeddings/fake.py @@ -18,7 +18,8 @@ class FakeEmbeddings(Embeddings, BaseModel): This embedding model creates embeddings by sampling from a normal distribution. - Do not use this outside of testing, as it is not a real embedding model. + !!! warning + Do not use this outside of testing, as it is not a real embedding model. Instantiate: ```python @@ -72,7 +73,8 @@ class DeterministicFakeEmbedding(Embeddings, BaseModel): This embedding model creates embeddings by sampling from a normal distribution with a seed based on the hash of the text. - Do not use this outside of testing, as it is not a real embedding model. + !!! warning + Do not use this outside of testing, as it is not a real embedding model. Instantiate: ```python diff --git a/libs/core/langchain_core/example_selectors/semantic_similarity.py b/libs/core/langchain_core/example_selectors/semantic_similarity.py index 57c3ece590a..27e88f0b645 100644 --- a/libs/core/langchain_core/example_selectors/semantic_similarity.py +++ b/libs/core/langchain_core/example_selectors/semantic_similarity.py @@ -154,7 +154,7 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector): examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vectorstore_cls: A vector store DB interface class, e.g. FAISS. - k: Number of examples to select. Default is 4. + k: Number of examples to select. input_keys: If provided, the search is based on the input variables instead of all variables. example_keys: If provided, keys to filter examples to. @@ -198,7 +198,7 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector): examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vectorstore_cls: A vector store DB interface class, e.g. FAISS. - k: Number of examples to select. Default is 4. + k: Number of examples to select. input_keys: If provided, the search is based on the input variables instead of all variables. example_keys: If provided, keys to filter examples to. @@ -285,9 +285,8 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector): examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vectorstore_cls: A vector store DB interface class, e.g. FAISS. - k: Number of examples to select. Default is 4. + k: Number of examples to select. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. input_keys: If provided, the search is based on the input variables instead of all variables. example_keys: If provided, keys to filter examples to. @@ -333,9 +332,8 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector): examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vectorstore_cls: A vector store DB interface class, e.g. FAISS. - k: Number of examples to select. Default is 4. + k: Number of examples to select. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. input_keys: If provided, the search is based on the input variables instead of all variables. example_keys: If provided, keys to filter examples to. diff --git a/libs/core/langchain_core/exceptions.py b/libs/core/langchain_core/exceptions.py index 9d62c18f62e..a372535339e 100644 --- a/libs/core/langchain_core/exceptions.py +++ b/libs/core/langchain_core/exceptions.py @@ -16,7 +16,7 @@ class OutputParserException(ValueError, LangChainException): # noqa: N818 """Exception that output parsers should raise to signify a parsing error. This exists to differentiate parsing errors from other code or execution errors - that also may arise inside the output parser. OutputParserExceptions will be + that also may arise inside the output parser. `OutputParserException` will be available to catch and handle in ways to fix the parsing error, while other errors will be raised. """ @@ -28,7 +28,7 @@ class OutputParserException(ValueError, LangChainException): # noqa: N818 llm_output: str | None = None, send_to_llm: bool = False, # noqa: FBT001,FBT002 ): - """Create an OutputParserException. + """Create an `OutputParserException`. Args: error: The error that's being re-raised or an error message. @@ -37,11 +37,10 @@ class OutputParserException(ValueError, LangChainException): # noqa: N818 llm_output: String model output which is error-ing. send_to_llm: Whether to send the observation and llm_output back to an Agent - after an OutputParserException has been raised. + after an `OutputParserException` has been raised. This gives the underlying model driving the agent the context that the previous output was improperly structured, in the hopes that it will update the output to the correct format. - Defaults to `False`. Raises: ValueError: If `send_to_llm` is True but either observation or diff --git a/libs/core/langchain_core/indexing/api.py b/libs/core/langchain_core/indexing/api.py index 4ef3776d123..6c75af601fd 100644 --- a/libs/core/langchain_core/indexing/api.py +++ b/libs/core/langchain_core/indexing/api.py @@ -326,8 +326,8 @@ def index( record_manager: Timestamped set to keep track of which documents were 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. + batch_size: Batch size to use when indexing. + cleanup: How to handle clean up of documents. - incremental: Cleans up all documents that haven't been updated AND that are associated with source ids that were seen during indexing. @@ -342,15 +342,12 @@ def index( 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. + of the document. cleanup_batch_size: Batch size to use when cleaning up documents. - Default is 1_000. 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". + metadata. Options include "blake2b", "sha256", and "sha512". !!! version-added "Added in version 0.3.66" @@ -667,8 +664,8 @@ async def aindex( record_manager: Timestamped set to keep track of which documents were 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. + batch_size: Batch size to use when indexing. + cleanup: How to handle clean up of documents. - incremental: Cleans up all documents that haven't been updated AND that are associated with source ids that were seen during indexing. @@ -683,15 +680,12 @@ async def aindex( 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. + of the document. cleanup_batch_size: Batch size to use when cleaning up documents. - Default is 1_000. 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". + metadata. Options include "blake2b", "sha256", and "sha512". !!! version-added "Added in version 0.3.66" diff --git a/libs/core/langchain_core/language_models/_utils.py b/libs/core/langchain_core/language_models/_utils.py index 453031f6776..07c0b4678be 100644 --- a/libs/core/langchain_core/language_models/_utils.py +++ b/libs/core/langchain_core/language_models/_utils.py @@ -89,7 +89,8 @@ class ParsedDataUri(TypedDict): def _parse_data_uri(uri: str) -> ParsedDataUri | None: """Parse a data URI into its components. - If parsing fails, return None. If either MIME type or data is missing, return None. + If parsing fails, return `None`. If either MIME type or data is missing, return + `None`. Example: ```python diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index 67ec50e8207..1b9b84fa8ff 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -1525,8 +1525,8 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC): with keys `'raw'`, `'parsed'`, and `'parsing_error'`. Raises: - ValueError: If there are any unsupported `kwargs`. - NotImplementedError: If the model does not implement + `ValueError`: If there are any unsupported `kwargs`. + `NotImplementedError`: If the model does not implement `with_structured_output()`. Returns: diff --git a/libs/core/langchain_core/language_models/fake_chat_models.py b/libs/core/langchain_core/language_models/fake_chat_models.py index 340f8ad3026..b619d3eb920 100644 --- a/libs/core/langchain_core/language_models/fake_chat_models.py +++ b/libs/core/langchain_core/language_models/fake_chat_models.py @@ -1,4 +1,4 @@ -"""Fake ChatModel for testing purposes.""" +"""Fake chat model for testing purposes.""" import asyncio import re @@ -19,7 +19,7 @@ from langchain_core.runnables import RunnableConfig class FakeMessagesListChatModel(BaseChatModel): - """Fake `ChatModel` for testing purposes.""" + """Fake chat model for testing purposes.""" responses: list[BaseMessage] """List of responses to **cycle** through in order.""" @@ -57,7 +57,7 @@ class FakeListChatModelError(Exception): class FakeListChatModel(SimpleChatModel): - """Fake ChatModel for testing purposes.""" + """Fake chat model for testing purposes.""" responses: list[str] """List of responses to **cycle** through in order.""" diff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.py index e81d40832a9..bd30493c9a4 100644 --- a/libs/core/langchain_core/language_models/llms.py +++ b/libs/core/langchain_core/language_models/llms.py @@ -74,8 +74,8 @@ def create_base_retry_decorator( Args: error_types: List of error types to retry on. - max_retries: Number of retries. Default is 1. - run_manager: Callback manager for the run. Default is None. + max_retries: Number of retries. + run_manager: Callback manager for the run. Returns: A retry decorator. @@ -153,7 +153,7 @@ def get_prompts( Args: params: Dictionary of parameters. prompts: List of prompts. - cache: Cache object. Default is None. + cache: Cache object. Returns: A tuple of existing prompts, llm_string, missing prompt indexes, @@ -189,7 +189,7 @@ async def aget_prompts( Args: params: Dictionary of parameters. prompts: List of prompts. - cache: Cache object. Default is None. + cache: Cache object. Returns: A tuple of existing prompts, llm_string, missing prompt indexes, diff --git a/libs/core/langchain_core/load/dump.py b/libs/core/langchain_core/load/dump.py index 01c886d33bd..f1c07db2fe3 100644 --- a/libs/core/langchain_core/load/dump.py +++ b/libs/core/langchain_core/load/dump.py @@ -42,10 +42,9 @@ def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str: Args: obj: The object to dump. - pretty: Whether to pretty print the json. If true, the json will be - indented with 2 spaces (if no indent is provided as part of kwargs). - Default is False. - **kwargs: Additional arguments to pass to json.dumps + pretty: Whether to pretty print the json. If `True`, the json will be + indented with 2 spaces (if no indent is provided as part of `kwargs`). + **kwargs: Additional arguments to pass to `json.dumps` Returns: A json string representation of the object. diff --git a/libs/core/langchain_core/load/load.py b/libs/core/langchain_core/load/load.py index 6f88786b06e..8fe8bbdfebb 100644 --- a/libs/core/langchain_core/load/load.py +++ b/libs/core/langchain_core/load/load.py @@ -67,12 +67,9 @@ class Reviver: valid_namespaces: A list of additional namespaces (modules) to allow to be deserialized. secrets_from_env: Whether to load secrets from the environment. - Defaults to `True`. additional_import_mappings: A dictionary of additional namespace mappings You can use this to override default mappings or add new mappings. - ignore_unserializable_fields: Whether to ignore unserializable fields. - Defaults to `False`. """ self.secrets_from_env = secrets_from_env self.secrets_map = secrets_map or {} @@ -103,10 +100,10 @@ class Reviver: The revived value. Raises: - ValueError: If the namespace is invalid. - ValueError: If trying to deserialize something that cannot + `ValueError`: If the namespace is invalid. + `ValueError`: If trying to deserialize something that cannot be deserialized in the current version of langchain-core. - NotImplementedError: If the object is not implemented and + `NotImplementedError`: If the object is not implemented and `ignore_unserializable_fields` is False. """ if ( @@ -204,12 +201,9 @@ def loads( valid_namespaces: A list of additional namespaces (modules) to allow to be deserialized. secrets_from_env: Whether to load secrets from the environment. - Defaults to `True`. additional_import_mappings: A dictionary of additional namespace mappings You can use this to override default mappings or add new mappings. - ignore_unserializable_fields: Whether to ignore unserializable fields. - Defaults to `False`. Returns: Revived LangChain objects. @@ -249,12 +243,9 @@ def load( valid_namespaces: A list of additional namespaces (modules) to allow to be deserialized. secrets_from_env: Whether to load secrets from the environment. - Defaults to `True`. additional_import_mappings: A dictionary of additional namespace mappings You can use this to override default mappings or add new mappings. - ignore_unserializable_fields: Whether to ignore unserializable fields. - Defaults to `False`. Returns: Revived LangChain objects. diff --git a/libs/core/langchain_core/load/serializable.py b/libs/core/langchain_core/load/serializable.py index ba729835d08..8234c3141ff 100644 --- a/libs/core/langchain_core/load/serializable.py +++ b/libs/core/langchain_core/load/serializable.py @@ -96,7 +96,7 @@ class Serializable(BaseModel, ABC): 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. + - `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. @@ -127,10 +127,10 @@ class Serializable(BaseModel, ABC): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. For example, if the class is `langchain.llms.openai.OpenAI`, then the - namespace is ["langchain", "llms", "openai"] + namespace is `["langchain", "llms", "openai"]` Returns: The namespace. diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.py index ccaf1f04ef0..167a99ee6c2 100644 --- a/libs/core/langchain_core/messages/ai.py +++ b/libs/core/langchain_core/messages/ai.py @@ -148,7 +148,7 @@ class UsageMetadata(TypedDict): class AIMessage(BaseMessage): """Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -168,7 +168,7 @@ class AIMessage(BaseMessage): """ type: Literal["ai"] = "ai" - """The type of the message (used for deserialization). Defaults to "ai".""" + """The type of the message (used for deserialization).""" @overload def __init__( @@ -191,7 +191,7 @@ class AIMessage(BaseMessage): content_blocks: list[types.ContentBlock] | None = None, **kwargs: Any, ) -> None: - """Initialize `AIMessage`. + """Initializes an `AIMessage`. Specify `content` as positional arg or `content_blocks` for typing. @@ -335,7 +335,6 @@ class AIMessage(BaseMessage): Args: html: Whether to return an HTML-formatted string. - Defaults to `False`. Returns: A pretty representation of the message. @@ -378,17 +377,13 @@ class AIMessageChunk(AIMessage, BaseMessageChunk): # to make sure that the chunk variant can be discriminated from the # non-chunk variant. type: Literal["AIMessageChunk"] = "AIMessageChunk" # type: ignore[assignment] - """The type of the message (used for deserialization). - - Defaults to `AIMessageChunk`. - - """ + """The type of the message (used for deserialization).""" tool_call_chunks: list[ToolCallChunk] = [] """If provided, tool call chunks associated with the message.""" chunk_position: Literal["last"] | None = None - """Optional span represented by an aggregated AIMessageChunk. + """Optional span represented by an aggregated `AIMessageChunk`. If a chunk with `chunk_position="last"` is aggregated into a stream, `tool_call_chunks` in message content will be parsed into `tool_calls`. @@ -553,7 +548,7 @@ class AIMessageChunk(AIMessage, BaseMessageChunk): @model_validator(mode="after") def init_server_tool_calls(self) -> Self: - """Parse server_tool_call_chunks.""" + """Parse `server_tool_call_chunks`.""" if ( self.chunk_position == "last" and self.response_metadata.get("output_version") == "v1" diff --git a/libs/core/langchain_core/messages/base.py b/libs/core/langchain_core/messages/base.py index 0a735eb2829..793c3551102 100644 --- a/libs/core/langchain_core/messages/base.py +++ b/libs/core/langchain_core/messages/base.py @@ -92,7 +92,7 @@ class TextAccessor(str): class BaseMessage(Serializable): """Base abstract message class. - Messages are the inputs and outputs of a `ChatModel`. + Messages are the inputs and outputs of a chat model. """ content: str | list[str | dict] @@ -159,7 +159,7 @@ class BaseMessage(Serializable): content_blocks: list[types.ContentBlock] | None = None, **kwargs: Any, ) -> None: - """Initialize `BaseMessage`. + """Initialize a `BaseMessage`. Specify `content` as positional arg or `content_blocks` for typing. @@ -184,7 +184,7 @@ class BaseMessage(Serializable): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "messages"]` @@ -307,7 +307,7 @@ class BaseMessage(Serializable): Args: html: Whether to format the message as HTML. If `True`, the message will be - formatted with HTML tags. Default is False. + formatted with HTML tags. Returns: A pretty representation of the message. @@ -464,7 +464,7 @@ def get_msg_title_repr(title: str, *, bold: bool = False) -> str: Args: title: The title. - bold: Whether to bold the title. Default is False. + bold: Whether to bold the title. Returns: The title representation. diff --git a/libs/core/langchain_core/messages/chat.py b/libs/core/langchain_core/messages/chat.py index 2050dd7fa0b..6786efcacf4 100644 --- a/libs/core/langchain_core/messages/chat.py +++ b/libs/core/langchain_core/messages/chat.py @@ -19,7 +19,7 @@ class ChatMessage(BaseMessage): """The speaker / role of the Message.""" type: Literal["chat"] = "chat" - """The type of the message (used during serialization). Defaults to "chat".""" + """The type of the message (used during serialization).""" class ChatMessageChunk(ChatMessage, BaseMessageChunk): @@ -29,11 +29,7 @@ class ChatMessageChunk(ChatMessage, BaseMessageChunk): # to make sure that the chunk variant can be discriminated from the # non-chunk variant. type: Literal["ChatMessageChunk"] = "ChatMessageChunk" # type: ignore[assignment] - """The type of the message (used during serialization). - - Defaults to `'ChatMessageChunk'`. - - """ + """The type of the message (used during serialization).""" @override def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore[override] diff --git a/libs/core/langchain_core/messages/content.py b/libs/core/langchain_core/messages/content.py index 889f58cfe29..a544e2d17e0 100644 --- a/libs/core/langchain_core/messages/content.py +++ b/libs/core/langchain_core/messages/content.py @@ -156,7 +156,9 @@ class Citation(TypedDict): """Type of the content block. Used for discrimination.""" id: NotRequired[str] - """Content block identifier. Either: + """Content block identifier. + + Either: - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -201,6 +203,7 @@ class NonStandardAnnotation(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -235,6 +238,7 @@ class TextContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -468,6 +472,7 @@ class ReasoningContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -510,6 +515,7 @@ class ImageContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -557,6 +563,7 @@ class VideoContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -603,6 +610,7 @@ class AudioContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -660,6 +668,7 @@ class PlainTextContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -719,6 +728,7 @@ class FileContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) @@ -781,6 +791,7 @@ class NonStandardContentBlock(TypedDict): """Content block identifier. Either: + - Generated by the provider (e.g., OpenAI's file ID) - Generated by LangChain upon creation (`UUID4` prefixed with `'lc_'`)) diff --git a/libs/core/langchain_core/messages/function.py b/libs/core/langchain_core/messages/function.py index 2bd63e04e69..ee0dad3975f 100644 --- a/libs/core/langchain_core/messages/function.py +++ b/libs/core/langchain_core/messages/function.py @@ -19,7 +19,7 @@ class FunctionMessage(BaseMessage): do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. """ @@ -28,7 +28,7 @@ class FunctionMessage(BaseMessage): """The name of the function that was executed.""" type: Literal["function"] = "function" - """The type of the message (used for serialization). Defaults to `'function'`.""" + """The type of the message (used for serialization).""" class FunctionMessageChunk(FunctionMessage, BaseMessageChunk): @@ -38,11 +38,7 @@ class FunctionMessageChunk(FunctionMessage, BaseMessageChunk): # to make sure that the chunk variant can be discriminated from the # non-chunk variant. type: Literal["FunctionMessageChunk"] = "FunctionMessageChunk" # type: ignore[assignment] - """The type of the message (used for serialization). - - Defaults to `'FunctionMessageChunk'`. - - """ + """The type of the message (used for serialization).""" @override def __add__(self, other: Any) -> BaseMessageChunk: # type: ignore[override] diff --git a/libs/core/langchain_core/messages/human.py b/libs/core/langchain_core/messages/human.py index d4626b6f415..7aa55ad5610 100644 --- a/libs/core/langchain_core/messages/human.py +++ b/libs/core/langchain_core/messages/human.py @@ -27,11 +27,7 @@ class HumanMessage(BaseMessage): """ type: Literal["human"] = "human" - """The type of the message (used for serialization). - - Defaults to `'human'`. - - """ + """The type of the message (used for serialization).""" @overload def __init__( @@ -71,5 +67,4 @@ class HumanMessageChunk(HumanMessage, BaseMessageChunk): # to make sure that the chunk variant can be discriminated from the # non-chunk variant. type: Literal["HumanMessageChunk"] = "HumanMessageChunk" # type: ignore[assignment] - """The type of the message (used for serialization). - Defaults to "HumanMessageChunk".""" + """The type of the message (used for serialization).""" diff --git a/libs/core/langchain_core/messages/modifier.py b/libs/core/langchain_core/messages/modifier.py index 364af242ea5..2175be492e8 100644 --- a/libs/core/langchain_core/messages/modifier.py +++ b/libs/core/langchain_core/messages/modifier.py @@ -9,7 +9,7 @@ class RemoveMessage(BaseMessage): """Message responsible for deleting other messages.""" type: Literal["remove"] = "remove" - """The type of the message (used for serialization). Defaults to "remove".""" + """The type of the message (used for serialization).""" def __init__( self, diff --git a/libs/core/langchain_core/messages/system.py b/libs/core/langchain_core/messages/system.py index 789506ce5ce..4a60811dffc 100644 --- a/libs/core/langchain_core/messages/system.py +++ b/libs/core/langchain_core/messages/system.py @@ -27,11 +27,7 @@ class SystemMessage(BaseMessage): """ type: Literal["system"] = "system" - """The type of the message (used for serialization). - - Defaults to `'system'`. - - """ + """The type of the message (used for serialization).""" @overload def __init__( @@ -71,8 +67,4 @@ class SystemMessageChunk(SystemMessage, BaseMessageChunk): # to make sure that the chunk variant can be discriminated from the # non-chunk variant. type: Literal["SystemMessageChunk"] = "SystemMessageChunk" # type: ignore[assignment] - """The type of the message (used for serialization). - - Defaults to `'SystemMessageChunk'`. - - """ + """The type of the message (used for serialization).""" diff --git a/libs/core/langchain_core/messages/tool.py b/libs/core/langchain_core/messages/tool.py index 47128bf5e42..4a6bfc9f1e2 100644 --- a/libs/core/langchain_core/messages/tool.py +++ b/libs/core/langchain_core/messages/tool.py @@ -60,7 +60,7 @@ class ToolMessage(BaseMessage, ToolOutputMixin): ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. """ @@ -69,11 +69,7 @@ class ToolMessage(BaseMessage, ToolOutputMixin): """Tool call that this message is responding to.""" type: Literal["tool"] = "tool" - """The type of the message (used for serialization). - - Defaults to `'tool'`. - - """ + """The type of the message (used for serialization).""" artifact: Any = None """Artifact of the Tool execution which is not meant to be sent to the model. @@ -164,7 +160,7 @@ class ToolMessage(BaseMessage, ToolOutputMixin): content_blocks: list[types.ContentBlock] | None = None, **kwargs: Any, ) -> None: - """Initialize `ToolMessage`. + """Initialize a `ToolMessage`. Specify `content` as positional arg or `content_blocks` for typing. diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.py index 9b3643c0899..489f2daa781 100644 --- a/libs/core/langchain_core/messages/utils.py +++ b/libs/core/langchain_core/messages/utils.py @@ -96,9 +96,7 @@ def get_buffer_string( Args: messages: Messages to be converted to strings. human_prefix: The prefix to prepend to contents of `HumanMessage`s. - Default is `'Human'`. - ai_prefix: The prefix to prepend to contents of `AIMessage`. Default is - `'AI'`. + ai_prefix: The prefix to prepend to contents of `AIMessage`. Returns: A single string concatenation of all input messages. @@ -227,10 +225,10 @@ def _create_message_from_message_type( Args: message_type: (str) the type of the message (e.g., `'human'`, `'ai'`, etc.). content: (str) the content string. - name: (str) the name of the message. Default is None. - tool_call_id: (str) the tool call id. Default is None. - tool_calls: (list[dict[str, Any]]) the tool calls. Default is None. - id: (str) the id of the message. Default is None. + name: (str) the name of the message. + tool_call_id: (str) the tool call id. + tool_calls: (list[dict[str, Any]]) the tool calls. + id: (str) the id of the message. additional_kwargs: (dict[str, Any]) additional keyword arguments. Returns: @@ -319,11 +317,11 @@ def _convert_to_message(message: MessageLikeRepresentation) -> BaseMessage: message: a representation of a message in one of the supported formats. Returns: - an instance of a message or a message template. + An instance of a message or a message template. Raises: - NotImplementedError: if the message type is not supported. - ValueError: if the message dict does not contain the required keys. + `NotImplementedError`: if the message type is not supported. + `ValueError`: if the message dict does not contain the required keys. """ if isinstance(message, BaseMessage): @@ -425,19 +423,19 @@ def filter_messages( Args: messages: Sequence Message-like objects to filter. - include_names: Message names to include. Default is None. - exclude_names: Messages names to exclude. Default is None. + include_names: Message names to include. + exclude_names: Messages names to exclude. include_types: Message types to include. Can be specified as string names (e.g. `'system'`, `'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...). - Default is None. + exclude_types: Message types to exclude. Can be specified as string names (e.g. `'system'`, `'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...). - Default is None. - include_ids: Message IDs to include. Default is None. - exclude_ids: Message IDs to exclude. Default is None. - exclude_tool_calls: Tool call IDs to exclude. Default is None. + + include_ids: Message IDs to include. + exclude_ids: Message IDs to exclude. + exclude_tool_calls: Tool call IDs to exclude. Can be one of the following: - `True`: all `AIMessage`s with tool calls and all `ToolMessage` objects will be excluded. @@ -568,7 +566,6 @@ def merge_message_runs( Args: messages: Sequence Message-like objects to merge. chunk_separator: Specify the string to be inserted between message chunks. - Defaults to `'\n'`. Returns: list of BaseMessages with consecutive runs of message types merged into single @@ -745,12 +742,10 @@ def trim_messages( 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'`. 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 message are included. - Default is False. end_on: The message type to end on. If specified then every message after the last occurrence of this type is ignored. If `strategy='last'` then this is done before we attempt to get the last `max_tokens`. If @@ -759,7 +754,7 @@ def trim_messages( `'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...). Can be a single type or a list of types. - Default is None. + start_on: The message type to start on. Should only be specified if `strategy='last'`. If specified then every message before the first occurrence of this type is ignored. This is done after we trim @@ -768,10 +763,9 @@ def trim_messages( specified as string names (e.g. `'system'`, `'human'`, `'ai'`, ...) or as `BaseMessage` classes (e.g. `SystemMessage`, `HumanMessage`, `AIMessage`, ...). Can be a single type or a list of types. - Default is None. + include_system: Whether to keep the SystemMessage if there is one at index 0. Should only be specified if `strategy="last"`. - Default is False. text_splitter: Function or `langchain_text_splitters.TextSplitter` for splitting the string contents of a message. Only used if `allow_partial=True`. If `strategy='last'` then the last split tokens @@ -1683,11 +1677,11 @@ def count_tokens_approximately( Args: messages: List of messages to count tokens for. chars_per_token: Number of characters per token to use for the approximation. - Default is 4 (one token corresponds to ~4 chars for common English text). + One token corresponds to ~4 chars for common English text. You can also specify float values for more fine-grained control. [See more here](https://platform.openai.com/tokenizer). - extra_tokens_per_message: Number of extra tokens to add per message. - Default is 3 (special tokens, including beginning/end of message). + extra_tokens_per_message: Number of extra tokens to add per message, e.g. + special tokens, including beginning/end of message. You can also specify float values for more fine-grained control. [See more here](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb). count_name: Whether to include message names in the count. diff --git a/libs/core/langchain_core/output_parsers/base.py b/libs/core/langchain_core/output_parsers/base.py index 6786ef7c058..1ef27eb11aa 100644 --- a/libs/core/langchain_core/output_parsers/base.py +++ b/libs/core/langchain_core/output_parsers/base.py @@ -31,13 +31,13 @@ class BaseLLMOutputParser(ABC, Generic[T]): @abstractmethod def parse_result(self, result: list[Generation], *, partial: bool = False) -> T: - """Parse a list of candidate model Generations into a specific format. + """Parse a list of candidate model `Generation` objects into a specific format. Args: - result: A list of Generations to be parsed. The Generations are assumed - to be different candidate outputs for a single model input. + result: A list of `Generation` to be parsed. The `Generation` objects are + assumed to be different candidate outputs for a single model input. partial: Whether to parse the output as a partial result. This is useful - for parsers that can parse partial results. Default is False. + for parsers that can parse partial results. Returns: Structured output. @@ -46,17 +46,17 @@ class BaseLLMOutputParser(ABC, Generic[T]): async def aparse_result( self, result: list[Generation], *, partial: bool = False ) -> T: - """Async parse a list of candidate model Generations into a specific format. + """Async parse a list of candidate model `Generation` objects into a specific format. Args: - result: A list of Generations to be parsed. The Generations are assumed + result: A list of `Generation` to be parsed. The Generations are assumed to be different candidate outputs for a single model input. partial: Whether to parse the output as a partial result. This is useful - for parsers that can parse partial results. Default is False. + for parsers that can parse partial results. Returns: Structured output. - """ + """ # noqa: E501 return await run_in_executor(None, self.parse_result, result, partial=partial) @@ -172,7 +172,7 @@ class BaseOutputParser( This property is inferred from the first type argument of the class. Raises: - TypeError: If the class doesn't have an inferable OutputType. + TypeError: If the class doesn't have an inferable `OutputType`. """ for base in self.__class__.mro(): if hasattr(base, "__pydantic_generic_metadata__"): @@ -234,16 +234,16 @@ class BaseOutputParser( @override def parse_result(self, result: list[Generation], *, partial: bool = False) -> T: - """Parse a list of candidate model Generations into a specific format. + """Parse a list of candidate model `Generation` objects into a specific format. - The return value is parsed from only the first Generation in the result, which - is assumed to be the highest-likelihood Generation. + The return value is parsed from only the first `Generation` in the result, which + is assumed to be the highest-likelihood `Generation`. Args: - result: A list of Generations to be parsed. The Generations are assumed - to be different candidate outputs for a single model input. + result: A list of `Generation` to be parsed. The `Generation` objects are + assumed to be different candidate outputs for a single model input. partial: Whether to parse the output as a partial result. This is useful - for parsers that can parse partial results. Default is False. + for parsers that can parse partial results. Returns: Structured output. @@ -264,20 +264,20 @@ class BaseOutputParser( async def aparse_result( self, result: list[Generation], *, partial: bool = False ) -> T: - """Async parse a list of candidate model Generations into a specific format. + """Async parse a list of candidate model `Generation` objects into a specific format. - The return value is parsed from only the first Generation in the result, which - is assumed to be the highest-likelihood Generation. + The return value is parsed from only the first `Generation` in the result, which + is assumed to be the highest-likelihood `Generation`. Args: - result: A list of Generations to be parsed. The Generations are assumed - to be different candidate outputs for a single model input. + result: A list of `Generation` to be parsed. The `Generation` objects are + assumed to be different candidate outputs for a single model input. partial: Whether to parse the output as a partial result. This is useful - for parsers that can parse partial results. Default is False. + for parsers that can parse partial results. Returns: Structured output. - """ + """ # noqa: E501 return await run_in_executor(None, self.parse_result, result, partial=partial) async def aparse(self, text: str) -> T: @@ -299,13 +299,13 @@ class BaseOutputParser( ) -> Any: """Parse the output of an LLM call with the input prompt for context. - The prompt is largely provided in the event the OutputParser wants + The prompt is largely provided in the event the `OutputParser` wants to retry or fix the output in some way, and needs information from the prompt to do so. Args: completion: String output of a language model. - prompt: Input PromptValue. + prompt: Input `PromptValue`. Returns: Structured output. diff --git a/libs/core/langchain_core/output_parsers/json.py b/libs/core/langchain_core/output_parsers/json.py index d665c33b48f..9daacf1ad53 100644 --- a/libs/core/langchain_core/output_parsers/json.py +++ b/libs/core/langchain_core/output_parsers/json.py @@ -62,7 +62,6 @@ class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]): If `True`, the output will be a JSON object containing all the keys that have been returned so far. If `False`, the output will be the full JSON object. - Default is False. Returns: The parsed JSON object. diff --git a/libs/core/langchain_core/output_parsers/list.py b/libs/core/langchain_core/output_parsers/list.py index f2c087225c3..22c77058a3e 100644 --- a/libs/core/langchain_core/output_parsers/list.py +++ b/libs/core/langchain_core/output_parsers/list.py @@ -146,7 +146,7 @@ class CommaSeparatedListOutputParser(ListOutputParser): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "output_parsers", "list"]` diff --git a/libs/core/langchain_core/output_parsers/openai_tools.py b/libs/core/langchain_core/output_parsers/openai_tools.py index 5f44b916ae2..d1d254170b4 100644 --- a/libs/core/langchain_core/output_parsers/openai_tools.py +++ b/libs/core/langchain_core/output_parsers/openai_tools.py @@ -31,10 +31,9 @@ def parse_tool_call( Args: raw_tool_call: The raw tool call to parse. - partial: Whether to parse partial JSON. Default is False. + partial: Whether to parse partial JSON. strict: Whether to allow non-JSON-compliant strings. - Default is False. - return_id: Whether to return the tool call id. Default is True. + return_id: Whether to return the tool call id. Returns: The parsed tool call. @@ -105,10 +104,9 @@ def parse_tool_calls( Args: raw_tool_calls: The raw tool calls to parse. - partial: Whether to parse partial JSON. Default is False. + partial: Whether to parse partial JSON. strict: Whether to allow non-JSON-compliant strings. - Default is False. - return_id: Whether to return the tool call id. Default is True. + return_id: Whether to return the tool call id. Returns: The parsed tool calls. @@ -165,7 +163,6 @@ class JsonOutputToolsParser(BaseCumulativeTransformOutputParser[Any]): If `True`, the output will be a JSON object containing all the keys that have been returned so far. If `False`, the output will be the full JSON object. - Default is False. Returns: The parsed tool calls. @@ -229,7 +226,6 @@ class JsonOutputKeyToolsParser(JsonOutputToolsParser): If `True`, the output will be a JSON object containing all the keys that have been returned so far. If `False`, the output will be the full JSON object. - Default is False. Raises: OutputParserException: If the generation is not a chat generation. @@ -313,7 +309,6 @@ class PydanticToolsParser(JsonOutputToolsParser): If `True`, the output will be a JSON object containing all the keys that have been returned so far. If `False`, the output will be the full JSON object. - Default is False. Returns: The parsed Pydantic objects. diff --git a/libs/core/langchain_core/output_parsers/string.py b/libs/core/langchain_core/output_parsers/string.py index 566daa25d82..456fcdc83b8 100644 --- a/libs/core/langchain_core/output_parsers/string.py +++ b/libs/core/langchain_core/output_parsers/string.py @@ -19,7 +19,7 @@ class StrOutputParser(BaseTransformOutputParser[str]): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "output_parser"]` diff --git a/libs/core/langchain_core/outputs/generation.py b/libs/core/langchain_core/outputs/generation.py index 960563dac99..913204c0e14 100644 --- a/libs/core/langchain_core/outputs/generation.py +++ b/libs/core/langchain_core/outputs/generation.py @@ -44,7 +44,7 @@ class Generation(Serializable): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "output"]` diff --git a/libs/core/langchain_core/prompt_values.py b/libs/core/langchain_core/prompt_values.py index 8380a3e067a..99b063bc5bc 100644 --- a/libs/core/langchain_core/prompt_values.py +++ b/libs/core/langchain_core/prompt_values.py @@ -24,8 +24,8 @@ from langchain_core.messages import ( class PromptValue(Serializable, ABC): """Base abstract class for inputs to any language model. - PromptValues can be converted to both LLM (pure text-generation) inputs and - ChatModel inputs. + `PromptValues` can be converted to both LLM (pure text-generation) inputs and + chat model inputs. """ @classmethod @@ -35,7 +35,7 @@ class PromptValue(Serializable, ABC): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. This is used to determine the namespace of the object when serializing. @@ -62,7 +62,7 @@ class StringPromptValue(PromptValue): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. This is used to determine the namespace of the object when serializing. @@ -99,7 +99,7 @@ class ChatPromptValue(PromptValue): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. This is used to determine the namespace of the object when serializing. @@ -113,11 +113,11 @@ class ImageURL(TypedDict, total=False): """Image URL.""" detail: Literal["auto", "low", "high"] - """Specifies the detail level of the image. Defaults to `'auto'`. + """Specifies the detail level of the image. + Can be `'auto'`, `'low'`, or `'high'`. This follows OpenAI's Chat Completion API's image URL format. - """ url: str diff --git a/libs/core/langchain_core/prompts/base.py b/libs/core/langchain_core/prompts/base.py index 43964fe0349..415fc92b62c 100644 --- a/libs/core/langchain_core/prompts/base.py +++ b/libs/core/langchain_core/prompts/base.py @@ -96,7 +96,7 @@ class BasePromptTemplate( @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "prompt_template"]` @@ -343,9 +343,9 @@ class BasePromptTemplate( file_path: Path to directory to save prompt to. Raises: - ValueError: If the prompt has partial variables. - ValueError: If the file path is not json or yaml. - NotImplementedError: If the prompt type is not implemented. + `ValueError`: If the prompt has partial variables. + `ValueError`: If the file path is not json or yaml. + `NotImplementedError`: If the prompt type is not implemented. Example: ```python diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.py index 1bcdb4bae6d..99b1b7451c1 100644 --- a/libs/core/langchain_core/prompts/chat.py +++ b/libs/core/langchain_core/prompts/chat.py @@ -147,7 +147,6 @@ class MessagesPlaceholder(BaseMessagePromptTemplate): optional: If `True` format_messages can be called with no arguments and will return an empty list. If `False` then a named argument with name `variable_name` must be passed in, even if the value is an empty list. - Defaults to `False`.] """ # mypy can't detect the init which is defined in the parent class # b/c these are BaseModel classes. @@ -195,7 +194,7 @@ class MessagesPlaceholder(BaseMessagePromptTemplate): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. @@ -235,7 +234,7 @@ class BaseStringMessagePromptTemplate(BaseMessagePromptTemplate, ABC): Args: template: a template. - template_format: format of the template. Defaults to "f-string". + template_format: format of the template. partial_variables: A dictionary of variables that can be used to partially fill in the template. For example, if the template is `"{variable1} {variable2}"`, and `partial_variables` is @@ -330,7 +329,7 @@ class BaseStringMessagePromptTemplate(BaseMessagePromptTemplate, ABC): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. @@ -412,7 +411,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate): Args: template: a template. template_format: format of the template. - Options are: 'f-string', 'mustache', 'jinja2'. Defaults to "f-string". + Options are: 'f-string', 'mustache', 'jinja2'. partial_variables: A dictionary of variables that can be used too partially. **kwargs: keyword arguments to pass to the constructor. @@ -637,7 +636,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. @@ -750,7 +749,7 @@ class BaseChatPromptTemplate(BasePromptTemplate, ABC): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. @@ -905,7 +904,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate): (message type, template); e.g., ("human", "{user_input}"), (4) 2-tuple of (message class, template), (5) a string which is shorthand for ("human", template); e.g., "{user_input}". - template_format: format of the template. Defaults to "f-string". + template_format: format of the template. input_variables: A list of the names of the variables whose values are required as inputs to the prompt. optional_variables: A list of the names of the variables for placeholder @@ -971,7 +970,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "chat"]` @@ -1128,7 +1127,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate): (message type, template); e.g., ("human", "{user_input}"), (4) 2-tuple of (message class, template), (5) a string which is shorthand for ("human", template); e.g., "{user_input}". - template_format: format of the template. Defaults to "f-string". + template_format: format of the template. Returns: a chat prompt template. @@ -1287,7 +1286,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. @@ -1306,7 +1305,7 @@ def _create_template_from_message_type( Args: message_type: str the type of the message template (e.g., "human", "ai", etc.) template: str the template string. - template_format: format of the template. Defaults to "f-string". + template_format: format of the template. Returns: a message prompt template of the appropriate type. @@ -1383,7 +1382,7 @@ def _convert_to_message_template( Args: message: a representation of a message in one of the supported formats. - template_format: format of the template. Defaults to "f-string". + template_format: format of the template. Returns: an instance of a message or a message template. diff --git a/libs/core/langchain_core/prompts/dict.py b/libs/core/langchain_core/prompts/dict.py index a8b0094f78e..b8e3a49c052 100644 --- a/libs/core/langchain_core/prompts/dict.py +++ b/libs/core/langchain_core/prompts/dict.py @@ -74,7 +74,7 @@ class DictPromptTemplate(RunnableSerializable[dict, dict]): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain_core", "prompts", "dict"]` @@ -85,7 +85,7 @@ class DictPromptTemplate(RunnableSerializable[dict, dict]): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. diff --git a/libs/core/langchain_core/prompts/few_shot_with_templates.py b/libs/core/langchain_core/prompts/few_shot_with_templates.py index 7d0997da9d5..0693de2205f 100644 --- a/libs/core/langchain_core/prompts/few_shot_with_templates.py +++ b/libs/core/langchain_core/prompts/few_shot_with_templates.py @@ -46,7 +46,7 @@ class FewShotPromptWithTemplates(StringPromptTemplate): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "few_shot_with_templates"]` diff --git a/libs/core/langchain_core/prompts/image.py b/libs/core/langchain_core/prompts/image.py index 26987576641..c650a032e73 100644 --- a/libs/core/langchain_core/prompts/image.py +++ b/libs/core/langchain_core/prompts/image.py @@ -49,7 +49,7 @@ class ImagePromptTemplate(BasePromptTemplate[ImageURL]): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "image"]` diff --git a/libs/core/langchain_core/prompts/message.py b/libs/core/langchain_core/prompts/message.py index d1938ff4d4c..9ae86ab0596 100644 --- a/libs/core/langchain_core/prompts/message.py +++ b/libs/core/langchain_core/prompts/message.py @@ -23,7 +23,7 @@ class BaseMessagePromptTemplate(Serializable, ABC): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "chat"]` @@ -68,7 +68,7 @@ class BaseMessagePromptTemplate(Serializable, ABC): """Human-readable representation. Args: - html: Whether to format as HTML. Defaults to `False`. + html: Whether to format as HTML. Returns: Human-readable representation. diff --git a/libs/core/langchain_core/prompts/prompt.py b/libs/core/langchain_core/prompts/prompt.py index 9f6a605b46c..9c763e11a56 100644 --- a/libs/core/langchain_core/prompts/prompt.py +++ b/libs/core/langchain_core/prompts/prompt.py @@ -66,7 +66,7 @@ class PromptTemplate(StringPromptTemplate): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "prompt"]` @@ -140,9 +140,9 @@ class PromptTemplate(StringPromptTemplate): """Override the + operator to allow for combining prompt templates. Raises: - ValueError: If the template formats are not f-string or if there are + `ValueError`: If the template formats are not f-string or if there are conflicting partial variables. - NotImplementedError: If the other object is not a `PromptTemplate` or str. + `NotImplementedError`: If the other object is not a `PromptTemplate` or str. Returns: A new `PromptTemplate` that is the combination of the two. @@ -220,7 +220,7 @@ class PromptTemplate(StringPromptTemplate): example_separator: The separator to use in between examples. Defaults to two new line characters. prefix: String that should go before any examples. Generally includes - examples. Default to an empty string. + examples. Returns: The final prompt generated. @@ -275,13 +275,12 @@ class PromptTemplate(StringPromptTemplate): Args: template: The template to load. template_format: The format of the template. Use `jinja2` for jinja2, - `mustache` for mustache, and `f-string` for f-strings. - Defaults to `f-string`. + `mustache` for mustache, and `f-string` for f-strings. partial_variables: A dictionary of variables that can be used to partially - fill in the template. For example, if the template is - `"{variable1} {variable2}"`, and `partial_variables` is - `{"variable1": "foo"}`, then the final prompt will be - `"foo {variable2}"`. + fill in the template. For example, if the template is + `"{variable1} {variable2}"`, and `partial_variables` is + `{"variable1": "foo"}`, then the final prompt will be + `"foo {variable2}"`. **kwargs: Any other arguments to pass to the prompt template. Returns: diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py index a689fb4b6b8..94cd0c70cf6 100644 --- a/libs/core/langchain_core/prompts/string.py +++ b/libs/core/langchain_core/prompts/string.py @@ -276,7 +276,7 @@ class StringPromptTemplate(BasePromptTemplate, ABC): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "prompts", "base"]` diff --git a/libs/core/langchain_core/prompts/structured.py b/libs/core/langchain_core/prompts/structured.py index e9bb54d6c7d..d1eb89a9517 100644 --- a/libs/core/langchain_core/prompts/structured.py +++ b/libs/core/langchain_core/prompts/structured.py @@ -63,13 +63,13 @@ class StructuredPrompt(ChatPromptTemplate): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. For example, if the class is `langchain.llms.openai.OpenAI`, then the namespace is `["langchain", "llms", "openai"]` Returns: - The namespace of the langchain object. + The namespace of the LangChain object. """ return cls.__module__.split(".") @@ -150,7 +150,7 @@ class StructuredPrompt(ChatPromptTemplate): A RunnableSequence object. Raises: - NotImplementedError: If the first element of `others` + `NotImplementedError`: If the first element of `others` is not a language model. """ if (others and isinstance(others[0], BaseLanguageModel)) or hasattr( diff --git a/libs/core/langchain_core/rate_limiters.py b/libs/core/langchain_core/rate_limiters.py index d15335679e9..2c1a77d2d88 100644 --- a/libs/core/langchain_core/rate_limiters.py +++ b/libs/core/langchain_core/rate_limiters.py @@ -21,8 +21,8 @@ class BaseRateLimiter(abc.ABC): Current limitations: - Rate limiting information is not surfaced in tracing or callbacks. This means - that the total time it takes to invoke a chat model will encompass both - the time spent waiting for tokens and the time spent making the request. + that the total time it takes to invoke a chat model will encompass both + the time spent waiting for tokens and the time spent making the request. !!! version-added "Added in version 0.2.24" @@ -33,18 +33,18 @@ class BaseRateLimiter(abc.ABC): """Attempt to acquire the necessary tokens for the rate limiter. This method blocks until the required tokens are available if `blocking` - is set to True. + is set to `True`. - If `blocking` is set to False, the method will immediately return the result + If `blocking` is set to `False`, the method will immediately return the result of the attempt to acquire the tokens. Args: blocking: If `True`, the method will block until the tokens are available. If `False`, the method will return immediately with the result of - the attempt. Defaults to `True`. + the attempt. Returns: - `True` if the tokens were successfully acquired, `False` otherwise. + `True` if the tokens were successfully acquired, `False` otherwise. """ @abc.abstractmethod @@ -52,18 +52,18 @@ class BaseRateLimiter(abc.ABC): """Attempt to acquire the necessary tokens for the rate limiter. This method blocks until the required tokens are available if `blocking` - is set to True. + is set to `True`. - If `blocking` is set to False, the method will immediately return the result + If `blocking` is set to `False`, the method will immediately return the result of the attempt to acquire the tokens. Args: blocking: If `True`, the method will block until the tokens are available. If `False`, the method will return immediately with the result of - the attempt. Defaults to `True`. + the attempt. Returns: - `True` if the tokens were successfully acquired, `False` otherwise. + `True` if the tokens were successfully acquired, `False` otherwise. """ @@ -84,7 +84,7 @@ class InMemoryRateLimiter(BaseRateLimiter): not enough tokens in the bucket, the request is blocked until there are enough tokens. - These *tokens* have NOTHING to do with LLM tokens. They are just + These tokens have nothing to do with LLM tokens. They are just a way to keep track of how many requests can be made at a given time. Current limitations: @@ -109,7 +109,7 @@ class InMemoryRateLimiter(BaseRateLimiter): from langchain_anthropic import ChatAnthropic model = ChatAnthropic( - model_name="claude-3-opus-20240229", rate_limiter=rate_limiter + model_name="claude-sonnet-4-5-20250929", rate_limiter=rate_limiter ) for _ in range(5): @@ -132,7 +132,7 @@ class InMemoryRateLimiter(BaseRateLimiter): ) -> None: """A rate limiter based on a token bucket. - These *tokens* have NOTHING to do with LLM tokens. They are just + These tokens have nothing to do with LLM tokens. They are just a way to keep track of how many requests can be made at a given time. This rate limiter is designed to work in a threaded environment. @@ -145,11 +145,11 @@ class InMemoryRateLimiter(BaseRateLimiter): Args: requests_per_second: The number of tokens to add per second to the bucket. The tokens represent "credit" that can be used to make requests. - check_every_n_seconds: check whether the tokens are available + check_every_n_seconds: Check whether the tokens are available every this many seconds. Can be a float to represent fractions of a second. max_bucket_size: The maximum number of tokens that can be in the bucket. - Must be at least 1. Used to prevent bursts of requests. + Must be at least `1`. Used to prevent bursts of requests. """ # Number of requests that we can make per second. self.requests_per_second = requests_per_second @@ -199,18 +199,18 @@ class InMemoryRateLimiter(BaseRateLimiter): """Attempt to acquire a token from the rate limiter. This method blocks until the required tokens are available if `blocking` - is set to True. + is set to `True`. - If `blocking` is set to False, the method will immediately return the result + If `blocking` is set to `False`, the method will immediately return the result of the attempt to acquire the tokens. Args: blocking: If `True`, the method will block until the tokens are available. If `False`, the method will return immediately with the result of - the attempt. Defaults to `True`. + the attempt. Returns: - `True` if the tokens were successfully acquired, `False` otherwise. + `True` if the tokens were successfully acquired, `False` otherwise. """ if not blocking: return self._consume() @@ -223,18 +223,18 @@ class InMemoryRateLimiter(BaseRateLimiter): """Attempt to acquire a token from the rate limiter. Async version. This method blocks until the required tokens are available if `blocking` - is set to True. + is set to `True`. - If `blocking` is set to False, the method will immediately return the result + If `blocking` is set to `False`, the method will immediately return the result of the attempt to acquire the tokens. Args: blocking: If `True`, the method will block until the tokens are available. If `False`, the method will return immediately with the result of - the attempt. Defaults to `True`. + the attempt. Returns: - `True` if the tokens were successfully acquired, `False` otherwise. + `True` if the tokens were successfully acquired, `False` otherwise. """ if not blocking: return self._consume() diff --git a/libs/core/langchain_core/retrievers.py b/libs/core/langchain_core/retrievers.py index b92413b0546..bb50d2985e0 100644 --- a/libs/core/langchain_core/retrievers.py +++ b/libs/core/langchain_core/retrievers.py @@ -70,45 +70,45 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC): Example: A retriever that returns the first 5 documents from a list of documents - ```python - from langchain_core.documents import Document - from langchain_core.retrievers import BaseRetriever + ```python + from langchain_core.documents import Document + from langchain_core.retrievers import BaseRetriever - class SimpleRetriever(BaseRetriever): - docs: list[Document] - k: int = 5 + class SimpleRetriever(BaseRetriever): + docs: list[Document] + k: int = 5 - def _get_relevant_documents(self, query: str) -> list[Document]: - \"\"\"Return the first k documents from the list of documents\"\"\" - return self.docs[:self.k] + def _get_relevant_documents(self, query: str) -> list[Document]: + \"\"\"Return the first k documents from the list of documents\"\"\" + return self.docs[:self.k] - async def _aget_relevant_documents(self, query: str) -> list[Document]: - \"\"\"(Optional) async native implementation.\"\"\" - return self.docs[:self.k] - ``` + async def _aget_relevant_documents(self, query: str) -> list[Document]: + \"\"\"(Optional) async native implementation.\"\"\" + return self.docs[:self.k] + ``` Example: A simple retriever based on a scikit-learn vectorizer - ```python - from sklearn.metrics.pairwise import cosine_similarity + ```python + from sklearn.metrics.pairwise import cosine_similarity - class TFIDFRetriever(BaseRetriever, BaseModel): - vectorizer: Any - docs: list[Document] - tfidf_array: Any - k: int = 4 + class TFIDFRetriever(BaseRetriever, BaseModel): + vectorizer: Any + docs: list[Document] + tfidf_array: Any + k: int = 4 - class Config: - arbitrary_types_allowed = True + class Config: + arbitrary_types_allowed = True - def _get_relevant_documents(self, query: str) -> list[Document]: - # Ip -- (n_docs,x), Op -- (n_docs,n_Feats) - query_vec = self.vectorizer.transform([query]) - # Op -- (n_docs,1) -- Cosine Sim with each doc - results = cosine_similarity(self.tfidf_array, query_vec).reshape((-1,)) - return [self.docs[i] for i in results.argsort()[-self.k :][::-1]] - ``` + def _get_relevant_documents(self, query: str) -> list[Document]: + # Ip -- (n_docs,x), Op -- (n_docs,n_Feats) + query_vec = self.vectorizer.transform([query]) + # Op -- (n_docs,1) -- Cosine Sim with each doc + results = cosine_similarity(self.tfidf_array, query_vec).reshape((-1,)) + return [self.docs[i] for i in results.argsort()[-self.k :][::-1]] + ``` """ model_config = ConfigDict( diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index 91b6ef1c5f4..3b83d5452e2 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -871,7 +871,6 @@ class Runnable(ABC, Generic[Input, Output]): to do in parallel, and other keys. Please refer to the `RunnableConfig` for more details. return_exceptions: Whether to return exceptions instead of raising them. - Defaults to `False`. **kwargs: Additional keyword arguments to pass to the `Runnable`. Returns: @@ -938,7 +937,6 @@ class Runnable(ABC, Generic[Input, Output]): do in parallel, and other keys. Please refer to the `RunnableConfig` for more details. return_exceptions: Whether to return exceptions instead of raising them. - Defaults to `False`. **kwargs: Additional keyword arguments to pass to the `Runnable`. Yields: @@ -1005,7 +1003,6 @@ class Runnable(ABC, Generic[Input, Output]): do in parallel, and other keys. Please refer to the `RunnableConfig` for more details. return_exceptions: Whether to return exceptions instead of raising them. - Defaults to `False`. **kwargs: Additional keyword arguments to pass to the `Runnable`. Returns: @@ -1069,7 +1066,6 @@ class Runnable(ABC, Generic[Input, Output]): do in parallel, and other keys. Please refer to the `RunnableConfig` for more details. return_exceptions: Whether to return exceptions instead of raising them. - Defaults to `False`. **kwargs: Additional keyword arguments to pass to the `Runnable`. Yields: @@ -1357,7 +1353,8 @@ class Runnable(ABC, Generic[Input, Output]): ).with_config({"run_name": "my_template", "tags": ["my_template"]}) ``` - Example: + For instance: + ```python from langchain_core.runnables import RunnableLambda @@ -1451,7 +1448,7 @@ class Runnable(ABC, Generic[Input, Output]): An async stream of `StreamEvent`. Raises: - NotImplementedError: If the version is not `'v1'` or `'v2'`. + `NotImplementedError`: If the version is not `'v1'` or `'v2'`. """ # noqa: E501 if version == "v2": @@ -1837,11 +1834,10 @@ class Runnable(ABC, Generic[Input, Output]): Args: retry_if_exception_type: A tuple of exception types to retry on. - Defaults to (Exception,). wait_exponential_jitter: Whether to add jitter to the wait - time between retries. Defaults to `True`. + time between retries. stop_after_attempt: The maximum number of attempts to make before - giving up. Defaults to 3. + giving up. exponential_jitter_params: Parameters for `tenacity.wait_exponential_jitter`. Namely: `initial`, `max`, `exp_base`, and `jitter` (all float values). @@ -1929,7 +1925,6 @@ class Runnable(ABC, Generic[Input, Output]): fallbacks: A sequence of runnables to try if the original `Runnable` fails. exceptions_to_handle: A tuple of exception types to handle. - Defaults to `(Exception,)`. exception_key: If string is specified then handled exceptions will be passed to fallbacks as part of the input under the specified key. If `None`, exceptions will not be passed to fallbacks. @@ -2633,9 +2628,7 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]): which: The `ConfigurableField` instance that will be used to select the alternative. default_key: The default key to use if no alternative is selected. - Defaults to `'default'`. prefix_keys: Whether to prefix the keys with the `ConfigurableField` id. - Defaults to `False`. **kwargs: A dictionary of keys to `Runnable` instances or callables that return `Runnable` instances. @@ -2896,7 +2889,7 @@ class RunnableSequence(RunnableSerializable[Input, Output]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -3627,7 +3620,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -5156,7 +5149,7 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -5479,7 +5472,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[ @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -5761,7 +5754,7 @@ class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-re `bind`: Bind kwargs to pass to the underlying `Runnable` when running it. ```python - # Create a Runnable binding that invokes the ChatModel with the + # Create a Runnable binding that invokes the chat model with the # additional kwarg `stop=['-']` when running it. from langchain_community.chat_models import ChatOpenAI diff --git a/libs/core/langchain_core/runnables/branch.py b/libs/core/langchain_core/runnables/branch.py index 542790286c9..e773d9165ff 100644 --- a/libs/core/langchain_core/runnables/branch.py +++ b/libs/core/langchain_core/runnables/branch.py @@ -146,7 +146,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` diff --git a/libs/core/langchain_core/runnables/config.py b/libs/core/langchain_core/runnables/config.py index c90915837ec..a77718414c1 100644 --- a/libs/core/langchain_core/runnables/config.py +++ b/libs/core/langchain_core/runnables/config.py @@ -536,7 +536,7 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor): fn: The function to map. *iterables: The iterables to map over. timeout: The timeout for the map. - chunksize: The chunksize for the map. Defaults to 1. + chunksize: The chunksize for the map. Returns: The iterator for the mapped function. diff --git a/libs/core/langchain_core/runnables/configurable.py b/libs/core/langchain_core/runnables/configurable.py index 120fdcc3f1f..0e877684b65 100644 --- a/libs/core/langchain_core/runnables/configurable.py +++ b/libs/core/langchain_core/runnables/configurable.py @@ -72,7 +72,7 @@ class DynamicRunnable(RunnableSerializable[Input, Output]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -540,7 +540,7 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]): """The alternatives to choose from.""" default_key: str = "default" - """The enum value to use for the default option. Defaults to `'default'`.""" + """The enum value to use for the default option.""" prefix_keys: bool """Whether to prefix configurable fields of each alternative with a namespace diff --git a/libs/core/langchain_core/runnables/fallbacks.py b/libs/core/langchain_core/runnables/fallbacks.py index a3794d8ebcb..32f05846950 100644 --- a/libs/core/langchain_core/runnables/fallbacks.py +++ b/libs/core/langchain_core/runnables/fallbacks.py @@ -143,7 +143,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` diff --git a/libs/core/langchain_core/runnables/graph.py b/libs/core/langchain_core/runnables/graph.py index 6e3b334120d..bf1d071bf20 100644 --- a/libs/core/langchain_core/runnables/graph.py +++ b/libs/core/langchain_core/runnables/graph.py @@ -71,7 +71,7 @@ class Edge(NamedTuple): data: Stringifiable | None = None """Optional data associated with the edge. """ conditional: bool = False - """Whether the edge is conditional. Defaults to `False`.""" + """Whether the edge is conditional.""" def copy(self, *, source: str | None = None, target: str | None = None) -> Edge: """Return a copy of the edge with optional new source and target nodes. @@ -157,9 +157,9 @@ class NodeStyles: """Schema for Hexadecimal color codes for different node types. 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". + default: The default color code. + first: The color code for the first node. + last: The color code for the last node. """ default: str = "fill:#f2f0ff,line-height:1.2" @@ -201,9 +201,9 @@ def node_data_json( """Convert the data of a node to a JSON-serializable format. Args: - node: The node to convert. - with_schemas: Whether to include the schema of the data if - it is a Pydantic model. Defaults to `False`. + node: The `Node` to convert. + with_schemas: Whether to include the schema of the data if it is a Pydantic + model. Returns: A dictionary with the type of the data and the data itself. @@ -267,7 +267,7 @@ class Graph: Args: with_schemas: Whether to include the schemas of the nodes if they are - Pydantic models. Defaults to `False`. + Pydantic models. Returns: A dictionary with the nodes and edges of the graph. @@ -362,7 +362,7 @@ class Graph: source: The source node of the edge. target: The target node of the edge. data: Optional data associated with the edge. - conditional: Whether the edge is conditional. Defaults to `False`. + conditional: Whether the edge is conditional. Returns: The edge that was added to the graph. @@ -391,7 +391,7 @@ class Graph: Args: graph: The graph to add. - prefix: The prefix to add to the node ids. Defaults to "". + prefix: The prefix to add to the node ids. Returns: A tuple of the first and last nodes of the subgraph. @@ -458,7 +458,7 @@ class Graph: def first_node(self) -> Node | None: """Find the single node that is not a target of any edge. - If there is no such node, or there are multiple, return None. + If there is no such node, or there are multiple, return `None`. When drawing the graph, this node would be the origin. Returns: @@ -470,7 +470,7 @@ class Graph: def last_node(self) -> Node | None: """Find the single node that is not a source of any edge. - If there is no such node, or there are multiple, return None. + If there is no such node, or there are multiple, return `None`. When drawing the graph, this node would be the destination. Returns: @@ -585,11 +585,10 @@ class Graph: """Draw the graph as a Mermaid syntax string. Args: - with_styles: Whether to include styles in the syntax. Defaults to `True`. - curve_style: The style of the edges. Defaults to CurveStyle.LINEAR. - node_colors: The colors of the nodes. Defaults to NodeStyles(). + with_styles: Whether to include styles in the syntax. + curve_style: The style of the edges. + node_colors: The colors of the nodes. wrap_label_n_words: The number of words to wrap the node labels at. - Defaults to 9. frontmatter_config: Mermaid frontmatter config. Can be used to customize theme and styles. Will be converted to YAML and added to the beginning of the mermaid graph. @@ -647,20 +646,16 @@ class Graph: """Draw the graph as a PNG image using Mermaid. Args: - curve_style: The style of the edges. Defaults to CurveStyle.LINEAR. - node_colors: The colors of the nodes. Defaults to NodeStyles(). + curve_style: The style of the edges. + node_colors: The colors of the nodes. wrap_label_n_words: The number of words to wrap the node labels at. - Defaults to 9. output_file_path: The path to save the image to. If `None`, the image is not saved. draw_method: The method to use to draw the graph. - Defaults to MermaidDrawMethod.API. - background_color: The color of the background. Defaults to "white". - padding: The padding around the graph. Defaults to 10. - max_retries: The maximum number of retries (MermaidDrawMethod.API). - Defaults to 1. - retry_delay: The delay between retries (MermaidDrawMethod.API). - Defaults to 1.0. + background_color: The color of the background. + padding: The padding around the graph. + max_retries: The maximum number of retries (`MermaidDrawMethod.API`). + retry_delay: The delay between retries (`MermaidDrawMethod.API`). frontmatter_config: Mermaid frontmatter config. Can be used to customize theme and styles. Will be converted to YAML and added to the beginning of the mermaid graph. @@ -712,7 +707,7 @@ def _first_node(graph: Graph, exclude: Sequence[str] = ()) -> Node | None: """Find the single node that is not a target of any edge. Exclude nodes/sources with ids in the exclude list. - If there is no such node, or there are multiple, return None. + If there is no such node, or there are multiple, return `None`. When drawing the graph, this node would be the origin. """ targets = {edge.target for edge in graph.edges if edge.source not in exclude} @@ -728,7 +723,7 @@ def _last_node(graph: Graph, exclude: Sequence[str] = ()) -> Node | None: """Find the single node that is not a source of any edge. Exclude nodes/targets with ids in the exclude list. - If there is no such node, or there are multiple, return None. + If there is no such node, or there are multiple, return `None`. When drawing the graph, this node would be the destination. """ sources = {edge.source for edge in graph.edges if edge.target not in exclude} diff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.py index bc6caed60db..1709c155266 100644 --- a/libs/core/langchain_core/runnables/graph_mermaid.py +++ b/libs/core/langchain_core/runnables/graph_mermaid.py @@ -60,10 +60,10 @@ def draw_mermaid( edges: List of edges, object with a source, target and data. first_node: Id of the first node. last_node: Id of the last node. - with_styles: Whether to include styles in the graph. Defaults to `True`. - curve_style: Curve style for the edges. Defaults to CurveStyle.LINEAR. - node_styles: Node colors for different types. Defaults to NodeStyles(). - wrap_label_n_words: Words to wrap the edge labels. Defaults to 9. + with_styles: Whether to include styles in the graph. + curve_style: Curve style for the edges. + node_styles: Node colors for different types. + wrap_label_n_words: Words to wrap the edge labels. frontmatter_config: Mermaid frontmatter config. Can be used to customize theme and styles. Will be converted to YAML and added to the beginning of the mermaid graph. @@ -287,11 +287,11 @@ def draw_mermaid_png( Args: mermaid_syntax: Mermaid graph syntax. output_file_path: Path to save the PNG image. - draw_method: Method to draw the graph. Defaults to MermaidDrawMethod.API. - background_color: Background color of the image. Defaults to "white". - padding: Padding around the image. Defaults to 10. - max_retries: Maximum number of retries (MermaidDrawMethod.API). Defaults to 1. - retry_delay: Delay between retries (MermaidDrawMethod.API). Defaults to 1.0. + draw_method: Method to draw the graph. + background_color: Background color of the image. + padding: Padding around the image. + max_retries: Maximum number of retries (MermaidDrawMethod.API). + retry_delay: Delay between retries (MermaidDrawMethod.API). base_url: Base URL for the Mermaid.ink API. Returns: diff --git a/libs/core/langchain_core/runnables/graph_png.py b/libs/core/langchain_core/runnables/graph_png.py index 3e599fc38ce..75cc2a796aa 100644 --- a/libs/core/langchain_core/runnables/graph_png.py +++ b/libs/core/langchain_core/runnables/graph_png.py @@ -105,7 +105,7 @@ class PngDrawer: source: The source node. target: The target node. label: The label for the edge. - conditional: Whether the edge is conditional. Defaults to `False`. + conditional: Whether the edge is conditional. """ viz.add_edge( source, diff --git a/libs/core/langchain_core/runnables/history.py b/libs/core/langchain_core/runnables/history.py index ce77aac9450..bfe1ab34de4 100644 --- a/libs/core/langchain_core/runnables/history.py +++ b/libs/core/langchain_core/runnables/history.py @@ -296,9 +296,9 @@ class RunnableWithMessageHistory(RunnableBindingBase): # type: ignore[no-redef] ``` input_messages_key: Must be specified if the base runnable accepts a dict - as input. Default is None. + as input. output_messages_key: Must be specified if the base runnable returns a dict - as output. Default is None. + as output. 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 diff --git a/libs/core/langchain_core/runnables/passthrough.py b/libs/core/langchain_core/runnables/passthrough.py index 76a0aa2126a..329e3a17484 100644 --- a/libs/core/langchain_core/runnables/passthrough.py +++ b/libs/core/langchain_core/runnables/passthrough.py @@ -185,7 +185,7 @@ class RunnablePassthrough(RunnableSerializable[Other, Other]): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -409,7 +409,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` @@ -714,7 +714,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` diff --git a/libs/core/langchain_core/runnables/router.py b/libs/core/langchain_core/runnables/router.py index 89b500d5e09..e32c21519b4 100644 --- a/libs/core/langchain_core/runnables/router.py +++ b/libs/core/langchain_core/runnables/router.py @@ -96,7 +96,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]): @classmethod @override def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object. + """Get the namespace of the LangChain object. Returns: `["langchain", "schema", "runnable"]` diff --git a/libs/core/langchain_core/runnables/utils.py b/libs/core/langchain_core/runnables/utils.py index 58ca3a75128..ba3dfbcd3d3 100644 --- a/libs/core/langchain_core/runnables/utils.py +++ b/libs/core/langchain_core/runnables/utils.py @@ -136,7 +136,7 @@ def coro_with_context( Args: coro: The coroutine to await. context: The context to use. - create_task: Whether to create a task. Defaults to `False`. + create_task: Whether to create a task. Returns: The coroutine with the context. @@ -558,7 +558,7 @@ class ConfigurableField(NamedTuple): annotation: Any | None = None """The annotation of the field. """ is_shared: bool = False - """Whether the field is shared. Defaults to `False`.""" + """Whether the field is shared.""" @override def __hash__(self) -> int: @@ -579,7 +579,7 @@ class ConfigurableFieldSingleOption(NamedTuple): description: str | None = None """The description of the field. """ is_shared: bool = False - """Whether the field is shared. Defaults to `False`.""" + """Whether the field is shared.""" @override def __hash__(self) -> int: @@ -600,7 +600,7 @@ class ConfigurableFieldMultiOption(NamedTuple): description: str | None = None """The description of the field. """ is_shared: bool = False - """Whether the field is shared. Defaults to `False`.""" + """Whether the field is shared.""" @override def __hash__(self) -> int: @@ -626,7 +626,7 @@ class ConfigurableFieldSpec(NamedTuple): default: Any = None """The default value for the field. """ is_shared: bool = False - """Whether the field is shared. Defaults to `False`.""" + """Whether the field is shared.""" dependencies: list[str] | None = None """The dependencies of the field. """ diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py index 5c491b19bab..87f06e6f2ec 100644 --- a/libs/core/langchain_core/tools/base.py +++ b/libs/core/langchain_core/tools/base.py @@ -248,7 +248,7 @@ def _function_annotations_are_pydantic_v1( True if all Pydantic annotations are from V1, `False` otherwise. Raises: - NotImplementedError: If the function contains mixed V1 and V2 annotations. + `NotImplementedError`: If the function contains mixed V1 and V2 annotations. """ any_v1_annotations = any( _is_pydantic_annotation(parameter.annotation, pydantic_version="v1") @@ -293,10 +293,9 @@ def create_schema_from_function( filter_args: Optional list of arguments to exclude from the schema. Defaults to `FILTERED_ARGS`. parse_docstring: Whether to parse the function's docstring for descriptions - for each argument. Defaults to `False`. + for each argument. error_on_invalid_docstring: if `parse_docstring` is provided, configure whether to raise `ValueError` on invalid Google Style docstrings. - Defaults to `False`. include_injected: Whether to include injected arguments in the schema. Defaults to `True`, since we want to include them in the schema when *validating* tool inputs. @@ -481,11 +480,11 @@ class ChildTool(BaseTool): """Handle the content of the ValidationError thrown.""" response_format: Literal["content", "content_and_artifact"] = "content" - """The tool response format. Defaults to 'content'. + """The tool response format. - If "content" then the output of the tool is interpreted as the contents of a - ToolMessage. If "content_and_artifact" then the output is expected to be a - two-tuple corresponding to the (content, artifact) of a ToolMessage. + If `"content"` then the output of the tool is interpreted as the contents of a + ToolMessage. If `"content_and_artifact"` then the output is expected to be a + two-tuple corresponding to the (content, artifact) of a `ToolMessage`. """ def __init__(self, **kwargs: Any) -> None: @@ -768,8 +767,8 @@ class ChildTool(BaseTool): Args: tool_input: The input to the tool. verbose: Whether to log the tool's progress. - start_color: The color to use when starting the tool. Defaults to 'green'. - color: The color to use when ending the tool. Defaults to 'green'. + start_color: The color to use when starting the tool. + color: The color to use when ending the tool. callbacks: Callbacks to be called during tool execution. tags: Optional list of tags associated with the tool. metadata: Optional metadata associated with the tool. @@ -880,8 +879,8 @@ class ChildTool(BaseTool): Args: tool_input: The input to the tool. verbose: Whether to log the tool's progress. - start_color: The color to use when starting the tool. Defaults to 'green'. - color: The color to use when ending the tool. Defaults to 'green'. + start_color: The color to use when starting the tool. + color: The color to use when ending the tool. callbacks: Callbacks to be called during tool execution. tags: Optional list of tags associated with the tool. metadata: Optional metadata associated with the tool. diff --git a/libs/core/langchain_core/tools/convert.py b/libs/core/langchain_core/tools/convert.py index d3c427e7c6d..97b3068d9ee 100644 --- a/libs/core/langchain_core/tools/convert.py +++ b/libs/core/langchain_core/tools/convert.py @@ -81,7 +81,7 @@ def tool( parse_docstring: bool = False, error_on_invalid_docstring: bool = True, ) -> BaseTool | Callable[[Callable | Runnable], BaseTool]: - """Make tools out of functions, can be used with or without arguments. + """Make tools out of Python functions, can be used with or without arguments. Args: name_or_callable: Optional name of the tool or the callable to be @@ -93,30 +93,26 @@ def tool( - `description` argument (used even if docstring and/or `args_schema` are provided) - - tool function docstring + - 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`. - args_schema: optional argument schema for user to specify. + than continuing the agent loop. + args_schema: Optional argument schema for user to specify. infer_schema: Whether to infer the schema of the arguments from the function's signature. This also makes the resultant tool accept a dictionary input to its `run()` function. - Defaults to `True`. - response_format: The tool response format. If "content" then the output of - the tool is interpreted as the contents of a ToolMessage. If - "content_and_artifact" then the output is expected to be a two-tuple - corresponding to the (content, artifact) of a ToolMessage. - Defaults to "content". + response_format: The tool response format. If `"content"` then the output of + the tool is interpreted as the contents of a `ToolMessage`. If + `"content_and_artifact"` then the output is expected to be a two-tuple + corresponding to the `(content, artifact)` of a `ToolMessage`. parse_docstring: if `infer_schema` and `parse_docstring`, will attempt to parse parameter descriptions from Google Style function docstrings. - Defaults to `False`. error_on_invalid_docstring: if `parse_docstring` is provided, configure - whether to raise ValueError on invalid Google Style docstrings. - Defaults to `True`. + whether to raise `ValueError` on invalid Google Style docstrings. Raises: ValueError: If too many positional arguments are provided. @@ -124,8 +120,8 @@ def tool( ValueError: If the first argument is not a string or callable with a `__name__` attribute. ValueError: If the function does not have a docstring and description - is not provided and `infer_schema` is False. - ValueError: If `parse_docstring` is True and the function has an invalid + is not provided and `infer_schema` is `False`. + ValueError: If `parse_docstring` is `True` and the function has an invalid Google-style docstring and `error_on_invalid_docstring` is True. ValueError: If a Runnable is provided that does not have an object schema. @@ -133,7 +129,7 @@ def tool( The tool. Requires: - - Function must be of type (str) -> str + - Function must be of type `(str) -> str` - Function must have a docstring Examples: @@ -197,7 +193,7 @@ def tool( Note that parsing by default will raise `ValueError` if the docstring is considered invalid. A docstring is considered invalid if it contains arguments not in the function signature, or is unable to be parsed into - a summary and "Args:" blocks. Examples below: + a summary and `"Args:"` blocks. Examples below: ```python # No args section diff --git a/libs/core/langchain_core/tools/retriever.py b/libs/core/langchain_core/tools/retriever.py index 7ae35abc7f8..298ae6dd93d 100644 --- a/libs/core/langchain_core/tools/retriever.py +++ b/libs/core/langchain_core/tools/retriever.py @@ -82,12 +82,12 @@ def create_retriever_tool( description: The description for the tool. This will be passed to the language model, so should be descriptive. document_prompt: The prompt to use for the document. - document_separator: The separator to use between documents. Defaults to "\n\n". - response_format: The tool response format. If "content" then the output of - the tool is interpreted as the contents of a ToolMessage. If - "content_and_artifact" then the output is expected to be a two-tuple - corresponding to the (content, artifact) of a ToolMessage (artifact - being a list of documents in this case). Defaults to "content". + document_separator: The separator to use between documents. + response_format: The tool response format. If `"content"` then the output of + the tool is interpreted as the contents of a `ToolMessage`. If + `"content_and_artifact"` then the output is expected to be a two-tuple + corresponding to the `(content, artifact)` of a `ToolMessage` (artifact + being a list of documents in this case). Returns: Tool class to pass to an agent. diff --git a/libs/core/langchain_core/tools/simple.py b/libs/core/langchain_core/tools/simple.py index 44d922ed53e..68c70e61258 100644 --- a/libs/core/langchain_core/tools/simple.py +++ b/libs/core/langchain_core/tools/simple.py @@ -176,7 +176,7 @@ class Tool(BaseTool): func: The function to create the tool from. name: The name of the tool. description: The description of the tool. - return_direct: Whether to return the output directly. Defaults to `False`. + return_direct: Whether to return the output directly. args_schema: The schema of the tool's input arguments. coroutine: The asynchronous version of the function. **kwargs: Additional arguments to pass to the tool. diff --git a/libs/core/langchain_core/tools/structured.py b/libs/core/langchain_core/tools/structured.py index 0dc223b63f3..632c450c3b3 100644 --- a/libs/core/langchain_core/tools/structured.py +++ b/libs/core/langchain_core/tools/structured.py @@ -149,21 +149,16 @@ class StructuredTool(BaseTool): description: The description of the tool. Defaults to the function docstring. return_direct: Whether to return the result directly or as a callback. - Defaults to `False`. args_schema: The schema of the tool's input arguments. infer_schema: Whether to infer the schema from the function's signature. - Defaults to `True`. - response_format: The tool response format. If "content" then the output of - the tool is interpreted as the contents of a ToolMessage. If - "content_and_artifact" then the output is expected to be a two-tuple - corresponding to the (content, artifact) of a ToolMessage. - Defaults to "content". + response_format: The tool response format. If `"content"` then the output of + the tool is interpreted as the contents of a `ToolMessage`. If + `"content_and_artifact"` then the output is expected to be a two-tuple + corresponding to the `(content, artifact)` of a `ToolMessage`. parse_docstring: if `infer_schema` and `parse_docstring`, will attempt to parse parameter descriptions from Google Style function docstrings. - Defaults to `False`. error_on_invalid_docstring: if `parse_docstring` is provided, configure - whether to raise ValueError on invalid Google Style docstrings. - Defaults to `False`. + whether to raise `ValueError` on invalid Google Style docstrings. **kwargs: Additional arguments to pass to the tool Returns: diff --git a/libs/core/langchain_core/tracers/memory_stream.py b/libs/core/langchain_core/tracers/memory_stream.py index a59e47821d4..0a9facf17af 100644 --- a/libs/core/langchain_core/tracers/memory_stream.py +++ b/libs/core/langchain_core/tracers/memory_stream.py @@ -5,7 +5,7 @@ channel. The writer and reader can be in the same event loop or in different eve loops. When they're in different event loops, they will also be in different threads. -This is useful in situations when there's a mix of synchronous and asynchronous +Useful in situations when there's a mix of synchronous and asynchronous used in the code. """ diff --git a/libs/core/langchain_core/tracers/root_listeners.py b/libs/core/langchain_core/tracers/root_listeners.py index 043805c16d4..923cd1c16f6 100644 --- a/libs/core/langchain_core/tracers/root_listeners.py +++ b/libs/core/langchain_core/tracers/root_listeners.py @@ -24,7 +24,7 @@ class RootListenersTracer(BaseTracer): """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.""" + """Whether to log a warning if the parent is missing.""" def __init__( self, @@ -79,7 +79,7 @@ class AsyncRootListenersTracer(AsyncBaseTracer): """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.""" + """Whether to log a warning if the parent is missing.""" def __init__( self, diff --git a/libs/core/langchain_core/tracers/stdout.py b/libs/core/langchain_core/tracers/stdout.py index 72e6cee19a6..119b9127fd6 100644 --- a/libs/core/langchain_core/tracers/stdout.py +++ b/libs/core/langchain_core/tracers/stdout.py @@ -49,8 +49,7 @@ class FunctionCallbackHandler(BaseTracer): """Tracer that calls a function with a single str parameter.""" name: str = "function_callback_handler" - """The name of the tracer. This is used to identify the tracer in the logs. - Default is "function_callback_handler".""" + """The name of the tracer. This is used to identify the tracer in the logs.""" def __init__(self, function: Callable[[str], None], **kwargs: Any) -> None: """Create a FunctionCallbackHandler. diff --git a/libs/core/langchain_core/utils/__init__.py b/libs/core/langchain_core/utils/__init__.py index e16f3c11583..04216583702 100644 --- a/libs/core/langchain_core/utils/__init__.py +++ b/libs/core/langchain_core/utils/__init__.py @@ -1,4 +1,4 @@ -"""**Utility functions** for LangChain. +"""Utility functions for LangChain. These functions do not depend on any other LangChain module. """ diff --git a/libs/core/langchain_core/utils/aiter.py b/libs/core/langchain_core/utils/aiter.py index 44319e1a654..b196b43aba8 100644 --- a/libs/core/langchain_core/utils/aiter.py +++ b/libs/core/langchain_core/utils/aiter.py @@ -201,7 +201,7 @@ class Tee(Generic[T]): Args: iterable: The iterable to split. - n: The number of iterators to create. Defaults to 2. + n: The number of iterators to create. lock: The lock to synchronise access to the shared buffers. """ diff --git a/libs/core/langchain_core/utils/function_calling.py b/libs/core/langchain_core/utils/function_calling.py index f2262c36895..8f5bee4f5ee 100644 --- a/libs/core/langchain_core/utils/function_calling.py +++ b/libs/core/langchain_core/utils/function_calling.py @@ -114,7 +114,7 @@ def _convert_json_schema_to_openai_function( used. description: The description of the function. If not provided, the description of the schema will be used. - rm_titles: Whether to remove titles from the schema. Defaults to `True`. + rm_titles: Whether to remove titles from the schema. Returns: The function description. @@ -148,7 +148,7 @@ def _convert_pydantic_to_openai_function( used. description: The description of the function. If not provided, the description of the schema will be used. - rm_titles: Whether to remove titles from the schema. Defaults to `True`. + rm_titles: Whether to remove titles from the schema. Raises: TypeError: If the model is not a Pydantic model. @@ -334,11 +334,11 @@ def convert_to_openai_function( Args: function: - A dictionary, Pydantic BaseModel class, TypedDict class, a LangChain - Tool object, or a Python function. If a dictionary is passed in, it is + A dictionary, Pydantic `BaseModel` class, `TypedDict` class, a LangChain + `Tool` object, or a Python function. If a dictionary is passed in, it is assumed to already be a valid OpenAI function, a JSON schema with - top-level 'title' key specified, an Anthropic format - tool, or an Amazon Bedrock Converse format tool. + top-level `title` key specified, an Anthropic format tool, or an Amazon + Bedrock Converse format tool. strict: If `True`, model output is guaranteed to exactly match the JSON Schema provided in the function definition. If `None`, `strict` argument will not @@ -351,17 +351,8 @@ def convert_to_openai_function( Raises: ValueError: If function is not in a supported format. - !!! warning "Behavior changed in 0.2.29" - `strict` arg added. - - !!! warning "Behavior changed in 0.3.13" - Support for Anthropic format tools added. - - !!! warning "Behavior changed in 0.3.14" - Support for Amazon Bedrock Converse format tools added. - !!! warning "Behavior changed in 0.3.16" - 'description' and 'parameters' keys are now optional. Only 'name' is + `description` and `parameters` keys are now optional. Only `name` is required and guaranteed to be part of the output. """ # an Anthropic format tool @@ -459,16 +450,14 @@ def convert_to_openai_tool( ) -> dict[str, Any]: """Convert a tool-like object to an OpenAI tool schema. - OpenAI tool schema reference: - https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools + [OpenAI tool schema reference](https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools) Args: tool: - Either a dictionary, a pydantic.BaseModel class, Python function, or - BaseTool. If a dictionary is passed in, it is - assumed to already be a valid OpenAI function, a JSON schema with - top-level 'title' key specified, an Anthropic format - tool, or an Amazon Bedrock Converse format tool. + Either a dictionary, a `pydantic.BaseModel` class, Python function, or + `BaseTool`. If a dictionary is passed in, it is assumed to already be a + valid OpenAI function, a JSON schema with top-level `title` key specified, + an Anthropic format tool, or an Amazon Bedrock Converse format tool. strict: If `True`, model output is guaranteed to exactly match the JSON Schema provided in the function definition. If `None`, `strict` argument will not @@ -478,26 +467,14 @@ def convert_to_openai_tool( A dict version of the passed in tool which is compatible with the OpenAI tool-calling API. - !!! warning "Behavior changed in 0.2.29" - `strict` arg added. - - !!! warning "Behavior changed in 0.3.13" - Support for Anthropic format tools added. - - !!! warning "Behavior changed in 0.3.14" - Support for Amazon Bedrock Converse format tools added. - !!! warning "Behavior changed in 0.3.16" - 'description' and 'parameters' keys are now optional. Only 'name' is + `description` and `parameters` keys are now optional. Only `name` is required and guaranteed to be part of the output. !!! warning "Behavior changed in 0.3.44" Return OpenAI Responses API-style tools unchanged. This includes - any dict with "type" in "file_search", "function", "computer_use_preview", - "web_search_preview". - - !!! warning "Behavior changed in 0.3.61" - Added support for OpenAI's built-in code interpreter and remote MCP tools. + any dict with `"type"` in `"file_search"`, `"function"`, + `"computer_use_preview"`, `"web_search_preview"`. !!! warning "Behavior changed in 0.3.63" Added support for OpenAI's image generation built-in tool. diff --git a/libs/core/langchain_core/utils/input.py b/libs/core/langchain_core/utils/input.py index e71c2618243..80c9c633033 100644 --- a/libs/core/langchain_core/utils/input.py +++ b/libs/core/langchain_core/utils/input.py @@ -66,7 +66,7 @@ def print_text( Args: text: The text to print. color: The color to use. - end: The end character to use. Defaults to "". + end: The end character to use. file: The file to write to. """ text_to_print = get_colored_text(text, color) if color else text diff --git a/libs/core/langchain_core/utils/iter.py b/libs/core/langchain_core/utils/iter.py index 3dbe28d204b..a4f9b0e1ade 100644 --- a/libs/core/langchain_core/utils/iter.py +++ b/libs/core/langchain_core/utils/iter.py @@ -137,7 +137,7 @@ class Tee(Generic[T]): Args: iterable: The iterable to split. - n: The number of iterators to create. Defaults to 2. + n: The number of iterators to create. lock: The lock to synchronise access to the shared buffers. """ diff --git a/libs/core/langchain_core/utils/json.py b/libs/core/langchain_core/utils/json.py index b1e3879a476..9c00b11f94f 100644 --- a/libs/core/langchain_core/utils/json.py +++ b/libs/core/langchain_core/utils/json.py @@ -51,7 +51,7 @@ def parse_partial_json(s: str, *, strict: bool = False) -> Any: Args: s: The JSON string to parse. - strict: Whether to use strict parsing. Defaults to `False`. + strict: Whether to use strict parsing. Returns: The parsed JSON object as a Python dictionary. diff --git a/libs/core/langchain_core/utils/strings.py b/libs/core/langchain_core/utils/strings.py index 86f7499dcc6..d6a3787b131 100644 --- a/libs/core/langchain_core/utils/strings.py +++ b/libs/core/langchain_core/utils/strings.py @@ -57,7 +57,7 @@ def sanitize_for_postgres(text: str, replacement: str = "") -> str: Args: text: The text to sanitize. - replacement: String to replace NUL bytes with. Defaults to empty string. + replacement: String to replace NUL bytes with. Returns: The sanitized text with NUL bytes removed or replaced. diff --git a/libs/core/langchain_core/vectorstores/base.py b/libs/core/langchain_core/vectorstores/base.py index fa15a3018aa..0ce79dfa813 100644 --- a/libs/core/langchain_core/vectorstores/base.py +++ b/libs/core/langchain_core/vectorstores/base.py @@ -109,7 +109,7 @@ class VectorStore(ABC): """Delete by vector ID or other criteria. Args: - ids: List of ids to delete. If `None`, delete all. Default is None. + ids: List of ids to delete. If `None`, delete all. **kwargs: Other keyword arguments that subclasses might use. Returns: @@ -176,7 +176,7 @@ class VectorStore(ABC): """Async delete by vector ID or other criteria. Args: - ids: List of ids to delete. If `None`, delete all. Default is None. + ids: List of ids to delete. If `None`, delete all. **kwargs: Other keyword arguments that subclasses might use. Returns: @@ -197,7 +197,6 @@ class VectorStore(ABC): Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. - Default is None. ids: Optional list **kwargs: vectorstore specific parameters. @@ -365,7 +364,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: Arguments to pass to the search method. Returns: @@ -462,7 +461,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs @@ -489,7 +488,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs @@ -513,7 +512,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs. @@ -562,7 +561,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs @@ -606,7 +605,7 @@ class VectorStore(ABC): Args: query: Input text. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: Arguments to pass to the search method. Returns: @@ -624,7 +623,7 @@ class VectorStore(ABC): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: Arguments to pass to the search method. Returns: @@ -639,7 +638,7 @@ class VectorStore(ABC): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: Arguments to pass to the search method. Returns: @@ -667,13 +666,11 @@ class VectorStore(ABC): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. **kwargs: Arguments to pass to the search method. Returns: @@ -696,13 +693,11 @@ class VectorStore(ABC): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. **kwargs: Arguments to pass to the search method. Returns: @@ -736,13 +731,11 @@ class VectorStore(ABC): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. **kwargs: Arguments to pass to the search method. Returns: @@ -765,13 +758,11 @@ class VectorStore(ABC): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Default is 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. **kwargs: Arguments to pass to the search method. Returns: @@ -864,7 +855,6 @@ class VectorStore(ABC): texts: Texts to add to the vectorstore. embedding: Embedding function to use. metadatas: Optional list of metadatas associated with the texts. - Default is None. ids: Optional list of IDs associated with the texts. **kwargs: Additional keyword arguments. @@ -888,7 +878,6 @@ class VectorStore(ABC): texts: Texts to add to the vectorstore. embedding: Embedding function to use. metadatas: Optional list of metadatas associated with the texts. - Default is None. ids: Optional list of IDs associated with the texts. **kwargs: Additional keyword arguments. diff --git a/libs/core/langchain_core/vectorstores/utils.py b/libs/core/langchain_core/vectorstores/utils.py index ca46e638223..c51c3302e68 100644 --- a/libs/core/langchain_core/vectorstores/utils.py +++ b/libs/core/langchain_core/vectorstores/utils.py @@ -112,8 +112,8 @@ def maximal_marginal_relevance( Args: query_embedding: The query embedding. embedding_list: A list of embeddings. - lambda_mult: The lambda parameter for MMR. Default is 0.5. - k: The number of embeddings to return. Default is 4. + lambda_mult: The lambda parameter for MMR. + k: The number of embeddings to return. Returns: A list of indices of the embeddings to return. diff --git a/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py b/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py index b246da593fa..667b2fba29b 100644 --- a/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py +++ b/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py @@ -26,11 +26,11 @@ class InMemoryCache(BaseCache): self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Look up based on prompt and llm_string.""" + """Look up based on `prompt` and `llm_string`.""" return self._cache.get((prompt, llm_string), None) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: - """Update cache based on prompt and llm_string.""" + """Update cache based on `prompt` and `llm_string`.""" self._cache[prompt, llm_string] = return_val @override diff --git a/libs/core/tests/unit_tests/language_models/llms/test_cache.py b/libs/core/tests/unit_tests/language_models/llms/test_cache.py index a0bd8a34b33..720c247a9c9 100644 --- a/libs/core/tests/unit_tests/language_models/llms/test_cache.py +++ b/libs/core/tests/unit_tests/language_models/llms/test_cache.py @@ -15,11 +15,11 @@ class InMemoryCache(BaseCache): self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Look up based on prompt and llm_string.""" + """Look up based on `prompt` and `llm_string`.""" return self._cache.get((prompt, llm_string), None) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: - """Update cache based on prompt and llm_string.""" + """Update cache based on `prompt` and `llm_string`.""" self._cache[prompt, llm_string] = return_val @override @@ -68,12 +68,12 @@ class InMemoryCacheBad(BaseCache): self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None: - """Look up based on prompt and llm_string.""" + """Look up based on `prompt` and `llm_string`.""" msg = "This code should not be triggered" raise NotImplementedError(msg) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: - """Update cache based on prompt and llm_string.""" + """Update cache based on `prompt` and `llm_string`.""" msg = "This code should not be triggered" raise NotImplementedError(msg) diff --git a/libs/core/tests/unit_tests/output_parsers/test_base_parsers.py b/libs/core/tests/unit_tests/output_parsers/test_base_parsers.py index fa5e9c9c9c0..2013790aa05 100644 --- a/libs/core/tests/unit_tests/output_parsers/test_base_parsers.py +++ b/libs/core/tests/unit_tests/output_parsers/test_base_parsers.py @@ -25,7 +25,7 @@ def test_base_generation_parser() -> None: """Parse a list of model Generations into a specific format. Args: - result: A list of Generations to be parsed. The Generations are assumed + result: A list of `Generation` to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that @@ -67,7 +67,7 @@ def test_base_transform_output_parser() -> None: """Parse a list of model Generations into a specific format. Args: - result: A list of Generations to be parsed. The Generations are assumed + result: A list of `Generation` to be parsed. The Generations are assumed to be different candidate outputs for a single model input. Many parsers assume that only a single generation is passed it in. We will assert for that diff --git a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr index 282713e6f33..0958f2ff241 100644 --- a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr +++ b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr @@ -7,7 +7,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -389,7 +389,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -1143,7 +1143,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -1428,7 +1428,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -1810,7 +1810,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -2564,7 +2564,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ diff --git a/libs/core/tests/unit_tests/prompts/test_structured.py b/libs/core/tests/unit_tests/prompts/test_structured.py index 44c6a215ba9..a3568bd380f 100644 --- a/libs/core/tests/unit_tests/prompts/test_structured.py +++ b/libs/core/tests/unit_tests/prompts/test_structured.py @@ -26,7 +26,7 @@ def _fake_runnable( class FakeStructuredChatModel(FakeListChatModel): - """Fake ChatModel for testing purposes.""" + """Fake chat model for testing purposes.""" @override def with_structured_output( diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr index b2d8068bfd8..6c957e613fc 100644 --- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr +++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_graph.ambr @@ -431,7 +431,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -813,7 +813,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -1567,7 +1567,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr index 599ef032e39..618a9147eb5 100644 --- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr +++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr @@ -1963,7 +1963,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -2340,7 +2340,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -3085,7 +3085,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -3364,7 +3364,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -3804,7 +3804,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -4568,7 +4568,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -4859,7 +4859,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -5299,7 +5299,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -6063,7 +6063,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -6292,7 +6292,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -6669,7 +6669,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -7414,7 +7414,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -7735,7 +7735,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -8175,7 +8175,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -8939,7 +8939,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -9213,7 +9213,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -9590,7 +9590,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -10335,7 +10335,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -10564,7 +10564,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -11004,7 +11004,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -11779,7 +11779,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -12020,7 +12020,7 @@ 'description': ''' Message from an AI. - AIMessage is returned from a chat model as a response to a prompt. + `AIMessage` is returned from a chat model as a response to a prompt. This message represents the output of the model and consists of both the raw output as returned by the model together standardized fields @@ -12460,7 +12460,7 @@ do not contain the `tool_call_id` field. The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ @@ -13224,7 +13224,7 @@ ``` The `tool_call_id` field is used to associate the tool call request with the - tool call response. This is useful in situations where a chat model is able + tool call response. Useful in situations where a chat model is able to request multiple tool calls in parallel. ''', 'properties': dict({ diff --git a/libs/langchain/langchain_classic/agents/agent.py b/libs/langchain/langchain_classic/agents/agent.py index 452f6df4a71..180f9ab0374 100644 --- a/libs/langchain/langchain_classic/agents/agent.py +++ b/libs/langchain/langchain_classic/agents/agent.py @@ -328,8 +328,8 @@ class BaseMultiActionAgent(BaseModel): file_path: Path to file to save the agent to. Raises: - NotImplementedError: If agent does not support saving. - ValueError: If file_path is not json or yaml. + `NotImplementedError`: If agent does not support saving. + `ValueError`: If `file_path` is not json or yaml. Example: ```python @@ -1053,9 +1053,9 @@ class AgentExecutor(Chain): Defaults to `False`, which raises the error. If `true`, the error will be sent back to the LLM as an observation. If a string, the string itself will be sent to the LLM as an observation. - If a callable function, the function will be called with the exception - as an argument, and the result of that function will be passed to the agent - as an observation. + If a callable function, the function will be called with the exception as an + argument, and the result of that function will be passed to the agent as an + observation. """ trim_intermediate_steps: ( int | Callable[[list[tuple[AgentAction, str]]], list[tuple[AgentAction, str]]] diff --git a/libs/langchain/langchain_classic/agents/agent_toolkits/conversational_retrieval/openai_functions.py b/libs/langchain/langchain_classic/agents/agent_toolkits/conversational_retrieval/openai_functions.py index 5b8ddfc6e1a..3c054a3ddb8 100644 --- a/libs/langchain/langchain_classic/agents/agent_toolkits/conversational_retrieval/openai_functions.py +++ b/libs/langchain/langchain_classic/agents/agent_toolkits/conversational_retrieval/openai_functions.py @@ -37,7 +37,7 @@ def create_conversational_retrieval_agent( """A convenience method for creating a conversational retrieval agent. Args: - llm: The language model to use, should be ChatOpenAI + llm: The language model to use, should be `ChatOpenAI` tools: A list of tools the agent has access to remember_intermediate_steps: Whether the agent should remember intermediate steps or not. Intermediate steps refer to prior action/observation @@ -47,11 +47,9 @@ def create_conversational_retrieval_agent( memory_key: The name of the memory key in the prompt. system_message: The system message to use. By default, a basic one will be used. - verbose: Whether or not the final AgentExecutor should be verbose or not, - defaults to False. + verbose: Whether or not the final AgentExecutor should be verbose or not. max_token_limit: The max number of tokens to keep around in memory. - Defaults to 2000. - **kwargs: Additional keyword arguments to pass to the AgentExecutor. + **kwargs: Additional keyword arguments to pass to the `AgentExecutor`. Returns: An agent executor initialized appropriately diff --git a/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/base.py b/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/base.py index d59458e761f..9a97eb32beb 100644 --- a/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/base.py +++ b/libs/langchain/langchain_classic/agents/agent_toolkits/vectorstore/base.py @@ -87,12 +87,11 @@ def create_vectorstore_agent( llm: LLM that will be used by the agent toolkit: Set of tools for the agent callback_manager: Object to handle the callback - prefix: The prefix prompt for the agent. If not provided uses default PREFIX. + prefix: The prefix prompt for the agent. verbose: If you want to see the content of the scratchpad. - [ Defaults to `False` ] agent_executor_kwargs: If there is any other parameter you want to send to the - agent. [ Defaults to `None` ] - kwargs: Additional named parameters to pass to the ZeroShotAgent. + agent. + kwargs: Additional named parameters to pass to the `ZeroShotAgent`. Returns: Returns a callable AgentExecutor object. @@ -202,15 +201,14 @@ def create_vectorstore_router_agent( vector stores callback_manager: Object to handle the callback prefix: The prefix prompt for the router agent. - If not provided uses default ROUTER_PREFIX. + If not provided uses default `ROUTER_PREFIX`. verbose: If you want to see the content of the scratchpad. - [ Defaults to `False` ] agent_executor_kwargs: If there is any other parameter you want to send to the - agent. [ Defaults to `None` ] - kwargs: Additional named parameters to pass to the ZeroShotAgent. + agent. + kwargs: Additional named parameters to pass to the `ZeroShotAgent`. Returns: - Returns a callable AgentExecutor object. + Returns a callable `AgentExecutor` object. Either you can call it or use run method with the query to get the response. """ diff --git a/libs/langchain/langchain_classic/agents/chat/base.py b/libs/langchain/langchain_classic/agents/chat/base.py index 136d4f1f4f0..d56cab58944 100644 --- a/libs/langchain/langchain_classic/agents/chat/base.py +++ b/libs/langchain/langchain_classic/agents/chat/base.py @@ -94,13 +94,10 @@ class ChatAgent(Agent): Args: tools: A list of tools. system_message_prefix: The system message prefix. - Default is SYSTEM_MESSAGE_PREFIX. system_message_suffix: The system message suffix. - Default is SYSTEM_MESSAGE_SUFFIX. - human_message: The human message. Default is HUMAN_MESSAGE. + human_message: The human message. format_instructions: The format instructions. - Default is FORMAT_INSTRUCTIONS. - input_variables: The input variables. Default is None. + input_variables: The input variables. Returns: A prompt template. @@ -141,16 +138,13 @@ class ChatAgent(Agent): Args: llm: The language model. tools: A list of tools. - callback_manager: The callback manager. Default is None. - output_parser: The output parser. Default is None. + callback_manager: The callback manager. + output_parser: The output parser. system_message_prefix: The system message prefix. - Default is SYSTEM_MESSAGE_PREFIX. system_message_suffix: The system message suffix. - Default is SYSTEM_MESSAGE_SUFFIX. - human_message: The human message. Default is HUMAN_MESSAGE. + human_message: The human message. format_instructions: The format instructions. - Default is FORMAT_INSTRUCTIONS. - input_variables: The input variables. Default is None. + input_variables: The input variables. kwargs: Additional keyword arguments. Returns: diff --git a/libs/langchain/langchain_classic/agents/conversational/base.py b/libs/langchain/langchain_classic/agents/conversational/base.py index f198bb733df..84afe3313d6 100644 --- a/libs/langchain/langchain_classic/agents/conversational/base.py +++ b/libs/langchain/langchain_classic/agents/conversational/base.py @@ -87,15 +87,13 @@ class ConversationalAgent(Agent): Args: tools: List of tools the agent will have access to, used to format the prompt. - prefix: String to put before the list of tools. Defaults to PREFIX. - suffix: String to put after the list of tools. Defaults to SUFFIX. - format_instructions: Instructions on how to use the tools. Defaults to - FORMAT_INSTRUCTIONS - ai_prefix: String to use before AI output. Defaults to "AI". + prefix: String to put before the list of tools. + suffix: String to put after the list of tools. + format_instructions: Instructions on how to use the tools. + ai_prefix: String to use before AI output. human_prefix: String to use before human output. - Defaults to "Human". input_variables: List of input variables the final prompt will expect. - Defaults to ["input", "chat_history", "agent_scratchpad"]. + Defaults to `["input", "chat_history", "agent_scratchpad"]`. Returns: A PromptTemplate with the template assembled from the pieces here. @@ -139,16 +137,14 @@ class ConversationalAgent(Agent): Args: llm: The language model to use. tools: A list of tools to use. - callback_manager: The callback manager to use. Default is None. - output_parser: The output parser to use. Default is None. - prefix: The prefix to use in the prompt. Default is PREFIX. - suffix: The suffix to use in the prompt. Default is SUFFIX. + callback_manager: The callback manager to use. + output_parser: The output parser to use. + prefix: The prefix to use in the prompt. + suffix: The suffix to use in the prompt. format_instructions: The format instructions to use. - Default is FORMAT_INSTRUCTIONS. - ai_prefix: The prefix to use before AI output. Default is "AI". + ai_prefix: The prefix to use before AI output. human_prefix: The prefix to use before human output. - Default is "Human". - input_variables: The input variables to use. Default is None. + input_variables: The input variables to use. **kwargs: Any additional keyword arguments to pass to the agent. Returns: diff --git a/libs/langchain/langchain_classic/agents/conversational_chat/base.py b/libs/langchain/langchain_classic/agents/conversational_chat/base.py index 23b5af55652..0630ebba992 100644 --- a/libs/langchain/langchain_classic/agents/conversational_chat/base.py +++ b/libs/langchain/langchain_classic/agents/conversational_chat/base.py @@ -88,14 +88,12 @@ class ConversationalChatAgent(Agent): Args: tools: The tools to use. system_message: The system message to use. - Defaults to the PREFIX. human_message: The human message to use. - Defaults to the SUFFIX. input_variables: The input variables to use. output_parser: The output parser to use. Returns: - A PromptTemplate. + A `PromptTemplate`. """ tool_strings = "\n".join( [f"> {tool.name}: {tool.description}" for tool in tools], @@ -150,11 +148,11 @@ class ConversationalChatAgent(Agent): Args: llm: The language model to use. tools: A list of tools to use. - callback_manager: The callback manager to use. Default is None. - output_parser: The output parser to use. Default is None. - system_message: The system message to use. Default is PREFIX. - human_message: The human message to use. Default is SUFFIX. - input_variables: The input variables to use. Default is None. + callback_manager: The callback manager to use. + output_parser: The output parser to use. + system_message: The system message to use. + human_message: The human message to use. + input_variables: The input variables to use. **kwargs: Any additional arguments. Returns: diff --git a/libs/langchain/langchain_classic/agents/format_scratchpad/log.py b/libs/langchain/langchain_classic/agents/format_scratchpad/log.py index a3672f817be..5bef3f0cec2 100644 --- a/libs/langchain/langchain_classic/agents/format_scratchpad/log.py +++ b/libs/langchain/langchain_classic/agents/format_scratchpad/log.py @@ -11,9 +11,7 @@ def format_log_to_str( Args: intermediate_steps: List of tuples of AgentAction and observation strings. observation_prefix: Prefix to append the observation with. - Defaults to "Observation: ". llm_prefix: Prefix to append the llm call with. - Defaults to "Thought: ". Returns: The scratchpad. diff --git a/libs/langchain/langchain_classic/agents/format_scratchpad/log_to_messages.py b/libs/langchain/langchain_classic/agents/format_scratchpad/log_to_messages.py index 650e58572ad..5bc338831e9 100644 --- a/libs/langchain/langchain_classic/agents/format_scratchpad/log_to_messages.py +++ b/libs/langchain/langchain_classic/agents/format_scratchpad/log_to_messages.py @@ -11,7 +11,7 @@ def format_log_to_messages( Args: intermediate_steps: List of tuples of AgentAction and observation strings. template_tool_response: Template to format the observation with. - Defaults to "{observation}". + Defaults to `"{observation}"`. Returns: The scratchpad. diff --git a/libs/langchain/langchain_classic/agents/json_chat/base.py b/libs/langchain/langchain_classic/agents/json_chat/base.py index 2c8091601e8..329612a70f4 100644 --- a/libs/langchain/langchain_classic/agents/json_chat/base.py +++ b/libs/langchain/langchain_classic/agents/json_chat/base.py @@ -30,13 +30,12 @@ def create_json_chat_agent( If `False`, does not add a stop token. If a list of str, uses the provided list as the stop tokens. - Default is True. You may to set this to False if the LLM you are using - does not support stop sequences. + You may to set this to False if the LLM you are using does not support stop + sequences. tools_renderer: This controls how the tools are converted into a string and - then passed into the LLM. Default is `render_text_description`. + then passed into the LLM. template_tool_response: Template prompt that uses the tool response (observation) to make the LLM generate the next action to take. - Default is TEMPLATE_TOOL_RESPONSE. Returns: A Runnable sequence representing an agent. It takes as input all the same input diff --git a/libs/langchain/langchain_classic/agents/mrkl/base.py b/libs/langchain/langchain_classic/agents/mrkl/base.py index de2dd7873bb..6ed82f50e72 100644 --- a/libs/langchain/langchain_classic/agents/mrkl/base.py +++ b/libs/langchain/langchain_classic/agents/mrkl/base.py @@ -93,10 +93,9 @@ class ZeroShotAgent(Agent): Args: tools: List of tools the agent will have access to, used to format the prompt. - prefix: String to put before the list of tools. Defaults to PREFIX. - suffix: String to put after the list of tools. Defaults to SUFFIX. + prefix: String to put before the list of tools. + suffix: String to put after the list of tools. format_instructions: Instructions on how to use the tools. - Defaults to FORMAT_INSTRUCTIONS input_variables: List of input variables the final prompt will expect. @@ -131,10 +130,9 @@ class ZeroShotAgent(Agent): tools: The tools to use. callback_manager: The callback manager to use. output_parser: The output parser to use. - prefix: The prefix to use. Defaults to PREFIX. - suffix: The suffix to use. Defaults to SUFFIX. + prefix: The prefix to use. + suffix: The suffix to use. format_instructions: The format instructions to use. - Defaults to FORMAT_INSTRUCTIONS. input_variables: The input variables to use. kwargs: Additional parameters to pass to the agent. """ diff --git a/libs/langchain/langchain_classic/agents/openai_functions_agent/agent_token_buffer_memory.py b/libs/langchain/langchain_classic/agents/openai_functions_agent/agent_token_buffer_memory.py index 4f8e49f0deb..666e6b8c75b 100644 --- a/libs/langchain/langchain_classic/agents/openai_functions_agent/agent_token_buffer_memory.py +++ b/libs/langchain/langchain_classic/agents/openai_functions_agent/agent_token_buffer_memory.py @@ -17,18 +17,17 @@ class AgentTokenBufferMemory(BaseChatMemory): """Memory used to save agent output AND intermediate steps. Args: - human_prefix: Prefix for human messages. Default is "Human". - ai_prefix: Prefix for AI messages. Default is "AI". + human_prefix: Prefix for human messages. + ai_prefix: Prefix for AI messages. llm: Language model. - memory_key: Key to save memory under. Default is "history". + memory_key: Key to save memory under. max_token_limit: Maximum number of tokens to keep in the buffer. Once the buffer exceeds this many tokens, the oldest - messages will be pruned. Default is 12000. - return_messages: Whether to return messages. Default is True. - output_key: Key to save output under. Default is "output". + messages will be pruned. + return_messages: Whether to return messages. + output_key: Key to save output under. intermediate_steps_key: Key to save intermediate steps under. - Default is "intermediate_steps". - format_as_tools: Whether to format as tools. Default is False. + format_as_tools: Whether to format as tools. """ human_prefix: str = "Human" diff --git a/libs/langchain/langchain_classic/agents/openai_functions_agent/base.py b/libs/langchain/langchain_classic/agents/openai_functions_agent/base.py index 0934d78c2fd..3217c1e8f2a 100644 --- a/libs/langchain/langchain_classic/agents/openai_functions_agent/base.py +++ b/libs/langchain/langchain_classic/agents/openai_functions_agent/base.py @@ -40,15 +40,14 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent): """An Agent driven by OpenAIs function powered API. Args: - llm: This should be an instance of ChatOpenAI, specifically a model + llm: This should be an instance of `ChatOpenAI`, specifically a model that supports using `functions`. tools: The tools this agent has access to. prompt: The prompt for this agent, should support agent_scratchpad as one of the variables. For an easy way to construct this prompt, use `OpenAIFunctionsAgent.create_prompt(...)` output_parser: The output parser for this agent. Should be an instance of - OpenAIFunctionsAgentOutputParser. - Defaults to OpenAIFunctionsAgentOutputParser. + `OpenAIFunctionsAgentOutputParser`. """ llm: BaseLanguageModel @@ -107,13 +106,13 @@ class OpenAIFunctionsAgent(BaseSingleActionAgent): intermediate_steps: Steps the LLM has taken to date, along with observations. callbacks: Callbacks to use. - with_functions: Whether to use functions. Defaults to `True`. + with_functions: Whether to use functions. **kwargs: User inputs. Returns: Action specifying what tool to use. - If the agent is finished, returns an AgentFinish. - If the agent is not finished, returns an AgentAction. + If the agent is finished, returns an `AgentFinish`. + If the agent is not finished, returns an `AgentAction`. """ agent_scratchpad = format_to_openai_function_messages(intermediate_steps) selected_inputs = { diff --git a/libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py b/libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py index 3c3383dd451..340a71c21cb 100644 --- a/libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py +++ b/libs/langchain/langchain_classic/agents/openai_functions_multi_agent/base.py @@ -212,7 +212,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent): Args: intermediate_steps: Steps the LLM has taken to date, along with observations. - callbacks: Callbacks to use. Default is None. + callbacks: Callbacks to use. **kwargs: User inputs. Returns: @@ -243,7 +243,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent): Args: intermediate_steps: Steps the LLM has taken to date, along with observations. - callbacks: Callbacks to use. Default is None. + callbacks: Callbacks to use. **kwargs: User inputs. Returns: @@ -275,7 +275,7 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent): system_message: Message to use as the system message that will be the first in the prompt. extra_prompt_messages: Prompt messages that will be placed between the - system message and the new human input. Default is None. + system message and the new human input. Returns: A prompt template to pass into this agent. @@ -313,10 +313,10 @@ class OpenAIMultiFunctionsAgent(BaseMultiActionAgent): Args: llm: The language model to use. tools: A list of tools to use. - callback_manager: The callback manager to use. Default is None. - extra_prompt_messages: Extra prompt messages to use. Default is None. - system_message: The system message to use. - Default is a default system message. + callback_manager: The callback manager to use. + extra_prompt_messages: Extra prompt messages to use. + system_message: The system message to use. Default is a default system + message. kwargs: Additional arguments. """ system_message_ = ( diff --git a/libs/langchain/langchain_classic/agents/react/agent.py b/libs/langchain/langchain_classic/agents/react/agent.py index ce56d6ed667..48dafe010b8 100644 --- a/libs/langchain/langchain_classic/agents/react/agent.py +++ b/libs/langchain/langchain_classic/agents/react/agent.py @@ -42,13 +42,13 @@ def create_react_agent( prompt: The prompt to use. See Prompt section below for more. output_parser: AgentOutputParser for parse the LLM output. tools_renderer: This controls how the tools are converted into a string and - then passed into the LLM. Default is `render_text_description`. + then passed into the LLM. stop_sequence: bool or list of str. If `True`, adds a stop token of "Observation:" to avoid hallucinates. If `False`, does not add a stop token. If a list of str, uses the provided list as the stop tokens. - Default is True. You may to set this to False if the LLM you are using + You may to set this to False if the LLM you are using does not support stop sequences. Returns: diff --git a/libs/langchain/langchain_classic/agents/structured_chat/base.py b/libs/langchain/langchain_classic/agents/structured_chat/base.py index 2a2d9f25aeb..6533f9a7790 100644 --- a/libs/langchain/langchain_classic/agents/structured_chat/base.py +++ b/libs/langchain/langchain_classic/agents/structured_chat/base.py @@ -182,10 +182,10 @@ def create_structured_chat_agent( If `False`, does not add a stop token. If a list of str, uses the provided list as the stop tokens. - Default is True. You may to set this to False if the LLM you are using + You may to set this to False if the LLM you are using does not support stop sequences. tools_renderer: This controls how the tools are converted into a string and - then passed into the LLM. Default is `render_text_description`. + then passed into the LLM. Returns: A Runnable sequence representing an agent. It takes as input all the same input diff --git a/libs/langchain/langchain_classic/agents/xml/base.py b/libs/langchain/langchain_classic/agents/xml/base.py index d06ff88c1ed..6b58afee65a 100644 --- a/libs/langchain/langchain_classic/agents/xml/base.py +++ b/libs/langchain/langchain_classic/agents/xml/base.py @@ -129,13 +129,13 @@ def create_xml_agent( `tools`: contains descriptions for each tool. `agent_scratchpad`: contains previous agent actions and tool outputs. tools_renderer: This controls how the tools are converted into a string and - then passed into the LLM. Default is `render_text_description`. + then passed into the LLM. stop_sequence: bool or list of str. If `True`, adds a stop token of "" to avoid hallucinates. If `False`, does not add a stop token. If a list of str, uses the provided list as the stop tokens. - Default is True. You may to set this to False if the LLM you are using + You may to set this to False if the LLM you are using does not support stop sequences. Returns: diff --git a/libs/langchain/langchain_classic/callbacks/streamlit/__init__.py b/libs/langchain/langchain_classic/callbacks/streamlit/__init__.py index 7d16e932b7e..a3b68eb384f 100644 --- a/libs/langchain/langchain_classic/callbacks/streamlit/__init__.py +++ b/libs/langchain/langchain_classic/callbacks/streamlit/__init__.py @@ -31,13 +31,12 @@ def StreamlitCallbackHandler( # noqa: N802 max_thought_containers The max number of completed LLM thought containers to show at once. When this threshold is reached, a new thought will cause the oldest thoughts to be - collapsed into a "History" expander. Defaults to 4. + collapsed into a "History" expander. expand_new_thoughts Each LLM "thought" gets its own `st.expander`. This param controls whether that - expander is expanded by default. Defaults to `True`. + expander is expanded by default. collapse_completed_thoughts If `True`, LLM thought expanders will be collapsed when completed. - Defaults to `True`. thought_labeler An optional custom LLMThoughtLabeler instance. If unspecified, the handler will use the default thought labeling logic. diff --git a/libs/langchain/langchain_classic/chains/base.py b/libs/langchain/langchain_classic/chains/base.py index df2d4b4c3eb..a259e8e5bc7 100644 --- a/libs/langchain/langchain_classic/chains/base.py +++ b/libs/langchain/langchain_classic/chains/base.py @@ -387,7 +387,7 @@ class Chain(RunnableSerializable[dict[str, Any], dict[str, Any]], ABC): return_only_outputs: Whether to return only outputs in the response. If `True`, only new keys generated by this chain will be returned. If `False`, both input keys and new keys generated by this - chain will be returned. Defaults to `False`. + chain will be returned. callbacks: Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects. @@ -439,7 +439,7 @@ class Chain(RunnableSerializable[dict[str, Any], dict[str, Any]], ABC): return_only_outputs: Whether to return only outputs in the response. If `True`, only new keys generated by this chain will be returned. If `False`, both input keys and new keys generated by this - chain will be returned. Defaults to `False`. + chain will be returned. callbacks: Callbacks to use for this chain run. These will be called in addition to callbacks passed to the chain during construction, but only these runtime callbacks will propagate to calls to other objects. diff --git a/libs/langchain/langchain_classic/chains/combine_documents/stuff.py b/libs/langchain/langchain_classic/chains/combine_documents/stuff.py index 363d74dcb6f..668bc16372f 100644 --- a/libs/langchain/langchain_classic/chains/combine_documents/stuff.py +++ b/libs/langchain/langchain_classic/chains/combine_documents/stuff.py @@ -35,10 +35,10 @@ def create_stuff_documents_chain( Args: llm: Language model. - prompt: Prompt template. Must contain input variable "context" (override by + prompt: Prompt template. Must contain input variable `"context"` (override by setting document_variable), which will be used for passing in the formatted documents. - output_parser: Output parser. Defaults to StrOutputParser. + output_parser: Output parser. Defaults to `StrOutputParser`. document_prompt: Prompt used for formatting each document into a string. Input variables can be "page_content" or any metadata keys that are in all documents. "page_content" will automatically retrieve the @@ -47,12 +47,12 @@ def create_stuff_documents_chain( a prompt that only contains `Document.page_content`. document_separator: String separator to use between formatted document strings. document_variable_name: Variable name to use for the formatted documents in the - prompt. Defaults to "context". + prompt. Defaults to `"context"`. Returns: - An LCEL Runnable. The input is a dictionary that must have a "context" key that - maps to a List[Document], and any other input variables expected in the prompt. - The Runnable return type depends on output_parser used. + An LCEL Runnable. The input is a dictionary that must have a `"context"` key + that maps to a `list[Document]`, and any other input variables expected in the + prompt. The `Runnable` return type depends on `output_parser` used. Example: ```python diff --git a/libs/langchain/langchain_classic/chains/elasticsearch_database/base.py b/libs/langchain/langchain_classic/chains/elasticsearch_database/base.py index 589cccbb832..b9f4afd6030 100644 --- a/libs/langchain/langchain_classic/chains/elasticsearch_database/base.py +++ b/libs/langchain/langchain_classic/chains/elasticsearch_database/base.py @@ -198,7 +198,7 @@ class ElasticsearchDatabaseChain(Chain): query_prompt: The prompt to use for query construction. answer_prompt: The prompt to use for answering user question given data. query_output_parser: The output parser to use for parsing model-generated - ES query. Defaults to SimpleJsonOutputParser. + ES query. Defaults to `SimpleJsonOutputParser`. kwargs: Additional arguments to pass to the constructor. """ query_prompt = query_prompt or DSL_PROMPT diff --git a/libs/langchain/langchain_classic/chains/llm.py b/libs/langchain/langchain_classic/chains/llm.py index fae99da5d53..b62edcd3023 100644 --- a/libs/langchain/langchain_classic/chains/llm.py +++ b/libs/langchain/langchain_classic/chains/llm.py @@ -88,8 +88,8 @@ class LLMChain(Chain): Defaults to one that takes the most likely string but does not change it otherwise.""" return_final_only: bool = True - """Whether to return only the final parsed result. Defaults to `True`. - If false, will return a bunch of extra information about the generation.""" + """Whether to return only the final parsed result. + If `False`, will return a bunch of extra information about the generation.""" llm_kwargs: dict = Field(default_factory=dict) model_config = ConfigDict( diff --git a/libs/langchain/langchain_classic/chains/openai_functions/extraction.py b/libs/langchain/langchain_classic/chains/openai_functions/extraction.py index 44f00f196ec..ce60f7c6b7e 100644 --- a/libs/langchain/langchain_classic/chains/openai_functions/extraction.py +++ b/libs/langchain/langchain_classic/chains/openai_functions/extraction.py @@ -94,8 +94,7 @@ def create_extraction_chain( prompt: The prompt to use for extraction. tags: Optional list of tags to associate with the chain. verbose: Whether to run in verbose mode. In verbose mode, some intermediate - logs will be printed to the console. Defaults to the global `verbose` value, - accessible via `langchain.globals.get_verbose()`. + logs will be printed to the console. Returns: Chain that can be used to extract information from a passage. @@ -162,8 +161,7 @@ def create_extraction_chain_pydantic( llm: The language model to use. prompt: The prompt to use for extraction. verbose: Whether to run in verbose mode. In verbose mode, some intermediate - logs will be printed to the console. Defaults to the global `verbose` value, - accessible via `langchain.globals.get_verbose()` + logs will be printed to the console. Returns: Chain that can be used to extract information from a passage. diff --git a/libs/langchain/langchain_classic/chains/openai_functions/qa_with_structure.py b/libs/langchain/langchain_classic/chains/openai_functions/qa_with_structure.py index 8e7bede26c6..b36a9c49a22 100644 --- a/libs/langchain/langchain_classic/chains/openai_functions/qa_with_structure.py +++ b/libs/langchain/langchain_classic/chains/openai_functions/qa_with_structure.py @@ -51,8 +51,7 @@ def create_qa_with_structure_chain( Args: llm: Language model to use for the chain. schema: Pydantic schema to use for the output. - output_parser: Output parser to use. Should be one of `pydantic` or `base`. - Default to `base`. + output_parser: Output parser to use. Should be one of `'pydantic'` or `'base'`. prompt: Optional prompt to use for the chain. verbose: Whether to run the chain in verbose mode. diff --git a/libs/langchain/langchain_classic/chains/query_constructor/base.py b/libs/langchain/langchain_classic/chains/query_constructor/base.py index 902ea26b8b3..ed42df6cb29 100644 --- a/libs/langchain/langchain_classic/chains/query_constructor/base.py +++ b/libs/langchain/langchain_classic/chains/query_constructor/base.py @@ -218,7 +218,7 @@ def get_query_constructor_prompt( examples: Optional list of examples to use for the chain. allowed_comparators: Sequence of allowed comparators. allowed_operators: Sequence of allowed operators. - enable_limit: Whether to enable the limit operator. Defaults to `False`. + enable_limit: Whether to enable the limit operator. schema_prompt: Prompt for describing query schema. Should have string input variables allowed_comparators and allowed_operators. kwargs: Additional named params to pass to FewShotPromptTemplate init. @@ -289,9 +289,10 @@ def load_query_constructor_chain( attribute_info: Sequence of attributes in the document. examples: Optional list of examples to use for the chain. allowed_comparators: Sequence of allowed comparators. Defaults to all - Comparators. - allowed_operators: Sequence of allowed operators. Defaults to all Operators. - enable_limit: Whether to enable the limit operator. Defaults to `False`. + `Comparator` objects. + allowed_operators: Sequence of allowed operators. Defaults to all `Operator` + objects. + enable_limit: Whether to enable the limit operator. schema_prompt: Prompt for describing query schema. Should have string input variables allowed_comparators and allowed_operators. **kwargs: Arbitrary named params to pass to LLMChain. @@ -344,9 +345,10 @@ def load_query_constructor_runnable( attribute_info: Sequence of attributes in the document. examples: Optional list of examples to use for the chain. allowed_comparators: Sequence of allowed comparators. Defaults to all - Comparators. - allowed_operators: Sequence of allowed operators. Defaults to all Operators. - enable_limit: Whether to enable the limit operator. Defaults to `False`. + `Comparator` objects. + allowed_operators: Sequence of allowed operators. Defaults to all `Operator` + objects. + enable_limit: Whether to enable the limit operator. schema_prompt: Prompt for describing query schema. Should have string input variables allowed_comparators and allowed_operators. fix_invalid: Whether to fix invalid filter directives by ignoring invalid diff --git a/libs/langchain/langchain_classic/chains/router/base.py b/libs/langchain/langchain_classic/chains/router/base.py index ea078cb037b..8122a90b65c 100644 --- a/libs/langchain/langchain_classic/chains/router/base.py +++ b/libs/langchain/langchain_classic/chains/router/base.py @@ -73,8 +73,7 @@ class MultiRouteChain(Chain): default_chain: Chain """Default chain to use when none of the destination chains are suitable.""" silent_errors: bool = False - """If `True`, use default_chain when an invalid destination name is provided. - Defaults to `False`.""" + """If `True`, use default_chain when an invalid destination name is provided.""" model_config = ConfigDict( arbitrary_types_allowed=True, diff --git a/libs/langchain/langchain_classic/chains/sql_database/query.py b/libs/langchain/langchain_classic/chains/sql_database/query.py index aa26b6bae21..12c6f4561e4 100644 --- a/libs/langchain/langchain_classic/chains/sql_database/query.py +++ b/libs/langchain/langchain_classic/chains/sql_database/query.py @@ -60,9 +60,8 @@ def create_sql_query_chain( db: The SQLDatabase to generate the query for. prompt: The prompt to use. If none is provided, will choose one based on dialect. See Prompt section below for more. - k: The number of results per select statement to return. Defaults to 5. + k: The number of results per select statement to return. get_col_comments: Whether to retrieve column comments along with table info. - Defaults to `False`. Returns: A chain that takes in a question and generates a SQL query that answers diff --git a/libs/langchain/langchain_classic/chains/summarize/chain.py b/libs/langchain/langchain_classic/chains/summarize/chain.py index dad2d373b11..163cc8eb1e9 100644 --- a/libs/langchain/langchain_classic/chains/summarize/chain.py +++ b/libs/langchain/langchain_classic/chains/summarize/chain.py @@ -47,9 +47,9 @@ def _load_stuff_chain( Args: llm: Language Model to use in the chain. prompt: Prompt template that controls how the documents are formatted and - passed into the LLM. Defaults to `stuff_prompt.PROMPT`. + passed into the LLM. document_variable_name: Variable name in the prompt template where the - document text will be inserted. Defaults to "text". + document text will be inserted. verbose: Whether to log progress and intermediate steps. **kwargs: Additional keyword arguments passed to the StuffDocumentsChain. @@ -104,22 +104,19 @@ def _load_map_reduce_chain( Args: llm: Language Model to use for map and reduce steps. map_prompt: Prompt used to summarize each documnet in the map step. - Defaults to `map_reduce_prompt.PROMPT`. combine_prompt: Prompt used to combine summaries in the reduce step. - Defaults to `map_reduce_prompt.PROMPT`. combine_document_variable_name: Variable name in the `combine_prompt` where - the mapped summaries are inserted. Defaults to "text". + the mapped summaries are inserted. map_reduce_document_variable_name: Variable name in the `map_prompt` - where document text is inserted. Defaults to "text". + where document text is inserted. collapse_prompt: Optional prompt used to collapse intermediate summaries if they exceed the token limit (`token_max`). - reduce_llm: Optional separate LLM for the reduce step. Defaults to `None`, + reduce_llm: Optional separate LLM for the reduce step. which uses the same model as the map step. - collapse_llm: Optional separate LLM for the collapse step. Defaults to `None`, + collapse_llm: Optional separate LLM for the collapse step. which uses the same model as the map step. verbose: Whether to log progess and intermediate steps. token_max: Token threshold that triggers the collapse step during reduction. - Defaults to 3000. callbacks: Optional callbacks for logging and tracing. collapse_max_retries: Maximum retries for the collapse step if it fails. diff --git a/libs/langchain/langchain_classic/chat_models/base.py b/libs/langchain/langchain_classic/chat_models/base.py index 20a888e9166..b0d28fd2648 100644 --- a/libs/langchain/langchain_classic/chat_models/base.py +++ b/libs/langchain/langchain_classic/chat_models/base.py @@ -76,15 +76,19 @@ def init_chat_model( config_prefix: str | None = None, **kwargs: Any, ) -> BaseChatModel | _ConfigurableModel: - """Initialize a ChatModel in a single line using the model's name and provider. + """Initialize a chat model in a single line using the model's name and provider. !!! note - Must have the integration package corresponding to the model provider installed. - You should look at the [provider integration's API reference](https://docs.langchain.com/oss/python/integrations/providers) - to see what parameters are supported by the model. + Requires the integration package for your model provider to be installed. + + See the `model_provider` parameter below for specific package names + (e.g., `pip install langchain-openai`). + + Refer to the [provider integration's API reference](https://docs.langchain.com/oss/python/integrations/providers) + for supported model parameters. Args: - model: The name of the model, e.g. `'o3-mini'`, `'claude-3-5-sonnet-latest'`. You can + model: The name of the model, e.g. `'o3-mini'`, `'claude-sonnet-4-5-20250929'`. You can also specify model and model provider in a single argument using `'{model_provider}:{model}'` format, e.g. `'openai:o1'`. model_provider: The model provider if not specified as part of model arg (see @@ -113,7 +117,7 @@ def init_chat_model( - `xai` -> `langchain-xai` - `perplexity` -> `langchain-perplexity` - Will attempt to infer model_provider from model if not specified. The + Will attempt to infer `model_provider` from model if not specified. The following providers will be inferred based on these model prefixes: - `gpt-...` | `o1...` | `o3...` -> `openai` @@ -128,15 +132,15 @@ def init_chat_model( - `sonar...` -> `perplexity` configurable_fields: Which model parameters are configurable: - - None: No configurable fields. + - `None`: No configurable fields. - `'any'`: All fields are configurable. **See Security Note below.** - - Union[List[str], Tuple[str, ...]]: Specified fields are configurable. + - `list[str] | Tuple[str, ...]`: Specified fields are configurable. - Fields are assumed to have config_prefix stripped if there is a - config_prefix. If model is specified, then defaults to None. If model is + Fields are assumed to have `config_prefix` stripped if there is a + `config_prefix`. If model is specified, then defaults to `None`. If model is not specified, then defaults to `("model", "model_provider")`. - ***Security Note***: Setting `configurable_fields="any"` means fields like + **Security Note**: Setting `configurable_fields="any"` means fields like `api_key`, `base_url`, etc. can be altered at runtime, potentially redirecting model requests to a different service/user. Make sure that if you're accepting untrusted configurations that you enumerate the @@ -157,16 +161,17 @@ def init_chat_model( rate_limiter: A `BaseRateLimiter` to space out requests to avoid exceeding rate limits. kwargs: Additional model-specific keyword args to pass to - `<>.__init__(model=model_name, **kwargs)`. + `<>.__init__(model=model_name, **kwargs)`. Returns: - A BaseChatModel corresponding to the model_name and model_provider specified if - configurability is inferred to be False. If configurable, a chat model emulator - that initializes the underlying model at runtime once a config is passed in. + A `BaseChatModel` corresponding to the `model_name` and `model_provider` + specified if configurability is inferred to be `False`. If configurable, a + chat model emulator that initializes the underlying model at runtime once a + config is passed in. Raises: - ValueError: If model_provider cannot be inferred or isn't supported. - ImportError: If the model provider integration package is not installed. + `ValueError`: If `model_provider` cannot be inferred or isn't supported. + `ImportError`: If the model provider integration package is not installed. ???+ note "Init non-configurable model" @@ -176,7 +181,7 @@ def init_chat_model( o3_mini = init_chat_model("openai:o3-mini", temperature=0) claude_sonnet = init_chat_model( - "anthropic:claude-3-5-sonnet-latest", temperature=0 + "anthropic:claude-sonnet-4-5-20250929", temperature=0 ) gemini_2_flash = init_chat_model( "google_vertexai:gemini-2.5-flash", temperature=0 @@ -203,9 +208,8 @@ def init_chat_model( configurable_model.invoke( "what's your name", - config={"configurable": {"model": "claude-3-5-sonnet-latest"}}, + config={"configurable": {"model": "claude-sonnet-4-5-20250929"}}, ) - # claude-3.5 sonnet response ``` ??? note "Fully configurable model with a default" @@ -216,7 +220,7 @@ def init_chat_model( configurable_model_with_default = init_chat_model( "openai:gpt-4o", - configurable_fields="any", # this allows us to configure other params like temperature, max_tokens, etc at runtime. + configurable_fields="any", # This allows us to configure other params like temperature, max_tokens, etc at runtime. config_prefix="foo", temperature=0, ) @@ -228,17 +232,16 @@ def init_chat_model( "what's your name", config={ "configurable": { - "foo_model": "anthropic:claude-3-5-sonnet-latest", + "foo_model": "anthropic:claude-sonnet-4-5-20250929", "foo_temperature": 0.6, } }, ) - # Claude-3.5 sonnet response with temperature 0.6 ``` ??? note "Bind tools to a configurable model" - You can call any ChatModel declarative methods on a configurable model in the + You can call any chat model declarative methods on a configurable model in the same way that you would with a normal model. ```python @@ -276,13 +279,11 @@ def init_chat_model( configurable_model_with_tools.invoke( "Which city is hotter today and which is bigger: LA or NY?" ) - # GPT-4o response with tool calls configurable_model_with_tools.invoke( "Which city is hotter today and which is bigger: LA or NY?", - config={"configurable": {"model": "claude-3-5-sonnet-latest"}}, + config={"configurable": {"model": "claude-sonnet-4-5-20250929"}}, ) - # Claude-3.5 sonnet response with tools ``` !!! version-added "Added in version 0.2.7" @@ -292,12 +293,12 @@ def init_chat_model( !!! warning "Behavior changed in 0.2.12" Support for Ollama via langchain-ollama package added - (langchain_ollama.ChatOllama). Previously, + (`langchain_ollama.ChatOllama`). Previously, the now-deprecated langchain-community version of Ollama was imported - (langchain_community.chat_models.ChatOllama). + (`langchain_community.chat_models.ChatOllama`). Support for AWS Bedrock models via the Converse API added - (model_provider="bedrock_converse"). + (`model_provider="bedrock_converse"`). !!! warning "Behavior changed in 0.3.5" Out of beta. diff --git a/libs/langchain/langchain_classic/embeddings/base.py b/libs/langchain/langchain_classic/embeddings/base.py index d7f4f8021eb..e409f2216a3 100644 --- a/libs/langchain/langchain_classic/embeddings/base.py +++ b/libs/langchain/langchain_classic/embeddings/base.py @@ -138,7 +138,7 @@ def init_embeddings( Args: model: Name of the model to use. Can be either: - - A model string like "openai:text-embedding-3-small" + - A model string like `"openai:text-embedding-3-small"` - Just the model name if provider is specified provider: Optional explicit provider name. If not specified, will attempt to parse from the model string. Supported providers diff --git a/libs/langchain/langchain_classic/evaluation/exact_match/base.py b/libs/langchain/langchain_classic/evaluation/exact_match/base.py index 93ebad7daac..05f0202a316 100644 --- a/libs/langchain/langchain_classic/evaluation/exact_match/base.py +++ b/libs/langchain/langchain_classic/evaluation/exact_match/base.py @@ -31,15 +31,12 @@ class ExactMatchStringEvaluator(StringEvaluator): ignore_numbers: bool = False, **_: Any, ): - """Initialize the ExactMatchStringEvaluator. + """Initialize the `ExactMatchStringEvaluator`. Args: ignore_case: Whether to ignore case when comparing strings. - Defaults to `False`. ignore_punctuation: Whether to ignore punctuation when comparing strings. - Defaults to `False`. ignore_numbers: Whether to ignore numbers when comparing strings. - Defaults to `False`. """ super().__init__() self.ignore_case = ignore_case diff --git a/libs/langchain/langchain_classic/evaluation/regex_match/base.py b/libs/langchain/langchain_classic/evaluation/regex_match/base.py index 40b16085efc..cda1ecee143 100644 --- a/libs/langchain/langchain_classic/evaluation/regex_match/base.py +++ b/libs/langchain/langchain_classic/evaluation/regex_match/base.py @@ -33,7 +33,7 @@ class RegexMatchStringEvaluator(StringEvaluator): """Initialize the RegexMatchStringEvaluator. Args: - flags: Flags to use for the regex match. Defaults to 0 (no flags). + flags: Flags to use for the regex match. Defaults to no flags. """ super().__init__() self.flags = flags diff --git a/libs/langchain/langchain_classic/indexes/_sql_record_manager.py b/libs/langchain/langchain_classic/indexes/_sql_record_manager.py index c5dbcfd8a67..e9ab8413ebb 100644 --- a/libs/langchain/langchain_classic/indexes/_sql_record_manager.py +++ b/libs/langchain/langchain_classic/indexes/_sql_record_manager.py @@ -97,21 +97,17 @@ class SQLRecordManager(RecordManager): """Initialize the SQLRecordManager. This class serves as a manager persistence layer that uses an SQL - backend to track upserted records. You should specify either a db_url + backend to track upserted records. You should specify either a `db_url` to create an engine or provide an existing engine. Args: namespace: The namespace associated with this record manager. engine: An already existing SQL Alchemy engine. - Default is None. - db_url: A database connection string used to create - an SQL Alchemy engine. Default is None. - engine_kwargs: Additional keyword arguments - to be passed when creating the engine. Default is an empty dictionary. - async_mode: Whether to create an async engine. - Driver should support async operations. - It only applies if db_url is provided. - Default is False. + db_url: A database connection string used to create an SQL Alchemy engine. + engine_kwargs: Additional keyword arguments to be passed when creating the + engine. + async_mode: Whether to create an async engine. Driver should support async + operations. It only applies if `db_url` is provided. Raises: ValueError: If both db_url and engine are provided or neither. diff --git a/libs/langchain/langchain_classic/memory/vectorstore_token_buffer_memory.py b/libs/langchain/langchain_classic/memory/vectorstore_token_buffer_memory.py index ede7673f9a5..dbbdc01c811 100644 --- a/libs/langchain/langchain_classic/memory/vectorstore_token_buffer_memory.py +++ b/libs/langchain/langchain_classic/memory/vectorstore_token_buffer_memory.py @@ -157,7 +157,7 @@ class ConversationVectorStoreTokenBufferMemory(ConversationTokenBufferMemory): def save_remainder(self) -> None: """Save the remainder of the conversation buffer to the vector store. - This is useful if you have made the vectorstore persistent, in which + Useful if you have made the vectorstore persistent, in which case this can be called before the end of the session to store the remainder of the conversation. """ diff --git a/libs/langchain/langchain_classic/output_parsers/structured.py b/libs/langchain/langchain_classic/output_parsers/structured.py index 306e2c82530..346b0ff9f19 100644 --- a/libs/langchain/langchain_classic/output_parsers/structured.py +++ b/libs/langchain/langchain_classic/output_parsers/structured.py @@ -97,7 +97,7 @@ class StructuredOutputParser(BaseOutputParser[dict[str, Any]]): Args: only_json: If `True`, only the json in the Markdown code snippet - will be returned, without the introducing text. Defaults to `False`. + will be returned, without the introducing text. """ schema_str = "\n".join( [_get_sub_string(schema) for schema in self.response_schemas], diff --git a/libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py b/libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py index 8930f052c15..9ceb69d8b6f 100644 --- a/libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py +++ b/libs/langchain/langchain_classic/retrievers/document_compressors/cohere_rerank.py @@ -75,7 +75,6 @@ class CohereRerank(BaseDocumentCompressor): documents: A sequence of documents to rerank. model: The model to use for re-ranking. Default to self.model. top_n : The number of results to return. If `None` returns all results. - Defaults to self.top_n. max_chunks_per_doc : The maximum number of chunks derived from a document. """ # noqa: E501 if len(documents) == 0: # to avoid empty api call diff --git a/libs/langchain/langchain_classic/retrievers/document_compressors/embeddings_filter.py b/libs/langchain/langchain_classic/retrievers/document_compressors/embeddings_filter.py index 3bb99c00946..885596ddae3 100644 --- a/libs/langchain/langchain_classic/retrievers/document_compressors/embeddings_filter.py +++ b/libs/langchain/langchain_classic/retrievers/document_compressors/embeddings_filter.py @@ -33,8 +33,8 @@ class EmbeddingsFilter(BaseDocumentCompressor): two matrices (List[List[float]]) and return a matrix of scores where higher values indicate greater similarity.""" k: int | None = 20 - """The number of relevant documents to return. Can be set to None, in which case - `similarity_threshold` must be specified. Defaults to 20.""" + """The number of relevant documents to return. Can be set to `None`, in which case + `similarity_threshold` must be specified.""" similarity_threshold: float | None = None """Threshold for determining when two documents are similar enough to be considered redundant. Defaults to `None`, must be specified if `k` is set diff --git a/libs/langchain/langchain_classic/retrievers/ensemble.py b/libs/langchain/langchain_classic/retrievers/ensemble.py index 4979a5d08c5..2e0ea312f15 100644 --- a/libs/langchain/langchain_classic/retrievers/ensemble.py +++ b/libs/langchain/langchain_classic/retrievers/ensemble.py @@ -61,7 +61,6 @@ class EnsembleRetriever(BaseRetriever): weighting for all retrievers. c: A constant added to the rank, controlling the balance between the importance of high-ranked items and the consideration given to lower-ranked items. - Default is 60. id_key: The key in the document's metadata used to determine unique documents. If not specified, page_content is used. """ diff --git a/libs/langchain/langchain_classic/smith/evaluation/runner_utils.py b/libs/langchain/langchain_classic/smith/evaluation/runner_utils.py index 8db9f6fa971..7637ef266bf 100644 --- a/libs/langchain/langchain_classic/smith/evaluation/runner_utils.py +++ b/libs/langchain/langchain_classic/smith/evaluation/runner_utils.py @@ -1371,7 +1371,7 @@ async def arun_on_dataset( dataset_version: Optional version of the dataset. concurrency_level: The number of async tasks to run concurrently. project_name: Name of the project to store the traces in. - Defaults to {dataset_name}-{chain class name}-{datetime}. + Defaults to `{dataset_name}-{chain class name}-{datetime}`. project_metadata: Optional metadata to add to the project. Useful for storing information the test variant. (prompt version, model version, etc.) @@ -1546,7 +1546,7 @@ def run_on_dataset( dataset_version: Optional version of the dataset. concurrency_level: The number of async tasks to run concurrently. project_name: Name of the project to store the traces in. - Defaults to {dataset_name}-{chain class name}-{datetime}. + Defaults to `{dataset_name}-{chain class name}-{datetime}`. project_metadata: Optional metadata to add to the project. Useful for storing information the test variant. (prompt version, model version, etc.) diff --git a/libs/langchain/langchain_classic/utils/__init__.py b/libs/langchain/langchain_classic/utils/__init__.py index 533d8db4ed7..0777d743a35 100644 --- a/libs/langchain/langchain_classic/utils/__init__.py +++ b/libs/langchain/langchain_classic/utils/__init__.py @@ -1,4 +1,4 @@ -"""**Utility functions** for LangChain. +"""Utility functions for LangChain. These functions do not depend on any other LangChain module. """ diff --git a/libs/langchain_v1/langchain/agents/__init__.py b/libs/langchain_v1/langchain/agents/__init__.py index 72abb7ed627..3552fa28f69 100644 --- a/libs/langchain_v1/langchain/agents/__init__.py +++ b/libs/langchain_v1/langchain/agents/__init__.py @@ -1,4 +1,7 @@ -"""langgraph.prebuilt exposes a higher-level API for creating and executing agents and tools.""" +"""Entrypoint to building [Agents](https://docs.langchain.com/oss/python/langchain/agents) with LangChain. + +See [the docs](https://docs.langchain.com/oss/python/langchain/agents) for more details. +""" # noqa: E501 from langchain.agents.factory import create_agent from langchain.agents.middleware.types import AgentState diff --git a/libs/langchain_v1/langchain/agents/factory.py b/libs/langchain_v1/langchain/agents/factory.py index 41538cd2660..e847a654535 100644 --- a/libs/langchain_v1/langchain/agents/factory.py +++ b/libs/langchain_v1/langchain/agents/factory.py @@ -535,10 +535,10 @@ def create_agent( # noqa: PLR0915 Args: model: The language model for the agent. Can be a string identifier (e.g., `"openai:gpt-4"`), a chat model instance (e.g., `ChatOpenAI()`). - tools: A list of tools, dicts, or callables. If `None` or an empty list, + tools: A list of tools, `dicts`, or `Callable`. If `None` or an empty list, the agent will consist of a model node without a tool calling loop. system_prompt: An optional system prompt for the LLM. Prompts are converted to a - `SystemMessage` and added to the beginning of the message list. + `SystemMessage` and added to the beginning of the message list. middleware: A sequence of middleware instances to apply to the agent. Middleware can intercept and modify agent behavior at various stages. response_format: An optional configuration for structured responses. @@ -553,12 +553,15 @@ def create_agent( # noqa: PLR0915 store: An optional store object. This is used for persisting data across multiple threads (e.g., multiple conversations / users). interrupt_before: An optional list of node names to interrupt before. - This is useful if you want to add a user confirmation or other interrupt + Useful if you want to add a user confirmation or other interrupt before taking an action. interrupt_after: An optional list of node names to interrupt after. - This is useful if you want to return directly or run additional processing + Useful if you want to return directly or run additional processing on an output. - debug: A flag indicating whether to enable debug mode. + debug: Whether to enable verbose logging for graph execution. When enabled, + prints detailed information about each node execution, state updates, + and transitions during agent runtime. Useful for debugging middleware + behavior and understanding agent execution flow. name: An optional name for the `CompiledStateGraph`. This name will be automatically used when adding the agent graph to another graph as a subgraph node - particularly useful for building @@ -569,8 +572,8 @@ def create_agent( # noqa: PLR0915 A compiled `StateGraph` that can be used for chat interactions. The agent node calls the language model with the messages list (after applying - the system prompt). If the resulting AIMessage contains `tool_calls`, the graph will - then call the tools. The tools node executes the tools and adds the responses + the system prompt). If the resulting `AIMessage` contains `tool_calls`, the graph + will then call the tools. The tools node executes the tools and adds the responses to the messages list as `ToolMessage` objects. The agent node then calls the language model again. The process repeats until no more `tool_calls` are present in the response. The agent then returns the full list of messages. @@ -586,7 +589,7 @@ def create_agent( # noqa: PLR0915 graph = create_agent( - model="anthropic:claude-3-7-sonnet-latest", + model="anthropic:claude-sonnet-4-5-20250929", tools=[check_weather], system_prompt="You are a helpful assistant", ) diff --git a/libs/langchain_v1/langchain/agents/middleware/__init__.py b/libs/langchain_v1/langchain/agents/middleware/__init__.py index 5710d28037c..5c63302997a 100644 --- a/libs/langchain_v1/langchain/agents/middleware/__init__.py +++ b/libs/langchain_v1/langchain/agents/middleware/__init__.py @@ -1,4 +1,8 @@ -"""Middleware plugins for agents.""" +"""Entrypoint to using [Middleware](https://docs.langchain.com/oss/python/langchain/middleware) plugins with [Agents](https://docs.langchain.com/oss/python/langchain/agents). + +See [the docs](https://docs.langchain.com/oss/python/langchain/middleware) for more +details. +""" # noqa: E501 from .context_editing import ( ClearToolUsesEdit, diff --git a/libs/langchain_v1/langchain/agents/middleware/context_editing.py b/libs/langchain_v1/langchain/agents/middleware/context_editing.py index 746fc120cbf..1f0978ed77f 100644 --- a/libs/langchain_v1/langchain/agents/middleware/context_editing.py +++ b/libs/langchain_v1/langchain/agents/middleware/context_editing.py @@ -198,7 +198,7 @@ class ContextEditingMiddleware(AgentMiddleware): edits: Iterable[ContextEdit] | None = None, token_count_method: Literal["approximate", "model"] = "approximate", # noqa: S107 ) -> None: - """Initialise a context editing middleware instance. + """Initializes a context editing middleware instance. Args: edits: Sequence of edit strategies to apply. Defaults to a single diff --git a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py index a29b23e7ec6..cc1a4f2df3f 100644 --- a/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py +++ b/libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py @@ -110,7 +110,8 @@ class _DescriptionFactory(Protocol): class InterruptOnConfig(TypedDict): """Configuration for an action requiring human in the loop. - This is the configuration format used in the `HumanInTheLoopMiddleware.__init__` method. + This is the configuration format used in the `HumanInTheLoopMiddleware.__init__` + method. """ allowed_decisions: list[DecisionType] @@ -120,6 +121,7 @@ class InterruptOnConfig(TypedDict): """The description attached to the request for human input. Can be either: + - A static string describing the approval request - A callable that dynamically generates the description based on agent state, runtime, and tool call information @@ -171,12 +173,14 @@ class HumanInTheLoopMiddleware(AgentMiddleware): * `True` indicates all decisions are allowed: approve, edit, and reject. * `False` indicates that the tool is auto-approved. - * `InterruptOnConfig` indicates the specific decisions allowed for this tool. - The InterruptOnConfig can include a `description` field (str or callable) for - custom formatting of the interrupt description. + * `InterruptOnConfig` indicates the specific decisions allowed for this + tool. + The InterruptOnConfig can include a `description` field (`str` or + `Callable`) for custom formatting of the interrupt description. description_prefix: The prefix to use when constructing action requests. - This is used to provide context about the tool call and the action being requested. - Not used if a tool has a `description` in its InterruptOnConfig. + This is used to provide context about the tool call and the action being + requested. Not used if a tool has a `description` in its + `InterruptOnConfig`. """ super().__init__() resolved_configs: dict[str, InterruptOnConfig] = {} @@ -270,7 +274,7 @@ class HumanInTheLoopMiddleware(AgentMiddleware): raise ValueError(msg) def after_model(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None: - """Trigger interrupt flows for relevant tool calls after an AIMessage.""" + """Trigger interrupt flows for relevant tool calls after an `AIMessage`.""" messages = state["messages"] if not messages: return None diff --git a/libs/langchain_v1/langchain/agents/middleware/model_call_limit.py b/libs/langchain_v1/langchain/agents/middleware/model_call_limit.py index 326e185000b..412cd16b8e3 100644 --- a/libs/langchain_v1/langchain/agents/middleware/model_call_limit.py +++ b/libs/langchain_v1/langchain/agents/middleware/model_call_limit.py @@ -127,17 +127,16 @@ class ModelCallLimitMiddleware(AgentMiddleware[ModelCallLimitState, Any]): Args: thread_limit: Maximum number of model calls allowed per thread. - None means no limit. Defaults to `None`. + None means no limit. run_limit: Maximum number of model calls allowed per run. - None means no limit. Defaults to `None`. + None means no limit. exit_behavior: What to do when limits are exceeded. - "end": Jump to the end of the agent execution and inject an artificial AI message indicating that the limit was exceeded. - - "error": Raise a ModelCallLimitExceededError - Defaults to "end". + - "error": Raise a `ModelCallLimitExceededError` Raises: - ValueError: If both limits are None or if exit_behavior is invalid. + ValueError: If both limits are `None` or if `exit_behavior` is invalid. """ super().__init__() diff --git a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py index 4afe2f97b37..df97f5da233 100644 --- a/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py +++ b/libs/langchain_v1/langchain/agents/middleware/tool_call_limit.py @@ -204,7 +204,7 @@ class ToolCallLimitMiddleware(AgentMiddleware[ToolCallLimitState, Any]): Defaults to "end". Raises: - ValueError: If both limits are None or if exit_behavior is invalid. + ValueError: If both limits are `None` or if `exit_behavior` is invalid. """ super().__init__() diff --git a/libs/langchain_v1/langchain/agents/middleware/types.py b/libs/langchain_v1/langchain/agents/middleware/types.py index ecaa65b4db7..3b63da8e4f7 100644 --- a/libs/langchain_v1/langchain/agents/middleware/types.py +++ b/libs/langchain_v1/langchain/agents/middleware/types.py @@ -238,19 +238,19 @@ class AgentMiddleware(Generic[StateT, ContextT]): ) -> ModelCallResult: """Intercept and control model execution via handler callback. - The handler callback executes the model request and returns a ModelResponse. + The handler callback executes the model request and returns a `ModelResponse`. Middleware can call the handler multiple times for retry logic, skip calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer. Args: request: Model request to execute (includes state and runtime). - handler: Callback that executes the model request and returns ModelResponse. - Call this to execute the model. Can be called multiple times - for retry logic. Can skip calling it to short-circuit. + handler: Callback that executes the model request and returns + `ModelResponse`. Call this to execute the model. Can be called multiple + times for retry logic. Can skip calling it to short-circuit. Returns: - ModelCallResult + `ModelCallResult` Examples: Retry on error: @@ -321,16 +321,16 @@ class AgentMiddleware(Generic[StateT, ContextT]): ) -> ModelCallResult: """Intercept and control async model execution via handler callback. - The handler callback executes the model request and returns a ModelResponse. + The handler callback executes the model request and returns a `ModelResponse`. Middleware can call the handler multiple times for retry logic, skip calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer. Args: request: Model request to execute (includes state and runtime). - handler: Async callback that executes the model request and returns ModelResponse. - Call this to execute the model. Can be called multiple times - for retry logic. Can skip calling it to short-circuit. + handler: Async callback that executes the model request and returns + `ModelResponse`. Call this to execute the model. Can be called multiple + times for retry logic. Can skip calling it to short-circuit. Returns: ModelCallResult @@ -375,15 +375,15 @@ class AgentMiddleware(Generic[StateT, ContextT]): """Intercept tool execution for retries, monitoring, or modification. Multiple middleware compose automatically (first defined = outermost). - Exceptions propagate unless handle_tool_errors is configured on ToolNode. + Exceptions propagate unless `handle_tool_errors` is configured on `ToolNode`. Args: - request: Tool call request with call dict, BaseTool, state, and runtime. - Access state via request.state and runtime via request.runtime. + request: Tool call request with call `dict`, `BaseTool`, state, and runtime. + Access state via `request.state` and runtime via `request.runtime`. handler: Callable to execute the tool (can be called multiple times). Returns: - ToolMessage or Command (the final result). + `ToolMessage` or `Command` (the final result). The handler callable can be invoked multiple times for retry logic. Each call to handler is independent and stateless. @@ -391,12 +391,15 @@ class AgentMiddleware(Generic[StateT, ContextT]): Examples: Modify request before execution: + ```python def wrap_tool_call(self, request, handler): request.tool_call["args"]["value"] *= 2 return handler(request) + ``` Retry on error (call handler multiple times): + ```python def wrap_tool_call(self, request, handler): for attempt in range(3): try: @@ -407,9 +410,11 @@ class AgentMiddleware(Generic[StateT, ContextT]): if attempt == 2: raise return result + ``` Conditional retry based on response: + ```python def wrap_tool_call(self, request, handler): for attempt in range(3): result = handler(request) @@ -418,6 +423,7 @@ class AgentMiddleware(Generic[StateT, ContextT]): if attempt < 2: continue return result + ``` """ msg = ( "Synchronous implementation of wrap_tool_call is not available. " @@ -438,20 +444,20 @@ class AgentMiddleware(Generic[StateT, ContextT]): ) -> ToolMessage | Command: """Intercept and control async tool execution via handler callback. - The handler callback executes the tool call and returns a ToolMessage or Command. - Middleware can call the handler multiple times for retry logic, skip calling - it to short-circuit, or modify the request/response. Multiple middleware + The handler callback executes the tool call and returns a `ToolMessage` or + `Command`. Middleware can call the handler multiple times for retry logic, skip + calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer. Args: - request: Tool call request with call dict, BaseTool, state, and runtime. - Access state via request.state and runtime via request.runtime. - handler: Async callable to execute the tool and returns ToolMessage or Command. - Call this to execute the tool. Can be called multiple times - for retry logic. Can skip calling it to short-circuit. + request: Tool call request with call `dict`, `BaseTool`, state, and runtime. + Access state via `request.state` and runtime via `request.runtime`. + handler: Async callable to execute the tool and returns `ToolMessage` or + `Command`. Call this to execute the tool. Can be called multiple times + for retry logic. Can skip calling it to short-circuit. Returns: - ToolMessage or Command (the final result). + `ToolMessage` or `Command` (the final result). The handler callable can be invoked multiple times for retry logic. Each call to handler is independent and stateless. @@ -471,13 +477,14 @@ class AgentMiddleware(Generic[StateT, ContextT]): return result ``` - + ```python async def awrap_tool_call(self, request, handler): if cached := await get_cache_async(request): return ToolMessage(content=cached, tool_call_id=request.tool_call["id"]) result = await handler(request) await save_cache_async(request, result) return result + ``` """ msg = ( "Asynchronous implementation of awrap_tool_call is not available. " @@ -493,7 +500,7 @@ class AgentMiddleware(Generic[StateT, ContextT]): class _CallableWithStateAndRuntime(Protocol[StateT_contra, ContextT]): - """Callable with AgentState and Runtime as arguments.""" + """Callable with `AgentState` and `Runtime` as arguments.""" def __call__( self, state: StateT_contra, runtime: Runtime[ContextT] @@ -503,7 +510,7 @@ class _CallableWithStateAndRuntime(Protocol[StateT_contra, ContextT]): class _CallableReturningPromptString(Protocol[StateT_contra, ContextT]): # type: ignore[misc] - """Callable that returns a prompt string given ModelRequest (contains state and runtime).""" + """Callable that returns a prompt string given `ModelRequest` (contains state and runtime).""" def __call__(self, request: ModelRequest) -> str | Awaitable[str]: """Generate a system prompt string based on the request.""" @@ -513,7 +520,8 @@ class _CallableReturningPromptString(Protocol[StateT_contra, ContextT]): # type class _CallableReturningModelResponse(Protocol[StateT_contra, ContextT]): # type: ignore[misc] """Callable for model call interception with handler callback. - Receives handler callback to execute model and returns ModelResponse or AIMessage. + Receives handler callback to execute model and returns `ModelResponse` or + `AIMessage`. """ def __call__( @@ -528,7 +536,8 @@ class _CallableReturningModelResponse(Protocol[StateT_contra, ContextT]): # typ class _CallableReturningToolResponse(Protocol): """Callable for tool call interception with handler callback. - Receives handler callback to execute tool and returns final ToolMessage or Command. + Receives handler callback to execute tool and returns final `ToolMessage` or + `Command`. """ def __call__( @@ -621,22 +630,22 @@ def before_model( Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]] | AgentMiddleware[StateT, ContextT] ): - """Decorator used to dynamically create a middleware with the before_model hook. + """Decorator used to dynamically create a middleware with the `before_model` hook. Args: func: The function to be decorated. Must accept: `state: StateT, runtime: Runtime[ContextT]` - State and runtime context state_schema: Optional custom state schema type. If not provided, uses the default - AgentState schema. + `AgentState` schema. tools: Optional list of additional tools to register with this middleware. can_jump_to: Optional list of valid jump destinations for conditional edges. - Valid values are: "tools", "model", "end" + Valid values are: `"tools"`, `"model"`, `"end"` name: Optional name for the generated middleware class. If not provided, uses the decorated function's name. Returns: - Either an AgentMiddleware instance (if func is provided directly) or a decorator function - that can be applied to a function it is wrapping. + Either an `AgentMiddleware` instance (if func is provided directly) or a + decorator function that can be applied to a function it is wrapping. The decorated function should return: - `dict[str, Any]` - State updates to merge into the agent state @@ -763,22 +772,22 @@ def after_model( Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]] | AgentMiddleware[StateT, ContextT] ): - """Decorator used to dynamically create a middleware with the after_model hook. + """Decorator used to dynamically create a middleware with the `after_model` hook. Args: func: The function to be decorated. Must accept: `state: StateT, runtime: Runtime[ContextT]` - State and runtime context - state_schema: Optional custom state schema type. If not provided, uses the default - AgentState schema. + state_schema: Optional custom state schema type. If not provided, uses the + default `AgentState` schema. tools: Optional list of additional tools to register with this middleware. can_jump_to: Optional list of valid jump destinations for conditional edges. - Valid values are: "tools", "model", "end" + Valid values are: `"tools"`, `"model"`, `"end"` name: Optional name for the generated middleware class. If not provided, uses the decorated function's name. Returns: - Either an AgentMiddleware instance (if func is provided) or a decorator function - that can be applied to a function. + Either an `AgentMiddleware` instance (if func is provided) or a decorator + function that can be applied to a function. The decorated function should return: - `dict[str, Any]` - State updates to merge into the agent state @@ -894,22 +903,22 @@ def before_agent( Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]] | AgentMiddleware[StateT, ContextT] ): - """Decorator used to dynamically create a middleware with the before_agent hook. + """Decorator used to dynamically create a middleware with the `before_agent` hook. Args: func: The function to be decorated. Must accept: `state: StateT, runtime: Runtime[ContextT]` - State and runtime context - state_schema: Optional custom state schema type. If not provided, uses the default - AgentState schema. + state_schema: Optional custom state schema type. If not provided, uses the + default `AgentState` schema. tools: Optional list of additional tools to register with this middleware. can_jump_to: Optional list of valid jump destinations for conditional edges. - Valid values are: "tools", "model", "end" + Valid values are: `"tools"`, `"model"`, `"end"` name: Optional name for the generated middleware class. If not provided, uses the decorated function's name. Returns: - Either an AgentMiddleware instance (if func is provided directly) or a decorator function - that can be applied to a function it is wrapping. + Either an `AgentMiddleware` instance (if func is provided directly) or a + decorator function that can be applied to a function it is wrapping. The decorated function should return: - `dict[str, Any]` - State updates to merge into the agent state @@ -1036,22 +1045,22 @@ def after_agent( Callable[[_CallableWithStateAndRuntime[StateT, ContextT]], AgentMiddleware[StateT, ContextT]] | AgentMiddleware[StateT, ContextT] ): - """Decorator used to dynamically create a middleware with the after_agent hook. + """Decorator used to dynamically create a middleware with the `after_agent` hook. Args: func: The function to be decorated. Must accept: `state: StateT, runtime: Runtime[ContextT]` - State and runtime context - state_schema: Optional custom state schema type. If not provided, uses the default - AgentState schema. + state_schema: Optional custom state schema type. If not provided, uses the + default `AgentState` schema. tools: Optional list of additional tools to register with this middleware. can_jump_to: Optional list of valid jump destinations for conditional edges. - Valid values are: "tools", "model", "end" + Valid values are: `"tools"`, `"model"`, `"end"` name: Optional name for the generated middleware class. If not provided, uses the decorated function's name. Returns: - Either an AgentMiddleware instance (if func is provided) or a decorator function - that can be applied to a function. + Either an `AgentMiddleware` instance (if func is provided) or a decorator + function that can be applied to a function. The decorated function should return: - `dict[str, Any]` - State updates to merge into the agent state @@ -1300,21 +1309,21 @@ def wrap_model_call( ] | AgentMiddleware[StateT, ContextT] ): - """Create middleware with wrap_model_call hook from a function. + """Create middleware with `wrap_model_call` hook from a function. Converts a function with handler callback into middleware that can intercept model calls, implement retry logic, handle errors, and rewrite responses. Args: func: Function accepting (request, handler) that calls handler(request) - to execute the model and returns ModelResponse or AIMessage. + to execute the model and returns `ModelResponse` or `AIMessage`. Request contains state and runtime. - state_schema: Custom state schema. Defaults to AgentState. + state_schema: Custom state schema. Defaults to `AgentState`. tools: Additional tools to register with this middleware. name: Middleware class name. Defaults to function name. Returns: - AgentMiddleware instance if func provided, otherwise a decorator. + `AgentMiddleware` instance if func provided, otherwise a decorator. Examples: Basic retry logic: @@ -1448,20 +1457,20 @@ def wrap_tool_call( ] | AgentMiddleware ): - """Create middleware with wrap_tool_call hook from a function. + """Create middleware with `wrap_tool_call` hook from a function. Converts a function with handler callback into middleware that can intercept tool calls, implement retry logic, monitor execution, and modify responses. Args: func: Function accepting (request, handler) that calls - handler(request) to execute the tool and returns final ToolMessage or Command. - Can be sync or async. + handler(request) to execute the tool and returns final `ToolMessage` or + `Command`. Can be sync or async. tools: Additional tools to register with this middleware. name: Middleware class name. Defaults to function name. Returns: - AgentMiddleware instance if func provided, otherwise a decorator. + `AgentMiddleware` instance if func provided, otherwise a decorator. Examples: Retry logic: diff --git a/libs/langchain_v1/langchain/agents/structured_output.py b/libs/langchain_v1/langchain/agents/structured_output.py index d7824ecaf2a..0893566e5ef 100644 --- a/libs/langchain_v1/langchain/agents/structured_output.py +++ b/libs/langchain_v1/langchain/agents/structured_output.py @@ -186,7 +186,7 @@ class ToolStrategy(Generic[SchemaT]): handle_errors: ( bool | str | type[Exception] | tuple[type[Exception], ...] | Callable[[Exception], str] ) - """Error handling strategy for structured output via ToolStrategy. Default is True. + """Error handling strategy for structured output via ToolStrategy. - True: Catch all errors with default error template - str: Catch all errors with this custom message diff --git a/libs/langchain_v1/langchain/chat_models/__init__.py b/libs/langchain_v1/langchain/chat_models/__init__.py index 324c590865b..7d33e942910 100644 --- a/libs/langchain_v1/langchain/chat_models/__init__.py +++ b/libs/langchain_v1/langchain/chat_models/__init__.py @@ -1,4 +1,8 @@ -"""Chat models.""" +"""Entrypoint to using [chat models](https://docs.langchain.com/oss/python/langchain/models) in LangChain. + +See [the docs](https://docs.langchain.com/oss/python/langchain/models) for more +details. +""" # noqa: E501 from langchain_core.language_models import BaseChatModel diff --git a/libs/langchain_v1/langchain/chat_models/base.py b/libs/langchain_v1/langchain/chat_models/base.py index 28e92b0458f..2b3f8972e62 100644 --- a/libs/langchain_v1/langchain/chat_models/base.py +++ b/libs/langchain_v1/langchain/chat_models/base.py @@ -71,98 +71,102 @@ def init_chat_model( config_prefix: str | None = None, **kwargs: Any, ) -> BaseChatModel | _ConfigurableModel: - """Initialize a ChatModel from the model name and provider. + """Initialize a chat model in a single line using the model's name and provider. !!! note - Must have the integration package corresponding to the model provider - installed. + Requires the integration package for your model provider to be installed. + + See the `model_provider` parameter below for specific package names + (e.g., `pip install langchain-openai`). + + Refer to the [provider integration's API reference](https://docs.langchain.com/oss/python/integrations/providers) + for supported model parameters. Args: - model: The name of the model, e.g. "o3-mini", "claude-3-5-sonnet-latest". You can + model: The name of the model, e.g. `'o3-mini'`, `'claude-sonnet-4-5-20250929'`. You can also specify model and model provider in a single argument using - '{model_provider}:{model}' format, e.g. "openai:o1". + `'{model_provider}:{model}'` format, e.g. `'openai:o1'`. model_provider: The model provider if not specified as part of model arg (see above). Supported model_provider values and the corresponding integration package are: - - 'openai' -> langchain-openai - - 'anthropic' -> langchain-anthropic - - 'azure_openai' -> langchain-openai - - 'azure_ai' -> langchain-azure-ai - - 'google_vertexai' -> langchain-google-vertexai - - 'google_genai' -> langchain-google-genai - - 'bedrock' -> langchain-aws - - 'bedrock_converse' -> langchain-aws - - 'cohere' -> langchain-cohere - - 'fireworks' -> langchain-fireworks - - 'together' -> langchain-together - - 'mistralai' -> langchain-mistralai - - 'huggingface' -> langchain-huggingface - - 'groq' -> langchain-groq - - 'ollama' -> langchain-ollama - - 'google_anthropic_vertex' -> langchain-google-vertexai - - 'deepseek' -> langchain-deepseek - - 'ibm' -> langchain-ibm - - 'nvidia' -> langchain-nvidia-ai-endpoints - - 'xai' -> langchain-xai - - 'perplexity' -> langchain-perplexity + - `openai` -> `langchain-openai` + - `anthropic` -> `langchain-anthropic` + - `azure_openai` -> `langchain-openai` + - `azure_ai` -> `langchain-azure-ai` + - `google_vertexai` -> `langchain-google-vertexai` + - `google_genai` -> `langchain-google-genai` + - `bedrock` -> `langchain-aws` + - `bedrock_converse` -> `langchain-aws` + - `cohere` -> `langchain-cohere` + - `fireworks` -> `langchain-fireworks` + - `together` -> `langchain-together` + - `mistralai` -> `langchain-mistralai` + - `huggingface` -> `langchain-huggingface` + - `groq` -> `langchain-groq` + - `ollama` -> `langchain-ollama` + - `google_anthropic_vertex` -> `langchain-google-vertexai` + - `deepseek` -> `langchain-deepseek` + - `ibm` -> `langchain-ibm` + - `nvidia` -> `langchain-nvidia-ai-endpoints` + - `xai` -> `langchain-xai` + - `perplexity` -> `langchain-perplexity` - Will attempt to infer model_provider from model if not specified. The + Will attempt to infer `model_provider` from model if not specified. The following providers will be inferred based on these model prefixes: - - 'gpt-...' | 'o1...' | 'o3...' -> 'openai' - - 'claude...' -> 'anthropic' - - 'amazon....' -> 'bedrock' - - 'gemini...' -> 'google_vertexai' - - 'command...' -> 'cohere' - - 'accounts/fireworks...' -> 'fireworks' - - 'mistral...' -> 'mistralai' - - 'deepseek...' -> 'deepseek' - - 'grok...' -> 'xai' - - 'sonar...' -> 'perplexity' - configurable_fields: Which model parameters are - configurable: + - `gpt-...` | `o1...` | `o3...` -> `openai` + - `claude...` -> `anthropic` + - `amazon...` -> `bedrock` + - `gemini...` -> `google_vertexai` + - `command...` -> `cohere` + - `accounts/fireworks...` -> `fireworks` + - `mistral...` -> `mistralai` + - `deepseek...` -> `deepseek` + - `grok...` -> `xai` + - `sonar...` -> `perplexity` + configurable_fields: Which model parameters are configurable: - - None: No configurable fields. - - "any": All fields are configurable. *See Security Note below.* - - Union[List[str], Tuple[str, ...]]: Specified fields are configurable. + - `None`: No configurable fields. + - `'any'`: All fields are configurable. **See Security Note below.** + - `list[str] | Tuple[str, ...]`: Specified fields are configurable. - Fields are assumed to have config_prefix stripped if there is a - config_prefix. If model is specified, then defaults to None. If model is + Fields are assumed to have `config_prefix` stripped if there is a + `config_prefix`. If model is specified, then defaults to `None`. If model is not specified, then defaults to `("model", "model_provider")`. **Security Note**: Setting `configurable_fields="any"` means fields like - api_key, base_url, etc. can be altered at runtime, potentially redirecting + `api_key`, `base_url`, etc. can be altered at runtime, potentially redirecting model requests to a different service/user. Make sure that if you're accepting untrusted configurations that you enumerate the `configurable_fields=(...)` explicitly. - config_prefix: If config_prefix is a non-empty string then model will be + config_prefix: If `'config_prefix'` is a non-empty string then model will be configurable at runtime via the `config["configurable"]["{config_prefix}_{param}"]` keys. If - config_prefix is an empty string then model will be configurable via + `'config_prefix'` is an empty string then model will be configurable via `config["configurable"]["{param}"]`. + temperature: Model temperature. + max_tokens: Max output tokens. + timeout: The maximum time (in seconds) to wait for a response from the model + before canceling the request. + max_retries: The maximum number of attempts the system will make to resend a + request if it fails due to issues like network timeouts or rate limits. + base_url: The URL of the API endpoint where requests are sent. + rate_limiter: A `BaseRateLimiter` to space out requests to avoid exceeding + rate limits. kwargs: Additional model-specific keyword args to pass to - `<>.__init__(model=model_name, **kwargs)`. Examples - include: - * temperature: Model temperature. - * max_tokens: Max output tokens. - * timeout: The maximum time (in seconds) to wait for a response from the model - before canceling the request. - * max_retries: The maximum number of attempts the system will make to resend a - request if it fails due to issues like network timeouts or rate limits. - * base_url: The URL of the API endpoint where requests are sent. - * rate_limiter: A `BaseRateLimiter` to space out requests to avoid exceeding - rate limits. + `<>.__init__(model=model_name, **kwargs)`. Returns: - A BaseChatModel corresponding to the model_name and model_provider specified if - configurability is inferred to be False. If configurable, a chat model emulator - that initializes the underlying model at runtime once a config is passed in. + A `BaseChatModel` corresponding to the `model_name` and `model_provider` + specified if configurability is inferred to be `False`. If configurable, a + chat model emulator that initializes the underlying model at runtime once a + config is passed in. Raises: - ValueError: If model_provider cannot be inferred or isn't supported. - ImportError: If the model provider integration package is not installed. + `ValueError`: If `model_provider` cannot be inferred or isn't supported. + `ImportError`: If the model provider integration package is not installed. ???+ note "Init non-configurable model" @@ -171,7 +175,7 @@ def init_chat_model( from langchain.chat_models import init_chat_model o3_mini = init_chat_model("openai:o3-mini", temperature=0) - claude_sonnet = init_chat_model("anthropic:claude-3-5-sonnet-latest", temperature=0) + claude_sonnet = init_chat_model("anthropic:claude-sonnet-4-5-20250929", temperature=0) gemini_2_flash = init_chat_model("google_vertexai:gemini-2.5-flash", temperature=0) o3_mini.invoke("what's your name") @@ -192,9 +196,9 @@ def init_chat_model( # GPT-4o response configurable_model.invoke( - "what's your name", config={"configurable": {"model": "claude-3-5-sonnet-latest"}} + "what's your name", + config={"configurable": {"model": "claude-sonnet-4-5-20250929"}}, ) - # claude-3.5 sonnet response ``` ??? note "Fully configurable model with a default" @@ -205,7 +209,7 @@ def init_chat_model( configurable_model_with_default = init_chat_model( "openai:gpt-4o", - configurable_fields="any", # this allows us to configure other params like temperature, max_tokens, etc at runtime. + configurable_fields="any", # This allows us to configure other params like temperature, max_tokens, etc at runtime. config_prefix="foo", temperature=0, ) @@ -217,17 +221,16 @@ def init_chat_model( "what's your name", config={ "configurable": { - "foo_model": "anthropic:claude-3-5-sonnet-latest", + "foo_model": "anthropic:claude-sonnet-4-5-20250929", "foo_temperature": 0.6, } }, ) - # Claude-3.5 sonnet response with temperature 0.6 ``` ??? note "Bind tools to a configurable model" - You can call any ChatModel declarative methods on a configurable model in the + You can call any chat model declarative methods on a configurable model in the same way that you would with a normal model. ```python @@ -252,39 +255,22 @@ def init_chat_model( "gpt-4o", configurable_fields=("model", "model_provider"), temperature=0 ) - configurable_model_with_tools = configurable_model.bind_tools([GetWeather, GetPopulation]) + configurable_model_with_tools = configurable_model.bind_tools( + [ + GetWeather, + GetPopulation, + ] + ) configurable_model_with_tools.invoke( "Which city is hotter today and which is bigger: LA or NY?" ) - # GPT-4o response with tool calls configurable_model_with_tools.invoke( "Which city is hotter today and which is bigger: LA or NY?", - config={"configurable": {"model": "claude-3-5-sonnet-latest"}}, + config={"configurable": {"model": "claude-sonnet-4-5-20250929"}}, ) - # Claude-3.5 sonnet response with tools ``` - !!! version-added "Added in version 0.2.7" - - !!! warning "Behavior changed in 0.2.8" - Support for `configurable_fields` and `config_prefix` added. - - !!! warning "Behavior changed in 0.2.12" - Support for Ollama via langchain-ollama package added - (langchain_ollama.ChatOllama). Previously, - the now-deprecated langchain-community version of Ollama was imported - (langchain_community.chat_models.ChatOllama). - - Support for AWS Bedrock models via the Converse API added - (model_provider="bedrock_converse"). - - !!! warning "Behavior changed in 0.3.5" - Out of beta. - - !!! warning "Behavior changed in 0.3.19" - Support for Deepseek, IBM, Nvidia, and xAI models added. - """ # noqa: E501 if not model and not configurable_fields: configurable_fields = ("model", "model_provider") diff --git a/libs/langchain_v1/langchain/embeddings/base.py b/libs/langchain_v1/langchain/embeddings/base.py index 5a5ed45712a..08e4e6a5381 100644 --- a/libs/langchain_v1/langchain/embeddings/base.py +++ b/libs/langchain_v1/langchain/embeddings/base.py @@ -134,7 +134,7 @@ def init_embeddings( Args: model: Name of the model to use. Can be either: - - A model string like "openai:text-embedding-3-small" + - A model string like `"openai:text-embedding-3-small"` - Just the model name if provider is specified provider: Optional explicit provider name. If not specified, will attempt to parse from the model string. Supported providers diff --git a/libs/langchain_v1/langchain/tools/tool_node.py b/libs/langchain_v1/langchain/tools/tool_node.py index 849c012f427..1d79c490e48 100644 --- a/libs/langchain_v1/langchain/tools/tool_node.py +++ b/libs/langchain_v1/langchain/tools/tool_node.py @@ -183,12 +183,15 @@ Examples: Modify request before execution: + ```python def handler(request, execute): request.tool_call["args"]["value"] *= 2 return execute(request) + ``` Retry on error (execute multiple times): + ```python def handler(request, execute): for attempt in range(3): try: @@ -199,9 +202,11 @@ Examples: if attempt == 2: raise return result + ``` Conditional retry based on response: + ```python def handler(request, execute): for attempt in range(3): result = execute(request) @@ -210,15 +215,18 @@ Examples: if attempt < 2: continue return result + ``` Cache/short-circuit without calling execute: + ```python def handler(request, execute): if cached := get_cache(request): return ToolMessage(content=cached, tool_call_id=request.tool_call["id"]) result = execute(request) save_cache(request, result) return result + ``` """ AsyncToolCallWrapper = Callable[ @@ -1294,7 +1302,7 @@ class InjectedState(InjectedToolArg): """Annotation for injecting graph state into tool arguments. This annotation enables tools to access graph state without exposing state - management details to the language model. Tools annotated with InjectedState + management details to the language model. Tools annotated with `InjectedState` receive state data automatically during execution while remaining invisible to the model's tool-calling interface. @@ -1352,9 +1360,9 @@ class InjectedState(InjectedToolArg): ``` Note: - - InjectedState arguments are automatically excluded from tool schemas + - `InjectedState` arguments are automatically excluded from tool schemas presented to language models - - ToolNode handles the injection process during execution + - `ToolNode` handles the injection process during execution - Tools can mix regular arguments (controlled by the model) with injected arguments (controlled by the system) - State injection occurs after the model generates tool calls but before @@ -1362,7 +1370,7 @@ class InjectedState(InjectedToolArg): """ def __init__(self, field: str | None = None) -> None: - """Initialize the InjectedState annotation.""" + """Initialize the `InjectedState` annotation.""" self.field = field @@ -1407,7 +1415,7 @@ class InjectedStore(InjectedToolArg): return result.value if result else "Not found" ``` - Usage with ToolNode and graph compilation: + Usage with `ToolNode` and graph compilation: ```python from langgraph.graph import StateGraph @@ -1432,9 +1440,9 @@ class InjectedStore(InjectedToolArg): ``` Note: - - InjectedStore arguments are automatically excluded from tool schemas + - `InjectedStore` arguments are automatically excluded from tool schemas presented to language models - - The store instance is automatically injected by ToolNode during execution + - The store instance is automatically injected by `ToolNode` during execution - Tools can access namespaced storage using the store's get/put methods - Store injection requires the graph to be compiled with a store instance - Multiple tools can share the same store instance for data consistency diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_handler_composition.py b/libs/langchain_v1/tests/unit_tests/agents/test_handler_composition.py index 65eac871313..efecfcd2023 100644 --- a/libs/langchain_v1/tests/unit_tests/agents/test_handler_composition.py +++ b/libs/langchain_v1/tests/unit_tests/agents/test_handler_composition.py @@ -11,7 +11,7 @@ from langgraph.runtime import Runtime def create_test_request(**kwargs): - """Helper to create a ModelRequest with sensible defaults.""" + """Helper to create a `ModelRequest` with sensible defaults.""" defaults = { "messages": [], @@ -28,7 +28,7 @@ def create_test_request(**kwargs): def create_mock_base_handler(content="test"): - """Helper to create a base handler that returns ModelResponse.""" + """Helper to create a base handler that returns `ModelResponse`.""" def mock_base_handler(req): return ModelResponse(result=[AIMessage(content=content)], structured_response=None) @@ -37,7 +37,7 @@ def create_mock_base_handler(content="test"): class TestChainModelCallHandlers: - """Test the _chain_model_call_handlers composition function.""" + """Test the `_chain_model_call_handlers` composition function.""" def test_empty_handlers_returns_none(self) -> None: """Test that empty handlers list returns None.""" diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index ee8aba6225f..bfcb3ba6dc4 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -1530,7 +1530,7 @@ class ChatAnthropic(BaseChatModel): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "chat_models", "anthropic"] @property diff --git a/libs/partners/anthropic/langchain_anthropic/output_parsers.py b/libs/partners/anthropic/langchain_anthropic/output_parsers.py index 20e60fb64cb..57e2121c5e5 100644 --- a/libs/partners/anthropic/langchain_anthropic/output_parsers.py +++ b/libs/partners/anthropic/langchain_anthropic/output_parsers.py @@ -29,7 +29,7 @@ class ToolsOutputParser(BaseGenerationOutputParser): """Parse a list of candidate model Generations into a specific format. Args: - result: A list of Generations to be parsed. The Generations are assumed + result: A list of `Generation` to be parsed. The Generations are assumed to be different candidate outputs for a single model input. partial: (Not used) Whether the result is a partial result. If `True`, the parser may return a partial result, which may not be complete or valid. diff --git a/libs/partners/anthropic/tests/integration_tests/test_standard.py b/libs/partners/anthropic/tests/integration_tests/test_standard.py index c9c0492e123..59dec97bf81 100644 --- a/libs/partners/anthropic/tests/integration_tests/test_standard.py +++ b/libs/partners/anthropic/tests/integration_tests/test_standard.py @@ -15,7 +15,7 @@ MODEL = "claude-3-5-haiku-latest" class TestAnthropicStandard(ChatModelIntegrationTests): - """Use the standard ChatModel integration tests against the ChatAnthropic class.""" + """Use standard chat model integration tests against the `ChatAnthropic` class.""" @property def chat_model_class(self) -> type[BaseChatModel]: diff --git a/libs/partners/anthropic/tests/unit_tests/test_standard.py b/libs/partners/anthropic/tests/unit_tests/test_standard.py index d8cdafc159f..b255cb60d29 100644 --- a/libs/partners/anthropic/tests/unit_tests/test_standard.py +++ b/libs/partners/anthropic/tests/unit_tests/test_standard.py @@ -9,7 +9,7 @@ from langchain_anthropic import ChatAnthropic class TestAnthropicStandard(ChatModelUnitTests): - """Use the standard ChatModel unit tests against the ChatAnthropic class.""" + """Use the standard chat model unit tests against the `ChatAnthropic` class.""" @property def chat_model_class(self) -> type[BaseChatModel]: diff --git a/libs/partners/chroma/langchain_chroma/vectorstores.py b/libs/partners/chroma/langchain_chroma/vectorstores.py index 0ec6f21631e..99189ff7c82 100644 --- a/libs/partners/chroma/langchain_chroma/vectorstores.py +++ b/libs/partners/chroma/langchain_chroma/vectorstores.py @@ -110,11 +110,10 @@ def maximal_marginal_relevance( Args: query_embedding: Query embedding. embedding_list: List of embeddings to select from. - lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. - k: Number of Documents to return. Defaults to 4. + lambda_mult: Number between `0` and `1` that determines the degree + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. + k: Number of Documents to return. Returns: List of indices of embeddings selected by maximal marginal relevance. @@ -455,7 +454,7 @@ class Chroma(VectorStore): Args: query_texts: List of query texts. query_embeddings: List of query embeddings. - n_results: Number of results to return. Defaults to 4. + n_results: Number of results to return. where: dict used to filter results by metadata. E.g. {"color" : "red"}. where_document: dict used to filter by the document contents. @@ -686,7 +685,7 @@ class Chroma(VectorStore): Args: query: Query text to search for. - k: Number of results to return. Defaults to 4. + k: Number of results to return. filter: Filter by metadata. kwargs: Additional keyword arguments to pass to Chroma collection query. @@ -713,7 +712,7 @@ class Chroma(VectorStore): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. where_document: dict used to filter by the document contents. E.g. {"$contains": "hello"}. @@ -743,7 +742,7 @@ class Chroma(VectorStore): Args: embedding (List[float]): Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. where_document: dict used to filter by the documents. E.g. {"$contains": "hello"}. @@ -774,7 +773,7 @@ class Chroma(VectorStore): Args: query: Query text to search for. - k: Number of results to return. Defaults to 4. + k: Number of results to return. filter: Filter by metadata. where_document: dict used to filter by document contents. E.g. {"$contains": "hello"}. @@ -816,7 +815,7 @@ class Chroma(VectorStore): Args: query: Query text to search for. - k: Number of results to return. Defaults to 4. + k: Number of results to return. filter: Filter by metadata. where_document: dict used to filter by the document contents. E.g. {"$contains": "hello"}. @@ -988,16 +987,14 @@ class Chroma(VectorStore): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. - fetch_k: Number of Documents to fetch to pass to MMR algorithm. Defaults to - 20. + k: Number of `Document` objects to return. + fetch_k: Number of `Document` objects to fetch to pass to MMR algorithm. lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. where_document: dict used to filter by the document contents. - E.g. {"$contains": "hello"}. + e.g. `{"$contains": "hello"}`. kwargs: Additional keyword arguments to pass to Chroma collection query. Returns: @@ -1039,15 +1036,14 @@ class Chroma(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + lambda_mult: Number between `0` and `1` that determines the degree + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. where_document: dict used to filter by the document contents. - E.g. {"$contains": "hello"}. + e.g. `{"$contains": "hello"}`. kwargs: Additional keyword arguments to pass to Chroma collection query. Returns: @@ -1354,7 +1350,6 @@ class Chroma(VectorStore): host: Hostname of a deployed Chroma server. port: Connection port for a deployed Chroma server. Default is 8000. ssl: Whether to establish an SSL connection with a deployed Chroma server. - Default is False. headers: HTTP headers to send to a deployed Chroma server. chroma_cloud_api_key: Chroma Cloud API key. tenant: Tenant ID. Required for Chroma Cloud connections. diff --git a/libs/partners/mistralai/langchain_mistralai/chat_models.py b/libs/partners/mistralai/langchain_mistralai/chat_models.py index c9ca6c02ca5..f31f96c2427 100644 --- a/libs/partners/mistralai/langchain_mistralai/chat_models.py +++ b/libs/partners/mistralai/langchain_mistralai/chat_models.py @@ -1133,7 +1133,7 @@ class ChatMistralAI(BaseChatModel): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "chat_models", "mistralai"] diff --git a/libs/partners/nomic/langchain_nomic/embeddings.py b/libs/partners/nomic/langchain_nomic/embeddings.py index 5e1253c1e92..1bcc6ce00e1 100644 --- a/libs/partners/nomic/langchain_nomic/embeddings.py +++ b/libs/partners/nomic/langchain_nomic/embeddings.py @@ -72,7 +72,7 @@ class NomicEmbeddings(Embeddings): dimensionality: The embedding dimension, for use with Matryoshka-capable models. Defaults to full-size. inference_mode: How to generate embeddings. One of `'remote'`, `'local'` - (Embed4All), or `'dynamic'` (automatic). Defaults to `'remote'`. + (Embed4All), or `'dynamic'` (automatic). device: The device to use for local embeddings. Choices include `'cpu'`, `'gpu'`, `'nvidia'`, `'amd'`, or a specific device name. See the docstring for `GPT4All.__init__` for more info. diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py index 215a4b691d8..60d663a06b3 100644 --- a/libs/partners/openai/langchain_openai/chat_models/azure.py +++ b/libs/partners/openai/langchain_openai/chat_models/azure.py @@ -572,7 +572,7 @@ class AzureChatOpenAI(BaseChatOpenAI): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "chat_models", "azure_openai"] @property diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 20bbeac10f5..8e8a3569c5b 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -630,8 +630,9 @@ class BaseChatOpenAI(BaseChatModel): """ store: bool | None = None - """If `True`, OpenAI may store response data for future use. Defaults to `True` - for the Responses API and `False` for the Chat Completions API. + """If `True`, OpenAI may store response data for future use. + + Defaults to `True` for the Responses API and `False` for the Chat Completions API. !!! version-added "Added in version 0.3.24" """ @@ -2745,7 +2746,7 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override] @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "chat_models", "openai"] @property diff --git a/libs/partners/openai/langchain_openai/embeddings/base.py b/libs/partners/openai/langchain_openai/embeddings/base.py index 087bfd71b99..5f92002ad72 100644 --- a/libs/partners/openai/langchain_openai/embeddings/base.py +++ b/libs/partners/openai/langchain_openai/embeddings/base.py @@ -230,8 +230,7 @@ class OpenAIEmbeddings(BaseModel, Embeddings): model_kwargs: dict[str, Any] = Field(default_factory=dict) """Holds any model parameters valid for `create` call not explicitly specified.""" skip_empty: bool = False - """Whether to skip empty strings when embedding or raise an error. - Defaults to not skipping.""" + """Whether to skip empty strings when embedding or raise an error.""" default_headers: Mapping[str, str] | None = None default_query: Mapping[str, object] | None = None # Configure a custom httpx client. See the diff --git a/libs/partners/openai/langchain_openai/llms/azure.py b/libs/partners/openai/langchain_openai/llms/azure.py index dc63d32f8e7..cad149c506f 100644 --- a/libs/partners/openai/langchain_openai/llms/azure.py +++ b/libs/partners/openai/langchain_openai/llms/azure.py @@ -97,7 +97,7 @@ class AzureOpenAI(BaseOpenAI): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "llms", "openai"] @property diff --git a/libs/partners/openai/langchain_openai/llms/base.py b/libs/partners/openai/langchain_openai/llms/base.py index b835526552b..bdffd5eff7b 100644 --- a/libs/partners/openai/langchain_openai/llms/base.py +++ b/libs/partners/openai/langchain_openai/llms/base.py @@ -793,7 +793,7 @@ class OpenAI(BaseOpenAI): @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain", "llms", "openai"] @classmethod diff --git a/libs/partners/perplexity/langchain_perplexity/chat_models.py b/libs/partners/perplexity/langchain_perplexity/chat_models.py index 7566792fe9e..c5e25f3eeed 100644 --- a/libs/partners/perplexity/langchain_perplexity/chat_models.py +++ b/libs/partners/perplexity/langchain_perplexity/chat_models.py @@ -75,7 +75,7 @@ class ChatPerplexity(BaseChatModel): model: str Name of the model to use. e.g. "sonar" temperature: float - Sampling temperature to use. Default is 0.7 + Sampling temperature to use. max_tokens: int | None Maximum number of tokens to generate. streaming: bool @@ -83,9 +83,9 @@ class ChatPerplexity(BaseChatModel): Key init args - client params: pplx_api_key: str | None - API key for PerplexityChat API. Default is None. + API key for PerplexityChat API. request_timeout: float | Tuple[float, float] | None - Timeout for requests to PerplexityChat completion API. Default is None. + Timeout for requests to PerplexityChat completion API. max_retries: int Maximum number of retries to make when generating. @@ -159,7 +159,7 @@ class ChatPerplexity(BaseChatModel): """Base URL path for API requests, leave blank if not using a proxy or service emulator.""" request_timeout: float | tuple[float, float] | None = Field(None, alias="timeout") - """Timeout for requests to PerplexityChat completion API. Default is None.""" + """Timeout for requests to PerplexityChat completion API.""" max_retries: int = 6 """Maximum number of retries to make when generating.""" streaming: bool = False diff --git a/libs/partners/prompty/langchain_prompty/utils.py b/libs/partners/prompty/langchain_prompty/utils.py index 19c4dd837ec..e6d384eec72 100644 --- a/libs/partners/prompty/langchain_prompty/utils.py +++ b/libs/partners/prompty/langchain_prompty/utils.py @@ -225,19 +225,19 @@ def execute( raw: bool = False, connection: str = "default", ) -> Any: - """Execute a prompty. + """Execute a `Prompty`. Args: prompt: The prompt to execute. - Can be a path to a prompty file or a Prompty object. - configuration: The configuration to use. Defaults to `{}`. - parameters: The parameters to use. Defaults to `{}`. - inputs: The inputs to the prompty. Defaults to `{}`. - raw: Whether to return the raw output. Defaults to `False`. - connection: The connection to use. Defaults to `'default'`. + Can be a path to a prompty file or a `Prompty` object. + configuration: The configuration to use. + parameters: The parameters to use. + inputs: The inputs to the `Prompty`. + raw: Whether to return the raw output. + connection: The connection to use. Returns: - The result of executing the prompty. + The result of executing the `Prompty`. """ if isinstance(prompt, str): diff --git a/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py b/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py index 4130f9074d5..d94c39e1757 100644 --- a/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py +++ b/libs/partners/qdrant/langchain_qdrant/fastembed_sparse.py @@ -28,22 +28,22 @@ class FastEmbedSparse(SparseEmbeddings): For a list of available models, see [the Qdrant docs](https://qdrant.github.io/fastembed/examples/Supported_Models/). Args: - model_name (str): The name of the model to use. Defaults to `"Qdrant/bm25"`. - batch_size (int): Batch size for encoding. Defaults to 256. + model_name (str): The name of the model to use. + batch_size (int): Batch size for encoding. cache_dir (str, optional): The path to the model cache directory.\ - Can also be set using the\ - `FASTEMBED_CACHE_PATH` env variable. + Can also be set using the\ + `FASTEMBED_CACHE_PATH` env variable. threads (int, optional): The number of threads onnxruntime session can use. providers (Sequence[Any], optional): List of ONNX execution providers.\ parallel (int, optional): If `>1`, data-parallel encoding will be used, r\ - Recommended for encoding of large datasets.\ - If `0`, use all available cores.\ - If `None`, don't use data-parallel processing,\ - use default onnxruntime threading instead.\ + Recommended for encoding of large datasets.\ + If `0`, use all available cores.\ + If `None`, don't use data-parallel processing,\ + use default onnxruntime threading instead.\ - kwargs: Additional options to pass to fastembed.SparseTextEmbedding + kwargs: Additional options to pass to `fastembed.SparseTextEmbedding` Raises: - ValueError: If the model_name is not supported in SparseTextEmbedding. + ValueError: If the `model_name` is not supported in `SparseTextEmbedding`. """ try: diff --git a/libs/partners/qdrant/langchain_qdrant/vectorstores.py b/libs/partners/qdrant/langchain_qdrant/vectorstores.py index 3e31309f056..f7e3eb5e735 100644 --- a/libs/partners/qdrant/langchain_qdrant/vectorstores.py +++ b/libs/partners/qdrant/langchain_qdrant/vectorstores.py @@ -237,7 +237,7 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -292,7 +292,7 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. **kwargs: Additional keyword arguments. @@ -318,7 +318,7 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -376,7 +376,7 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -435,7 +435,7 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -494,7 +494,7 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -553,7 +553,7 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -642,7 +642,7 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. filter: Filter by metadata. search_params: Additional search params offset: @@ -740,7 +740,7 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity @@ -804,12 +804,11 @@ class Qdrant(VectorStore): Args: query: Text to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + lambda_mult: Number between `0` and `1` that determines the degree + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. search_params: Additional search params score_threshold: @@ -822,19 +821,20 @@ class Qdrant(VectorStore): Read consistency of the search. Defines how many replicas should be queried before returning the result. Values: - - int - number of replicas to query, values should present in all + - `int` - number of replicas to query, values should present in all queried replicas - - 'majority' - query all replicas, but return values present in the + - `'majority'` - query all replicas, but return values present in the majority of replicas - - 'quorum' - query the majority of replicas, return values present in + - `'quorum'` - query the majority of replicas, return values present in all of them - - 'all' - query all replicas, and return values present in all replicas + - `'all'` - query all replicas, and return values present in all + replicas **kwargs: Any other named arguments to pass through to - AsyncQdrantClient.Search(). + `AsyncQdrantClient.Search()`. Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ query_embedding = await self._aembed_query(query) @@ -869,12 +869,11 @@ class Qdrant(VectorStore): Args: embedding: Embedding to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + lambda_mult: Number between `0` and `1` that determines the degree + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. search_params: Additional search params score_threshold: @@ -882,23 +881,24 @@ class Qdrant(VectorStore): If defined, less similar results will not be returned. Score of the returned result might be higher or smaller than the threshold depending on the Distance function used. - E.g. for cosine similarity only higher scores will be returned. + e.g. for cosine similarity only higher scores will be returned. consistency: Read consistency of the search. Defines how many replicas should be queried before returning the result. Values: - - int - number of replicas to query, values should present in all + - `int` - number of replicas to query, values should present in all queried replicas - - 'majority' - query all replicas, but return values present in the + - `'majority'` - query all replicas, but return values present in the majority of replicas - - 'quorum' - query the majority of replicas, return values present in + - `'quorum'` - query the majority of replicas, return values present in all of them - - 'all' - query all replicas, and return values present in all replicas + - `'all'` - query all replicas, and return values present in all + replicas **kwargs: - Any other named arguments to pass through to QdrantClient.search() + Any other named arguments to pass through to `QdrantClient.search()` Returns: - List of Documents selected by maximal marginal relevance. + List of `Document` objects selected by maximal marginal relevance. """ results = self.max_marginal_relevance_search_with_score_by_vector( @@ -934,12 +934,11 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. - fetch_k: Number of Documents to fetch to pass to MMR algorithm. - lambda_mult: Number between 0 and 1 that determines the degree - of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + k: Number of `Document` objects to return. + fetch_k: Number of `Document` to fetch to pass to MMR algorithm. + lambda_mult: Number between `0` and `1` that determines the degree + of diversity among the results with `0` corresponding + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. search_params: Additional search params score_threshold: @@ -952,20 +951,21 @@ class Qdrant(VectorStore): Read consistency of the search. Defines how many replicas should be queried before returning the result. Values: - - int - number of replicas to query, values should present in all + - `int` - number of replicas to query, values should present in all queried replicas - - 'majority' - query all replicas, but return values present in the - majority of replicas - - 'quorum' - query the majority of replicas, return values present in - all of them - - 'all' - query all replicas, and return values present in all replicas + - `'majority'` - query all replicas, but return values present in the + majority of replicas + - `'quorum'` - query the majority of replicas, return values present in + all of them + - `'all'` - query all replicas, and return values present in all + replicas **kwargs: Any other named arguments to pass through to - AsyncQdrantClient.Search(). + `AsyncQdrantClient.Search()`. Returns: - List of Documents selected by maximal marginal relevance and distance for - each. + List of `Document` objects selected by maximal marginal relevance and + distance for each. """ results = await self.amax_marginal_relevance_search_with_score_by_vector( @@ -1000,13 +1000,11 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Defaults to 20. - lambda_mult: Number between 0 and 1 that determines the degree + lambda_mult: Number between `0` and `1` that determines the degree of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. search_params: Additional search params score_threshold: @@ -1092,13 +1090,11 @@ class Qdrant(VectorStore): Args: embedding: Embedding vector to look up documents similar to. - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. fetch_k: Number of Documents to fetch to pass to MMR algorithm. - Defaults to 20. - lambda_mult: Number between 0 and 1 that determines the degree + lambda_mult: Number between `0` and `1` that determines the degree of diversity among the results with 0 corresponding - to maximum diversity and 1 to minimum diversity. - Defaults to 0.5. + to maximum diversity and `1` to minimum diversity. filter: Filter by metadata. search_params: Additional search params. score_threshold: Define a minimal score threshold for the result. @@ -1983,7 +1979,7 @@ class Qdrant(VectorStore): Args: query: input text - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs @@ -2007,7 +2003,7 @@ class Qdrant(VectorStore): Args: query: input text - k: Number of Documents to return. Defaults to 4. + k: Number of Documents to return. **kwargs: kwargs to be passed to similarity search. Should include: score_threshold: Optional, a floating point value between 0 to 1 to filter the resulting set of retrieved docs diff --git a/libs/partners/xai/langchain_xai/chat_models.py b/libs/partners/xai/langchain_xai/chat_models.py index 5bfd2fb62d7..a3e0bdbaddb 100644 --- a/libs/partners/xai/langchain_xai/chat_models.py +++ b/libs/partners/xai/langchain_xai/chat_models.py @@ -426,7 +426,7 @@ class ChatXAI(BaseChatOpenAI): # type: ignore[override] @classmethod def get_lc_namespace(cls) -> list[str]: - """Get the namespace of the langchain object.""" + """Get the namespace of the LangChain object.""" return ["langchain_xai", "chat_models"] @property diff --git a/libs/standard-tests/README.md b/libs/standard-tests/README.md index 6b821eb62f8..c24b6dc1f91 100644 --- a/libs/standard-tests/README.md +++ b/libs/standard-tests/README.md @@ -37,7 +37,7 @@ For detailed information on how to contribute, see the [Contributing Guide](http ## Usage -To add standard tests to an integration package (e.g., for a ChatModel), you need to create +To add standard tests to an integration package (e.g., for a chat model), you need to create 1. A unit test class that inherits from `ChatModelUnitTests` 2. An integration test class that inherits from `ChatModelIntegrationTests` diff --git a/libs/standard-tests/langchain_tests/integration_tests/chat_models.py b/libs/standard-tests/langchain_tests/integration_tests/chat_models.py index c897616c602..c9fc99d07ca 100644 --- a/libs/standard-tests/langchain_tests/integration_tests/chat_models.py +++ b/libs/standard-tests/langchain_tests/integration_tests/chat_models.py @@ -301,6 +301,7 @@ class ChatModelIntegrationTests(ChatModelTests): ??? note "`supports_image_inputs`" Boolean property indicating whether the chat model supports image inputs. + Defaults to `False`. If set to `True`, the chat model will be tested by inputting an @@ -334,7 +335,9 @@ class ChatModelIntegrationTests(ChatModelTests): ??? note "`supports_image_urls`" Boolean property indicating whether the chat model supports image inputs from - URLs. Defaults to `False`. + URLs. + + Defaults to `False`. If set to `True`, the chat model will be tested using content blocks of the form @@ -357,6 +360,7 @@ class ChatModelIntegrationTests(ChatModelTests): ??? note "`supports_pdf_inputs`" Boolean property indicating whether the chat model supports PDF inputs. + Defaults to `False`. If set to `True`, the chat model will be tested by inputting a @@ -381,6 +385,7 @@ class ChatModelIntegrationTests(ChatModelTests): ??? note "`supports_audio_inputs`" Boolean property indicating whether the chat model supports audio inputs. + Defaults to `False`. If set to `True`, the chat model will be tested by inputting an @@ -405,12 +410,15 @@ class ChatModelIntegrationTests(ChatModelTests): ??? note "`supports_video_inputs`" Boolean property indicating whether the chat model supports image inputs. + Defaults to `False`. No current tests are written for this feature. ??? note "`returns_usage_metadata`" Boolean property indicating whether the chat model returns usage metadata - on invoke and streaming responses. Defaults to `True`. + on invoke and streaming responses. + + Defaults to `True`. `usage_metadata` is an optional dict attribute on `AIMessage`s that track input and output tokens. diff --git a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py index 1b99be089b6..fb5077604f8 100644 --- a/libs/standard-tests/langchain_tests/unit_tests/chat_models.py +++ b/libs/standard-tests/langchain_tests/unit_tests/chat_models.py @@ -407,6 +407,7 @@ class ChatModelUnitTests(ChatModelTests): ??? note "`supports_image_inputs`" Boolean property indicating whether the chat model supports image inputs. + Defaults to `False`. If set to `True`, the chat model will be tested using the LangChain @@ -441,7 +442,9 @@ class ChatModelUnitTests(ChatModelTests): ??? note "`supports_image_urls`" Boolean property indicating whether the chat model supports image inputs from - URLs. Defaults to `False`. + URLs. + + Defaults to `False`. If set to `True`, the chat model will be tested using content blocks of the form. @@ -465,6 +468,7 @@ class ChatModelUnitTests(ChatModelTests): ??? note "`supports_pdf_inputs`" Boolean property indicating whether the chat model supports PDF inputs. + Defaults to `False`. If set to `True`, the chat model will be tested using the LangChain @@ -490,6 +494,7 @@ class ChatModelUnitTests(ChatModelTests): ??? note "`supports_audio_inputs`" Boolean property indicating whether the chat model supports audio inputs. + Defaults to `False`. If set to `True`, the chat model will be tested using the LangChain @@ -514,12 +519,15 @@ class ChatModelUnitTests(ChatModelTests): ??? note "`supports_video_inputs`" Boolean property indicating whether the chat model supports image inputs. + Defaults to `False`. No current tests are written for this feature. ??? note "`returns_usage_metadata`" Boolean property indicating whether the chat model returns usage metadata - on invoke and streaming responses. Defaults to `True`. + on invoke and streaming responses. + + Defaults to `True`. `usage_metadata` is an optional dict attribute on `AIMessage`s that track input and output tokens. diff --git a/libs/text-splitters/langchain_text_splitters/html.py b/libs/text-splitters/langchain_text_splitters/html.py index d47dbdd24cb..fd8cc2bf32a 100644 --- a/libs/text-splitters/langchain_text_splitters/html.py +++ b/libs/text-splitters/langchain_text_splitters/html.py @@ -189,7 +189,7 @@ class HTMLHeaderTextSplitter: Args: url: The URL to fetch content from. - timeout: Timeout for the request. Defaults to 10. + timeout: Timeout for the request. **kwargs: Additional keyword arguments for the request. Returns: diff --git a/libs/text-splitters/langchain_text_splitters/json.py b/libs/text-splitters/langchain_text_splitters/json.py index 5e5b39f7d08..d8374c01484 100644 --- a/libs/text-splitters/langchain_text_splitters/json.py +++ b/libs/text-splitters/langchain_text_splitters/json.py @@ -19,7 +19,7 @@ class RecursiveJsonSplitter: """ max_chunk_size: int = 2000 - """The maximum size for each chunk. Defaults to 2000.""" + """The maximum size for each chunk.""" min_chunk_size: int = 1800 """The minimum size for each chunk, derived from `max_chunk_size` if not explicitly provided.""" @@ -34,7 +34,7 @@ class RecursiveJsonSplitter: `max_chunk_size` if not explicitly provided. Args: - max_chunk_size: The maximum size for a chunk. Defaults to 2000. + max_chunk_size: The maximum size for a chunk. min_chunk_size: The minimum size for a chunk. If `None`, defaults to the maximum chunk size minus 200, with a lower bound of 50. """ diff --git a/libs/text-splitters/langchain_text_splitters/markdown.py b/libs/text-splitters/langchain_text_splitters/markdown.py index 46b1ad0c80b..efb1cdcc732 100644 --- a/libs/text-splitters/langchain_text_splitters/markdown.py +++ b/libs/text-splitters/langchain_text_splitters/markdown.py @@ -345,7 +345,6 @@ class ExperimentalMarkdownSyntaxTextSplitter: Defaults to `False`, which aggregates lines into larger chunks. strip_headers (bool): Whether to exclude headers from the resulting chunks. - Defaults to `True`. """ self.chunks: list[Document] = [] self.current_chunk = Document(page_content="")