The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
chore(core): fix some ruff preview rules (#32785)
Christophe Bornet01fdeede5011
diff --git a/libs/core/langchain_core/_api/deprecation.py b/libs/core/langchain_core/_api/deprecation.pyindex 7999d8a321..bac037fbce 100644--- a/libs/core/langchain_core/_api/deprecation.py+++ b/libs/core/langchain_core/_api/deprecation.py@@ -361,7 +361,8 @@ def deprecated(# Modify the docstring to include a deprecation notice.if (_alternative- and _alternative.split(".")[-1].lower() == _alternative.split(".")[-1]+ and _alternative.rsplit(".", maxsplit=1)[-1].lower()+ == _alternative.rsplit(".", maxsplit=1)[-1]):_alternative = f":meth:`~{_alternative}`"elif _alternative:@@ -369,8 +370,8 @@ def deprecated(if (_alternative_import- and _alternative_import.split(".")[-1].lower()- == _alternative_import.split(".")[-1]+ and _alternative_import.rsplit(".", maxsplit=1)[-1].lower()+ == _alternative_import.rsplit(".", maxsplit=1)[-1]):_alternative_import = f":meth:`~{_alternative_import}`"elif _alternative_import:@@ -474,7 +475,7 @@ def warn_deprecated(if not message:message = ""package_ = (- package or name.split(".")[0].replace("_", "-")+ package or name.split(".", maxsplit=1)[0].replace("_", "-")if "." in nameelse "LangChain")@@ -493,7 +494,7 @@ def warn_deprecated(message += f" and will be removed {removal}"if alternative_import:- alt_package = alternative_import.split(".")[0].replace("_", "-")+ alt_package = alternative_import.split(".", maxsplit=1)[0].replace("_", "-")if alt_package == package_:message += f". Use {alternative_import} instead."else:diff --git a/libs/core/langchain_core/embeddings/fake.py b/libs/core/langchain_core/embeddings/fake.pyindex 73b605dfa4..cd644bf8c6 100644--- a/libs/core/langchain_core/embeddings/fake.py+++ b/libs/core/langchain_core/embeddings/fake.py@@ -119,7 +119,8 @@ class DeterministicFakeEmbedding(Embeddings, BaseModel):rng = np.random.default_rng(seed)return list(rng.normal(size=self.size))- def _get_seed(self, text: str) -> int:+ @staticmethod+ def _get_seed(text: str) -> int:"""Get a seed for the random generator, using the hash of the text."""return int(hashlib.sha256(text.encode("utf-8")).hexdigest(), 16) % 10**8diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.pyindex f4ec3d48af..a07115196b 100644--- a/libs/core/langchain_core/language_models/chat_models.py+++ b/libs/core/langchain_core/language_models/chat_models.py@@ -148,8 +148,6 @@ def _format_for_tracing(messages: list[BaseMessage]) -> list[BaseMessage]:"type": key,key: block[key],}- else:- passmessages_to_trace.append(message_to_trace)return messages_to_tracediff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.pyindex e864054564..e0208d5f5a 100644--- a/libs/core/langchain_core/messages/ai.py+++ b/libs/core/langchain_core/messages/ai.py@@ -352,10 +352,7 @@ class AIMessageChunk(AIMessage, BaseMessageChunk):for chunk in self.tool_call_chunks:try:- if chunk["args"] is not None and chunk["args"] != "":- args_ = parse_partial_json(chunk["args"])- else:- args_ = {}+ args_ = parse_partial_json(chunk["args"]) if chunk["args"] else {}if isinstance(args_, dict):tool_calls.append(create_tool_call(diff --git a/libs/core/langchain_core/messages/base.py b/libs/core/langchain_core/messages/base.pyindex 2f831d79d0..8810d1ecf1 100644--- a/libs/core/langchain_core/messages/base.py+++ b/libs/core/langchain_core/messages/base.py@@ -179,9 +179,7 @@ def merge_content(elif merged and isinstance(merged[-1], str):merged[-1] += content# If second content is an empty string, treat as a no-op- elif content == "":- pass- else:+ elif content:# Otherwise, add the second content as a new element of the listmerged.append(content)return mergeddiff --git a/libs/core/langchain_core/output_parsers/json.py b/libs/core/langchain_core/output_parsers/json.pyindex 5457703775..0d1513e6d8 100644--- a/libs/core/langchain_core/output_parsers/json.py+++ b/libs/core/langchain_core/output_parsers/json.py@@ -46,11 +46,13 @@ class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]):def _diff(self, prev: Optional[Any], next: Any) -> Any:return jsonpatch.make_patch(prev, next).patch- def _get_schema(self, pydantic_object: type[TBaseModel]) -> dict[str, Any]:+ @staticmethod+ def _get_schema(pydantic_object: type[TBaseModel]) -> dict[str, Any]:if issubclass(pydantic_object, pydantic.BaseModel):return pydantic_object.model_json_schema()return pydantic_object.schema()+ @overridedef parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:"""Parse the result of an LLM call to a JSON object.diff --git a/libs/core/langchain_core/output_parsers/list.py b/libs/core/langchain_core/output_parsers/list.pyindex cc8ca4238c..b0d1ba4bfd 100644--- a/libs/core/langchain_core/output_parsers/list.py+++ b/libs/core/langchain_core/output_parsers/list.py@@ -155,6 +155,7 @@ class CommaSeparatedListOutputParser(ListOutputParser):"""return ["langchain", "output_parsers", "list"]+ @overridedef get_format_instructions(self) -> str:"""Return the format instructions for the comma-separated list output."""return (@@ -162,6 +163,7 @@ class CommaSeparatedListOutputParser(ListOutputParser):"eg: `foo, bar, baz` or `foo,bar,baz`")+ @overridedef parse(self, text: str) -> list[str]:"""Parse the output of an LLM call.@@ -224,6 +226,7 @@ class MarkdownListOutputParser(ListOutputParser):pattern: str = r"^\s*[-*]\s([^\n]+)$""""The pattern to match a Markdown list item."""+ @overridedef get_format_instructions(self) -> str:"""Return the format instructions for the Markdown list output."""return "Your response should be a markdown list, eg: `- foo\n- bar\n- baz`"diff --git a/libs/core/langchain_core/output_parsers/string.py b/libs/core/langchain_core/output_parsers/string.pyindex c2194e8bdd..35c9aab89f 100644--- a/libs/core/langchain_core/output_parsers/string.py+++ b/libs/core/langchain_core/output_parsers/string.py@@ -1,5 +1,7 @@"""String output parser."""+from typing_extensions import override+from langchain_core.output_parsers.transform import BaseTransformOutputParser@@ -29,6 +31,7 @@ class StrOutputParser(BaseTransformOutputParser[str]):"""Return the output parser type for serialization."""return "default"+ @overridedef parse(self, text: str) -> str:"""Returns the input text with no changes."""return textdiff --git a/libs/core/langchain_core/prompts/base.py b/libs/core/langchain_core/prompts/base.pyindex 6581c56902..370380d444 100644--- a/libs/core/langchain_core/prompts/base.py+++ b/libs/core/langchain_core/prompts/base.py@@ -210,7 +210,7 @@ class BasePromptTemplate(if self.metadata:config["metadata"] = {**config["metadata"], **self.metadata}if self.tags:- config["tags"] = config["tags"] + self.tags+ config["tags"] += self.tagsreturn self._call_with_config(self._format_prompt_with_error_handling,input,diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.pyindex 12357bf3a2..b6f1cdae28 100644--- a/libs/core/langchain_core/prompts/string.py+++ b/libs/core/langchain_core/prompts/string.py@@ -166,7 +166,7 @@ def mustache_schema(prefix = section_stack.pop()elif type_ in {"section", "inverted section"}:section_stack.append(prefix)- prefix = prefix + tuple(key.split("."))+ prefix += tuple(key.split("."))fields[prefix] = Falseelif type_ in {"variable", "no escape"}:fields[prefix + tuple(key.split("."))] = Truediff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.pyindex a7655072d6..17ebc55a51 100644--- a/libs/core/langchain_core/runnables/graph_mermaid.py+++ b/libs/core/langchain_core/runnables/graph_mermaid.py@@ -155,7 +155,7 @@ def draw_mermaid(nonlocal mermaid_graphself_loop = len(edges) == 1 and edges[0].source == edges[0].targetif prefix and not self_loop:- subgraph = prefix.split(":")[-1]+ subgraph = prefix.rsplit(":", maxsplit=1)[-1]if subgraph in seen_subgraphs:msg = (f"Found duplicate subgraph '{subgraph}' -- this likely means that "@@ -214,7 +214,7 @@ def draw_mermaid(# Add remaining subgraphs with edgesfor prefix, edges_ in edge_groups.items():- if ":" in prefix or prefix == "":+ if not prefix or ":" in prefix:continueadd_subgraph(edges_, prefix)seen_subgraphs.add(prefix)diff --git a/libs/core/langchain_core/tracers/core.py b/libs/core/langchain_core/tracers/core.pyindex 54261a2c22..9e680172a8 100644--- a/libs/core/langchain_core/tracers/core.py+++ b/libs/core/langchain_core/tracers/core.py@@ -82,7 +82,7 @@ class _TracerCore(ABC):"""Map of run ID to (trace_id, dotted_order). Cleared when tracer GCed."""@abstractmethod- def _persist_run(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]:+ def _persist_run(self, run: Run) -> Union[Coroutine[Any, Any, None], None]:"""Persist a run."""@staticmethod@@ -108,7 +108,7 @@ class _TracerCore(ABC):except: # noqa: E722return msg- def _start_trace(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # type: ignore[return]+ def _start_trace(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # type: ignore[return]current_dotted_order = run.start_time.strftime("%Y%m%dT%H%M%S%fZ") + str(run.id)if run.parent_run_id:if parent := self.order_map.get(run.parent_run_id):@@ -538,7 +538,7 @@ class _TracerCore(ABC):"""Return self copied."""return self- def _end_trace(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _end_trace(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""End a trace for a run.Args:@@ -546,7 +546,7 @@ class _TracerCore(ABC):"""return None- def _on_run_create(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_run_create(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process a run upon creation.Args:@@ -554,7 +554,7 @@ class _TracerCore(ABC):"""return None- def _on_run_update(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_run_update(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process a run upon update.Args:@@ -562,7 +562,7 @@ class _TracerCore(ABC):"""return None- def _on_llm_start(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_llm_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the LLM Run upon start.Args:@@ -575,7 +575,7 @@ class _TracerCore(ABC):run: Run, # noqa: ARG002token: str, # noqa: ARG002chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]], # noqa: ARG002- ) -> Union[None, Coroutine[Any, Any, None]]:+ ) -> Union[Coroutine[Any, Any, None], None]:"""Process new LLM token.Args:@@ -585,7 +585,7 @@ class _TracerCore(ABC):"""return None- def _on_llm_end(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_llm_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the LLM Run.Args:@@ -593,7 +593,7 @@ class _TracerCore(ABC):"""return None- def _on_llm_error(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_llm_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the LLM Run upon error.Args:@@ -601,7 +601,7 @@ class _TracerCore(ABC):"""return None- def _on_chain_start(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_chain_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Chain Run upon start.Args:@@ -609,7 +609,7 @@ class _TracerCore(ABC):"""return None- def _on_chain_end(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_chain_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Chain Run.Args:@@ -617,7 +617,7 @@ class _TracerCore(ABC):"""return None- def _on_chain_error(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_chain_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Chain Run upon error.Args:@@ -625,7 +625,7 @@ class _TracerCore(ABC):"""return None- def _on_tool_start(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_tool_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Tool Run upon start.Args:@@ -633,7 +633,7 @@ class _TracerCore(ABC):"""return None- def _on_tool_end(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_tool_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Tool Run.Args:@@ -641,7 +641,7 @@ class _TracerCore(ABC):"""return None- def _on_tool_error(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_tool_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Tool Run upon error.Args:@@ -649,7 +649,7 @@ class _TracerCore(ABC):"""return None- def _on_chat_model_start(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_chat_model_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Chat Model Run upon start.Args:@@ -657,7 +657,7 @@ class _TracerCore(ABC):"""return None- def _on_retriever_start(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_retriever_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Retriever Run upon start.Args:@@ -665,7 +665,7 @@ class _TracerCore(ABC):"""return None- def _on_retriever_end(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_retriever_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Retriever Run.Args:@@ -673,7 +673,7 @@ class _TracerCore(ABC):"""return None- def _on_retriever_error(self, run: Run) -> Union[None, Coroutine[Any, Any, None]]: # noqa: ARG002+ def _on_retriever_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002"""Process the Retriever Run upon error.Args:diff --git a/libs/core/langchain_core/utils/aiter.py b/libs/core/langchain_core/utils/aiter.pyindex 7a0477c658..e319fb0891 100644--- a/libs/core/langchain_core/utils/aiter.py+++ b/libs/core/langchain_core/utils/aiter.py@@ -37,7 +37,7 @@ _no_default = object()# before 3.10, the builtin anext() was not availabledef py_anext(iterator: AsyncIterator[T], default: Union[T, Any] = _no_default-) -> Awaitable[Union[T, None, Any]]:+) -> Awaitable[Union[T, Any, None]]:"""Pure-Python implementation of anext() for testing purposes.Closely matches the builtin anext() C implementation.diff --git a/libs/core/langchain_core/utils/mustache.py b/libs/core/langchain_core/utils/mustache.pyindex fd2ae35917..9c8a93cacc 100644--- a/libs/core/langchain_core/utils/mustache.py+++ b/libs/core/langchain_core/utils/mustache.py@@ -82,7 +82,7 @@ def l_sa_check("""# If there is a newline, or the previous tag was a standaloneif literal.find("\n") != -1 or is_standalone:- padding = literal.split("\n")[-1]+ padding = literal.rsplit("\n", maxsplit=1)[-1]# If all the characters since the last newline are spaces# Then the next tag could be a standalonediff --git a/libs/core/langchain_core/utils/utils.py b/libs/core/langchain_core/utils/utils.pyindex 1a41b63dc2..122f1c0af2 100644--- a/libs/core/langchain_core/utils/utils.py+++ b/libs/core/langchain_core/utils/utils.py@@ -134,7 +134,7 @@ def guard_import(try:module = importlib.import_module(module_name, package)except (ImportError, ModuleNotFoundError) as e:- pip_name = pip_name or module_name.split(".")[0].replace("_", "-")+ pip_name = pip_name or module_name.split(".", maxsplit=1)[0].replace("_", "-")msg = (f"Could not import {module_name} python package. "f"Please install it with `pip install {pip_name}`."diff --git a/libs/core/pyproject.toml b/libs/core/pyproject.tomlindex 1ea42cee12..8f48ba5d5e 100644--- a/libs/core/pyproject.toml+++ b/libs/core/pyproject.toml@@ -83,27 +83,39 @@ docstring-code-format = true[tool.ruff.lint]select = [ "ALL",]ignore = [- "C90", # McCabe complexity- "COM812", # Messes with the formatter- "FA100", # Can't activate since we exclude UP007 for now- "FIX002", # Line contains TODO- "ISC001", # Messes with the formatter+ "C90", # McCabe complexity+ "COM812", # Messes with the formatter+ "CPY", # No copyright+ "FIX002", # Line contains TODO+ "ISC001", # Messes with the formatter+ "C90", # McCabe complexity+ "COM812", # Messes with the formatter+ "CPY", # No copyright+ "FIX002", # Line contains TODO+ "ISC001", # Messes with the formatter"PERF203", # Rarely useful- "PLC0414", # Enable re-export- "PLR09", # Too many something (arg, statements, etc)- "RUF012", # Doesn't play well with Pydantic- "TC001", # Doesn't play well with Pydantic- "TC002", # Doesn't play well with Pydantic- "TC003", # Doesn't play well with Pydantic- "TD002", # Missing author in TODO- "TD003", # Missing issue link in TODO+ "PLR09", # Too many something (arg, statements, etc)+ "RUF012", # Doesn't play well with Pydantic+ "TC001", # Doesn't play well with Pydantic+ "TC002", # Doesn't play well with Pydantic+ "TC003", # Doesn't play well with Pydantic+ "TD002", # Missing author in TODO+ "TD003", # Missing issue link in TODO+ "PLR09", # Too many something (arg, statements, etc)+ "RUF012", # Doesn't play well with Pydantic+ "TC001", # Doesn't play well with Pydantic+ "TC002", # Doesn't play well with Pydantic+ "TC003", # Doesn't play well with Pydantic+ "TD002", # Missing author in TODO+ "TD003", # Missing issue link in TODO# TODO rules- "ANN401",- "BLE",- "ERA",- "PLC0415",- "PLR2004",+ "ANN401", # No Any types+ "BLE", # Blind exceptions+ "DOC", # Docstrings (preview)+ "ERA", # No commented-out code+ "PLC0415", # Imports outside top level+ "PLR2004", # Comparison to magic number]unfixable = ["PLW1510",]diff --git a/libs/core/tests/unit_tests/document_loaders/test_base.py b/libs/core/tests/unit_tests/document_loaders/test_base.pyindex 87ab70e9e0..c7d5811e18 100644--- a/libs/core/tests/unit_tests/document_loaders/test_base.py+++ b/libs/core/tests/unit_tests/document_loaders/test_base.py@@ -35,6 +35,7 @@ def test_base_blob_parser() -> None:def test_default_lazy_load() -> None:class FakeLoader(BaseLoader):+ @overridedef load(self) -> list[Document]:return [Document(page_content="foo"),@@ -57,6 +58,7 @@ def test_lazy_load_not_implemented() -> None:async def test_default_aload() -> None:class FakeLoader(BaseLoader):+ @overridedef lazy_load(self) -> Iterator[Document]:yield from [Document(page_content="foo"),diff --git a/libs/core/tests/unit_tests/example_selectors/test_base.py b/libs/core/tests/unit_tests/example_selectors/test_base.pyindex 5479362798..c49e7745f0 100644--- a/libs/core/tests/unit_tests/example_selectors/test_base.py+++ b/libs/core/tests/unit_tests/example_selectors/test_base.py@@ -1,5 +1,7 @@from typing import Optional+from typing_extensions import override+from langchain_core.example_selectors import BaseExampleSelector@@ -10,6 +12,7 @@ class DummyExampleSelector(BaseExampleSelector):def add_example(self, example: dict[str, str]) -> None:self.example = example+ @overridedef select_examples(self, input_variables: dict[str, str]) -> list[dict]:return [input_variables]diff --git a/libs/core/tests/unit_tests/indexing/test_in_memory_indexer.py b/libs/core/tests/unit_tests/indexing/test_in_memory_indexer.pyindex b16823a762..decf9f67ac 100644--- a/libs/core/tests/unit_tests/indexing/test_in_memory_indexer.py+++ b/libs/core/tests/unit_tests/indexing/test_in_memory_indexer.py@@ -7,6 +7,7 @@ from langchain_tests.integration_tests.indexer import (AsyncDocumentIndexTestSuite,DocumentIndexerTestSuite,)+from typing_extensions import overridefrom langchain_core.documents import Documentfrom langchain_core.indexing.base import DocumentIndex@@ -17,6 +18,7 @@ from langchain_core.indexing.in_memory import (class TestDocumentIndexerTestSuite(DocumentIndexerTestSuite):@pytest.fixture+ @overridedef index(self) -> Generator[DocumentIndex, None, None]:yield InMemoryDocumentIndex() # noqa: PT022@@ -24,6 +26,7 @@ class TestDocumentIndexerTestSuite(DocumentIndexerTestSuite):class TestAsyncDocumentIndexerTestSuite(AsyncDocumentIndexTestSuite):# Something funky is going on with mypy and async pytest fixture@pytest.fixture+ @overrideasync def index(self) -> AsyncGenerator[DocumentIndex, None]:yield InMemoryDocumentIndex() # noqa: PT022diff --git a/libs/core/tests/unit_tests/indexing/test_indexing.py b/libs/core/tests/unit_tests/indexing/test_indexing.pyindex cc579d4d03..a3e88e7535 100644--- a/libs/core/tests/unit_tests/indexing/test_indexing.py+++ b/libs/core/tests/unit_tests/indexing/test_indexing.py@@ -501,15 +501,14 @@ def test_incremental_fails_with_bad_source_ids(with pytest.raises(ValueError,match="Source id key is required when cleanup mode is "- "incremental or scoped_full.",+ "incremental or scoped_full",):# Should raise an error because no source id function was specifiedindex(loader, record_manager, vector_store, cleanup="incremental")with pytest.raises(ValueError,- match="Source ids are required when cleanup mode "- "is incremental or scoped_full.",+ match="Source ids are required when cleanup mode is incremental or scoped_full",):# Should raise an error because no source id function was specifiedindex(@@ -545,7 +544,7 @@ async def test_aincremental_fails_with_bad_source_ids(with pytest.raises(ValueError,match="Source id key is required when cleanup mode "- "is incremental or scoped_full.",+ "is incremental or scoped_full",):# Should raise an error because no source id function was specifiedawait aindex(@@ -557,8 +556,7 @@ async def test_aincremental_fails_with_bad_source_ids(with pytest.raises(ValueError,- match="Source ids are required when cleanup mode "- "is incremental or scoped_full.",+ match="Source ids are required when cleanup mode is incremental or scoped_full",):# Should raise an error because no source id function was specifiedawait aindex(@@ -838,15 +836,14 @@ def test_scoped_full_fails_with_bad_source_ids(with pytest.raises(ValueError,match="Source id key is required when cleanup mode "- "is incremental or scoped_full.",+ "is incremental or scoped_full",):# Should raise an error because no source id function was specifiedindex(loader, record_manager, vector_store, cleanup="scoped_full")with pytest.raises(ValueError,- match="Source ids are required when cleanup mode "- "is incremental or scoped_full.",+ match="Source ids are required when cleanup mode is incremental or scoped_full",):# Should raise an error because no source id function was specifiedindex(@@ -882,15 +879,14 @@ async def test_ascoped_full_fails_with_bad_source_ids(with pytest.raises(ValueError,match="Source id key is required when cleanup mode "- "is incremental or scoped_full.",+ "is incremental or scoped_full",):# Should raise an error because no source id function was specifiedawait aindex(loader, arecord_manager, vector_store, cleanup="scoped_full")with pytest.raises(ValueError,- match="Source ids are required when cleanup mode "- "is incremental or scoped_full.",+ match="Source ids are required when cleanup mode is incremental or scoped_full",):# Should raise an error because no source id function was specifiedawait aindex(diff --git a/libs/core/tests/unit_tests/prompts/test_chat.py b/libs/core/tests/unit_tests/prompts/test_chat.pyindex f873deb087..1b1fee580f 100644--- a/libs/core/tests/unit_tests/prompts/test_chat.py+++ b/libs/core/tests/unit_tests/prompts/test_chat.py@@ -173,7 +173,7 @@ def test_create_system_message_prompt_list_template_partial_variables_not_null()"""with pytest.raises(- ValueError, match="Partial variables are not supported for list of templates."+ ValueError, match="Partial variables are not supported for list of templates"):_ = SystemMessagePromptTemplate.from_template(template=[graph_creator_content1, graph_creator_content2],@@ -568,7 +568,7 @@ def test_chat_prompt_template_append_and_extend() -> None:def test_convert_to_message_is_strict() -> None:"""Verify that _convert_to_message is strict."""- with pytest.raises(ValueError, match="Unexpected message type: meow."):+ with pytest.raises(ValueError, match="Unexpected message type: meow"):# meow does not correspond to a valid message type.# this test is here to ensure that functionality to interpret `meow`# as a role is NOT added.@@ -758,7 +758,7 @@ async def test_chat_tmpl_from_messages_multipart_image() -> None:async def test_chat_tmpl_from_messages_multipart_formatting_with_path() -> None:"""Verify that we cannot pass `path` for an image as a variable."""- in_mem = "base64mem"+ in_mem_ = "base64mem"template = ChatPromptTemplate.from_messages([@@ -781,23 +781,27 @@ async def test_chat_tmpl_from_messages_multipart_formatting_with_path() -> None:)with pytest.raises(ValueError,- match="Loading images from 'path' has been removed "- "as of 0.3.15 for security reasons.",+ match=re.escape(+ "Loading images from 'path' has been removed as of 0.3.15 "+ "for security reasons."+ ),):template.format_messages(name="R2D2",- in_mem=in_mem,+ in_mem=in_mem_,file_path="some/path",)with pytest.raises(ValueError,- match="Loading images from 'path' has been removed "- "as of 0.3.15 for security reasons.",+ match=re.escape(+ "Loading images from 'path' has been removed as of 0.3.15 "+ "for security reasons."+ ),):await template.aformat_messages(name="R2D2",- in_mem=in_mem,+ in_mem=in_mem_,file_path="some/path",)@@ -900,7 +904,7 @@ def test_chat_prompt_message_dict() -> None:with pytest.raises(ValueError, match="Invalid template: False"):ChatPromptTemplate([{"role": "system", "content": False}])- with pytest.raises(ValueError, match="Unexpected message type: foo."):+ with pytest.raises(ValueError, match="Unexpected message type: foo"):ChatPromptTemplate([{"role": "foo", "content": "foo"}])diff --git a/libs/core/tests/unit_tests/prompts/test_loading.py b/libs/core/tests/unit_tests/prompts/test_loading.pyindex 76d7531477..3e230c54bb 100644--- a/libs/core/tests/unit_tests/prompts/test_loading.py+++ b/libs/core/tests/unit_tests/prompts/test_loading.py@@ -49,14 +49,14 @@ def test_loading_from_json() -> None:def test_loading_jinja_from_json() -> None:"""Test that loading jinja2 format prompts from JSON raises ValueError."""prompt_path = EXAMPLE_DIR / "jinja_injection_prompt.json"- with pytest.raises(ValueError, match=".*can lead to arbitrary code execution.*"):+ with pytest.raises(ValueError, match=r".*can lead to arbitrary code execution.*"):load_prompt(prompt_path)def test_loading_jinja_from_yaml() -> None:"""Test that loading jinja2 format prompts from YAML raises ValueError."""prompt_path = EXAMPLE_DIR / "jinja_injection_prompt.yaml"- with pytest.raises(ValueError, match=".*can lead to arbitrary code execution.*"):+ with pytest.raises(ValueError, match=r".*can lead to arbitrary code execution.*"):load_prompt(prompt_path)diff --git a/libs/core/tests/unit_tests/prompts/test_structured.py b/libs/core/tests/unit_tests/prompts/test_structured.pyindex 4f5cbc9ab7..9758f1d1d1 100644--- a/libs/core/tests/unit_tests/prompts/test_structured.py+++ b/libs/core/tests/unit_tests/prompts/test_structured.py@@ -4,6 +4,7 @@ from typing import Any, Union, castimport pytestfrom pydantic import BaseModel+from typing_extensions import overridefrom langchain_core.language_models import FakeListChatModelfrom langchain_core.load.dump import dumps@@ -27,6 +28,7 @@ def _fake_runnable(class FakeStructuredChatModel(FakeListChatModel):"""Fake ChatModel for testing purposes."""+ @overridedef with_structured_output(self, schema: Union[dict, type[BaseModel]], **kwargs: Any) -> Runnable:diff --git a/libs/core/tests/unit_tests/runnables/test_context.py b/libs/core/tests/unit_tests/runnables/test_context.pyindex b638ac78f5..073b972fe0 100644--- a/libs/core/tests/unit_tests/runnables/test_context.py+++ b/libs/core/tests/unit_tests/runnables/test_context.py@@ -368,7 +368,7 @@ def test_runnable_context_seq_key_order() -> None:with pytest.raises(ValueError,- match="Context setter for key foo must be defined after all getters.",+ match="Context setter for key foo must be defined after all getters",):seq.invoke("foo")diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.pyindex 5c33c5a99c..530ce18574 100644--- a/libs/core/tests/unit_tests/runnables/test_runnable.py+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py@@ -3799,12 +3799,14 @@ class FakeSplitIntoListParser(BaseOutputParser[list[str]]):"""Return whether or not the class is serializable."""return True+ @overridedef get_format_instructions(self) -> str:return ("Your response should be a list of comma separated values, ""eg: `foo, bar, baz`")+ @overridedef parse(self, text: str) -> list[str]:"""Parse the output of an LLM call."""return text.strip().split(", ")diff --git a/libs/core/tests/unit_tests/stores/test_in_memory.py b/libs/core/tests/unit_tests/stores/test_in_memory.pyindex cc8ec684fb..7a0036b578 100644--- a/libs/core/tests/unit_tests/stores/test_in_memory.py+++ b/libs/core/tests/unit_tests/stores/test_in_memory.py@@ -3,6 +3,7 @@ from langchain_tests.integration_tests.base_store import (BaseStoreAsyncTests,BaseStoreSyncTests,)+from typing_extensions import overridefrom langchain_core.stores import InMemoryStore@@ -10,20 +11,24 @@ from langchain_core.stores import InMemoryStore# Check against standard testsclass TestSyncInMemoryStore(BaseStoreSyncTests):@pytest.fixture+ @overridedef kv_store(self) -> InMemoryStore:return InMemoryStore()@pytest.fixture+ @overridedef three_values(self) -> tuple[str, str, str]:return "value1", "value2", "value3"class TestAsyncInMemoryStore(BaseStoreAsyncTests):@pytest.fixture+ @overrideasync def kv_store(self) -> InMemoryStore:return InMemoryStore()@pytest.fixture+ @overridedef three_values(self) -> tuple[str, str, str]:return "value1", "value2", "value3"diff --git a/libs/core/tests/unit_tests/test_messages.py b/libs/core/tests/unit_tests/test_messages.pyindex b1df495523..9f0f43474e 100644--- a/libs/core/tests/unit_tests/test_messages.py+++ b/libs/core/tests/unit_tests/test_messages.py@@ -202,7 +202,7 @@ def test_chat_message_chunks() -> None:)with pytest.raises(- ValueError, match="Cannot concatenate ChatMessageChunks with different roles."+ ValueError, match="Cannot concatenate ChatMessageChunks with different roles"):ChatMessageChunk(role="User", content="I am") + ChatMessageChunk(role="Assistant", content=" indeed."@@ -311,7 +311,7 @@ def test_function_message_chunks() -> None:with pytest.raises(ValueError,- match="Cannot concatenate FunctionMessageChunks with different names.",+ match="Cannot concatenate FunctionMessageChunks with different names",):FunctionMessageChunk(name="hello", content="I am") + FunctionMessageChunk(name="bye", content=" indeed."@@ -327,7 +327,7 @@ def test_ai_message_chunks() -> None:with pytest.raises(ValueError,- match="Cannot concatenate AIMessageChunks with different example values.",+ match="Cannot concatenate AIMessageChunks with different example values",):AIMessageChunk(example=True, content="I am") + AIMessageChunk(example=False, content=" indeed."@@ -339,7 +339,7 @@ class TestGetBufferString:_AI_MSG = AIMessage(content="ai")def test_empty_input(self) -> None:- assert get_buffer_string([]) == ""+ assert not get_buffer_string([])def test_valid_single_message(self) -> None:expected_output = "Human: human"@@ -1052,7 +1052,7 @@ def test_message_text() -> None:# content dict types: [text], [not text], [no type]assert HumanMessage(content="foo").text() == "foo"- assert AIMessage(content=[]).text() == ""+ assert not AIMessage(content=[]).text()assert AIMessage(content=["foo", "bar"]).text() == "foobar"assert (AIMessage(@@ -1092,14 +1092,11 @@ def test_message_text() -> None:assert (AIMessage(content=[{"text": "hi there"}, "hi"]).text() == "hi") # missing type: text- assert AIMessage(content=[{"type": "nottext", "text": "hi"}]).text() == ""- assert AIMessage(content=[]).text() == ""- assert (- AIMessage(- content="", tool_calls=[create_tool_call(name="a", args={"b": 1}, id=None)]- ).text()- == ""- )+ assert not AIMessage(content=[{"type": "nottext", "text": "hi"}]).text()+ assert not AIMessage(content=[]).text()+ assert not AIMessage(+ content="", tool_calls=[create_tool_call(name="a", args={"b": 1}, id=None)]+ ).text()def test_is_data_content_block() -> None:diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.pyindex 72c6a5a387..6310736122 100644--- a/libs/core/tests/unit_tests/test_tools.py+++ b/libs/core/tests/unit_tests/test_tools.py@@ -24,7 +24,7 @@ import pytestfrom pydantic import BaseModel, Field, ValidationErrorfrom pydantic.v1 import BaseModel as BaseModelV1from pydantic.v1 import ValidationError as ValidationErrorV1-from typing_extensions import TypedDict+from typing_extensions import TypedDict, overridefrom langchain_core import toolsfrom langchain_core.callbacks import (@@ -119,6 +119,7 @@ class _MockStructuredTool(BaseTool):args_schema: type[BaseModel] = _MockSchemadescription: str = "A Structured Tool"+ @overridedef _run(self, *, arg1: int, arg2: bool, arg3: Optional[dict] = None) -> str:return f"{arg1} {arg2} {arg3}"@@ -146,6 +147,7 @@ def test_misannotated_base_tool_raises_error() -> None:args_schema: BaseModel = _MockSchema # type: ignore[assignment]description: str = "A Structured Tool"+ @overridedef _run(self, *, arg1: int, arg2: bool, arg3: Optional[dict] = None) -> str:@@ -165,6 +167,7 @@ def test_forward_ref_annotated_base_tool_accepted() -> None:args_schema: "type[BaseModel]" = _MockSchemadescription: str = "A Structured Tool"+ @overridedef _run(self, *, arg1: int, arg2: bool, arg3: Optional[dict] = None) -> str:return f"{arg1} {arg2} {arg3}"@@ -182,6 +185,7 @@ def test_subclass_annotated_base_tool_accepted() -> None:args_schema: type[_MockSchema] = _MockSchemadescription: str = "A Structured Tool"+ @overridedef _run(self, *, arg1: int, arg2: bool, arg3: Optional[dict] = None) -> str:return f"{arg1} {arg2} {arg3}"@@ -237,6 +241,7 @@ def test_args_kwargs_filtered() -> None:name: str = "single_arg_tool"description: str = "A single arged tool with kwargs"+ @overridedef _run(self,some_arg: str,@@ -260,6 +265,7 @@ def test_args_kwargs_filtered() -> None:name: str = "single_arg_tool"description: str = "A single arged tool with kwargs"+ @overridedef _run(self,*args: Any,@@ -396,9 +402,11 @@ def test_base_tool_inheritance_base_schema() -> None:name: str = "simple_tool"description: str = "A Simple Tool"+ @overridedef _run(self, tool_input: str) -> str:return f"{tool_input}"+ @overrideasync def _arun(self, tool_input: str) -> str:raise NotImplementedError@@ -705,7 +713,7 @@ def test_missing_docstring() -> None:class MyTool(BaseModel):foo: str- assert MyTool.description == "" # type: ignore[attr-defined]+ assert not MyTool.description # type: ignore[attr-defined]def test_create_tool_positional_args() -> None:@@ -909,9 +917,11 @@ def test_validation_error_handling_non_validation_error() -> Union[str, dict[str, Any]]:raise NotImplementedError+ @overridedef _run(self) -> str:return "dummy"+ @overrideasync def _arun(self) -> str:return "dummy"@@ -975,9 +985,11 @@ async def test_async_validation_error_handling_non_validation_error() -> Union[str, dict[str, Any]]:raise NotImplementedError+ @overridedef _run(self) -> str:return "dummy"+ @overrideasync def _arun(self) -> str:return "dummy"@@ -1096,11 +1108,13 @@ class FooBase(BaseTool):name: str = "Foo"description: str = "Foo"+ @overridedef _run(self, bar: Any, bar_config: RunnableConfig, **kwargs: Any) -> Any:return assert_bar(bar, bar_config)class AFooBase(FooBase):+ @overrideasync def _arun(self, bar: Any, bar_config: RunnableConfig, **kwargs: Any) -> Any:return assert_bar(bar, bar_config)@@ -1121,6 +1135,7 @@ def test_tool_pass_config(tool: BaseTool) -> None:class FooBaseNonPickleable(FooBase):+ @overridedef _run(self, bar: Any, bar_config: RunnableConfig, **kwargs: Any) -> Any:return True@@ -1332,7 +1347,7 @@ def test_tool_invalid_docstrings() -> None:return barfor func in {foo3, foo4}:- with pytest.raises(ValueError, match="Found invalid Google-Style docstring."):+ with pytest.raises(ValueError, match="Found invalid Google-Style docstring"):_ = tool(func, parse_docstring=True)def foo5(bar: str, baz: int) -> str: # noqa: D417@@ -1345,7 +1360,7 @@ def test_tool_invalid_docstrings() -> None:return barwith pytest.raises(- ValueError, match="Arg banana in docstring not found in function signature."+ ValueError, match="Arg banana in docstring not found in function signature"):_ = tool(foo5, parse_docstring=True)@@ -1404,10 +1419,11 @@ class _MockStructuredToolWithRawOutput(BaseTool):description: str = "A Structured Tool"response_format: Literal["content_and_artifact"] = "content_and_artifact"+ @overridedef _run(self,arg1: int,- arg2: bool, # noqa: FBT001+ arg2: bool,arg3: Optional[dict] = None,) -> tuple[str, dict]:return f"{arg1} {arg2}", {"arg1": arg1, "arg2": arg2, "arg3": arg3}@@ -1554,6 +1570,7 @@ class InjectedTool(BaseTool):name: str = "foo"description: str = "foo."+ @overridedef _run(self, x: int, y: Annotated[str, InjectedToolArg]) -> Any:"""Foo.@@ -1578,6 +1595,7 @@ class InjectedToolWithSchema(BaseTool):description: str = "foo."args_schema: type[BaseModel] = fooSchema+ @overridedef _run(self, x: int, y: str) -> Any:return y@@ -1743,6 +1761,7 @@ def test_tool_inherited_injected_arg() -> None:description: str = "foo."args_schema: type[BaseModel] = FooSchema+ @overridedef _run(self, x: int, y: str) -> Any:return y@@ -1854,6 +1873,7 @@ def test_args_schema_as_pydantic(pydantic_model: Any) -> None:class SomeTool(BaseTool):args_schema: type[pydantic_model] = pydantic_model+ @overridedef _run(self, *args: Any, **kwargs: Any) -> str:return "foo"@@ -1913,6 +1933,7 @@ def test_args_schema_explicitly_typed() -> None:# for pydantic 2!args_schema: type[BaseModel] = Foo+ @overridedef _run(self, *args: Any, **kwargs: Any) -> str:return "foo"@@ -2136,6 +2157,7 @@ def test_tool_annotations_preserved() -> None:def test_create_retriever_tool() -> None:class MyRetriever(BaseRetriever):+ @overridedef _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) -> list[Document]:@@ -2364,6 +2386,7 @@ def test_tool_mutate_input() -> None:name: str = "MyTool"description: str = "a tool"+ @overridedef _run(self,x: str,diff --git a/libs/core/tests/unit_tests/utils/test_env.py b/libs/core/tests/unit_tests/utils/test_env.pyindex ea49268cee..0d52e8534c 100644--- a/libs/core/tests/unit_tests/utils/test_env.py+++ b/libs/core/tests/unit_tests/utils/test_env.py@@ -55,7 +55,7 @@ def test_get_from_dict_or_env() -> None:ValueError,match="Did not find not exists, ""please add an environment variable `__SOME_KEY_IN_ENV` which contains it, "- "or pass `not exists` as a named parameter.",+ "or pass `not exists` as a named parameter",):assert (get_from_dict_or_env(diff --git a/libs/core/tests/unit_tests/utils/test_html.py b/libs/core/tests/unit_tests/utils/test_html.pyindex a6332e4b60..7d6c00dab6 100644--- a/libs/core/tests/unit_tests/utils/test_html.py+++ b/libs/core/tests/unit_tests/utils/test_html.py@@ -36,31 +36,31 @@ def test_find_all_links_multiple() -> None:def test_find_all_links_ignore_suffix() -> None:html = 'href="foobar{suffix}"'- for suffix in SUFFIXES_TO_IGNORE:- actual = find_all_links(html.format(suffix=suffix))+ for suffix_ in SUFFIXES_TO_IGNORE:+ actual = find_all_links(html.format(suffix=suffix_))assert actual == []# Don't ignore if pattern doesn't occur at end of link.html = 'href="foobar{suffix}more"'- for suffix in SUFFIXES_TO_IGNORE:- actual = find_all_links(html.format(suffix=suffix))- assert actual == [f"foobar{suffix}more"]+ for suffix_ in SUFFIXES_TO_IGNORE:+ actual = find_all_links(html.format(suffix=suffix_))+ assert actual == [f"foobar{suffix_}more"]def test_find_all_links_ignore_prefix() -> None:html = 'href="{prefix}foobar"'- for prefix in PREFIXES_TO_IGNORE:- actual = find_all_links(html.format(prefix=prefix))+ for prefix_ in PREFIXES_TO_IGNORE:+ actual = find_all_links(html.format(prefix=prefix_))assert actual == []# Don't ignore if pattern doesn't occur at beginning of link.html = 'href="foobar{prefix}more"'- for prefix in PREFIXES_TO_IGNORE:+ for prefix_ in PREFIXES_TO_IGNORE:# Pound signs are split on when not prefixes.- if prefix == "#":+ if prefix_ == "#":continue- actual = find_all_links(html.format(prefix=prefix))- assert actual == [f"foobar{prefix}more"]+ actual = find_all_links(html.format(prefix=prefix_))+ assert actual == [f"foobar{prefix_}more"]def test_find_all_links_drop_fragment() -> None:diff --git a/libs/core/tests/unit_tests/utils/test_strings.py b/libs/core/tests/unit_tests/utils/test_strings.pyindex 2162fb3efe..85c328a475 100644--- a/libs/core/tests/unit_tests/utils/test_strings.py+++ b/libs/core/tests/unit_tests/utils/test_strings.py@@ -24,7 +24,7 @@ def test_sanitize_for_postgres() -> None:assert sanitize_for_postgres(clean_text) == clean_text# Test empty string- assert sanitize_for_postgres("") == ""+ assert not sanitize_for_postgres("")# Test with multiple consecutive NUL bytestext_with_multiple_nuls = "Hello\x00\x00\x00world"diff --git a/libs/core/tests/unit_tests/utils/test_usage.py b/libs/core/tests/unit_tests/utils/test_usage.pyindex 04c89b0537..1ad3500d6d 100644--- a/libs/core/tests/unit_tests/utils/test_usage.py+++ b/libs/core/tests/unit_tests/utils/test_usage.py@@ -30,7 +30,7 @@ def test_dict_int_op_max_depth_exceeded() -> None:left = {"a": {"b": {"c": 1}}}right = {"a": {"b": {"c": 2}}}with pytest.raises(- ValueError, match="max_depth=2 exceeded, unable to combine dicts."+ ValueError, match="max_depth=2 exceeded, unable to combine dicts"):_dict_int_op(left, right, operator.add, max_depth=2)@@ -40,6 +40,6 @@ def test_dict_int_op_invalid_types() -> None:right = {"a": 2, "b": 3}with pytest.raises(ValueError,- match="Only dict and int values are supported.",+ match="Only dict and int values are supported",):_dict_int_op(left, right, operator.add)diff --git a/libs/core/tests/unit_tests/utils/test_utils.py b/libs/core/tests/unit_tests/utils/test_utils.pyindex c38b951da6..51967b82a1 100644--- a/libs/core/tests/unit_tests/utils/test_utils.py+++ b/libs/core/tests/unit_tests/utils/test_utils.py@@ -96,7 +96,7 @@ def test_check_package_version(TypeError,match=("Additional kwargs key a already exists in left dict and value "- "has unsupported type .+tuple.+."+ r"has unsupported type .+tuple.+."),),),@@ -136,7 +136,7 @@ def test_merge_dicts(({"type": "foo"},{"type": "bar"},- pytest.raises(ValueError, match="Unable to merge."),+ pytest.raises(ValueError, match="Unable to merge"),),],)