The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
chore(langchain): add ruff rules N (#32098)
Christophe Bornet1e101ae9a280
diff --git a/libs/langchain/langchain/agents/__init__.py b/libs/langchain/langchain/agents/__init__.pyindex f919f5fbdf..f182b3b6b7 100644--- a/libs/langchain/langchain/agents/__init__.py+++ b/libs/langchain/langchain/agents/__init__.py@@ -120,11 +120,11 @@ def __getattr__(name: str) -> Any:"""Get attr name."""if name in DEPRECATED_CODE:# Get directory of langchain package- HERE = Path(__file__).parents[1]+ here = Path(__file__).parents[1]relative_path = as_import_path(Path(__file__).parent,suffix=name,- relative_to=HERE,+ relative_to=here,)old_path = "langchain." + relative_pathnew_path = "langchain_experimental." + relative_pathdiff --git a/libs/langchain/langchain/agents/agent_toolkits/csv/__init__.py b/libs/langchain/langchain/agents/agent_toolkits/csv/__init__.pyindex 82c49e246a..c877de1257 100644--- a/libs/langchain/langchain/agents/agent_toolkits/csv/__init__.py+++ b/libs/langchain/langchain/agents/agent_toolkits/csv/__init__.py@@ -1,19 +1,10 @@-from pathlib import Pathfrom typing import Any-from langchain_core._api.path import as_import_path-def __getattr__(name: str) -> Any:"""Get attr name."""if name == "create_csv_agent":- # Get directory of langchain package- HERE = Path(__file__).parents[3]- here = as_import_path(Path(__file__).parent, relative_to=HERE)-- old_path = "langchain." + here + "." + name- new_path = "langchain_experimental." + here + "." + namemsg = ("This agent has been moved to langchain experiment. ""This agent relies on python REPL tool under the hood, so to use it "@@ -21,7 +12,9 @@ def __getattr__(name: str) -> Any:"Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md ""and https://github.com/langchain-ai/langchain/discussions/11680""To keep using this code as is, install langchain experimental and "- f"update your import statement from:\n `{old_path}` to `{new_path}`."+ "update your import statement from:\n "+ f"`langchain.agents.agent_toolkits.csv.{name}` to "+ f"`langchain_experimental.agents.agent_toolkits.{name}`.")raise ImportError(msg)msg = f"{name} does not exist"diff --git a/libs/langchain/langchain/agents/agent_toolkits/pandas/__init__.py b/libs/langchain/langchain/agents/agent_toolkits/pandas/__init__.pyindex edb90f5a7c..4ed1ad4a19 100644--- a/libs/langchain/langchain/agents/agent_toolkits/pandas/__init__.py+++ b/libs/langchain/langchain/agents/agent_toolkits/pandas/__init__.py@@ -1,19 +1,10 @@-from pathlib import Pathfrom typing import Any-from langchain_core._api.path import as_import_path-def __getattr__(name: str) -> Any:"""Get attr name."""if name == "create_pandas_dataframe_agent":- # Get directory of langchain package- HERE = Path(__file__).parents[3]- here = as_import_path(Path(__file__).parent, relative_to=HERE)-- old_path = "langchain." + here + "." + name- new_path = "langchain_experimental." + here + "." + namemsg = ("This agent has been moved to langchain experiment. ""This agent relies on python REPL tool under the hood, so to use it "@@ -21,7 +12,9 @@ def __getattr__(name: str) -> Any:"Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md ""and https://github.com/langchain-ai/langchain/discussions/11680""To keep using this code as is, install langchain experimental and "- f"update your import statement from:\n `{old_path}` to `{new_path}`."+ "update your import statement from:\n"+ f"`langchain.agents.agent_toolkits.pandas.{name}` to "+ f"`langchain_experimental.agents.agent_toolkits.{name}`.")raise ImportError(msg)msg = f"{name} does not exist"diff --git a/libs/langchain/langchain/agents/agent_toolkits/python/__init__.py b/libs/langchain/langchain/agents/agent_toolkits/python/__init__.pyindex 0ce36303ed..674f38e96c 100644--- a/libs/langchain/langchain/agents/agent_toolkits/python/__init__.py+++ b/libs/langchain/langchain/agents/agent_toolkits/python/__init__.py@@ -1,19 +1,10 @@-from pathlib import Pathfrom typing import Any-from langchain_core._api.path import as_import_path-def __getattr__(name: str) -> Any:"""Get attr name."""if name == "create_python_agent":- # Get directory of langchain package- HERE = Path(__file__).parents[3]- here = as_import_path(Path(__file__).parent, relative_to=HERE)-- old_path = "langchain." + here + "." + name- new_path = "langchain_experimental." + here + "." + namemsg = ("This agent has been moved to langchain experiment. ""This agent relies on python REPL tool under the hood, so to use it "@@ -21,7 +12,9 @@ def __getattr__(name: str) -> Any:"Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md ""and https://github.com/langchain-ai/langchain/discussions/11680""To keep using this code as is, install langchain experimental and "- f"update your import statement from:\n `{old_path}` to `{new_path}`."+ "update your import statement from:\n"+ f"`langchain.agents.agent_toolkits.python.{name}` to "+ f"`langchain_experimental.agents.agent_toolkits.{name}`.")raise ImportError(msg)msg = f"{name} does not exist"diff --git a/libs/langchain/langchain/agents/agent_toolkits/spark/__init__.py b/libs/langchain/langchain/agents/agent_toolkits/spark/__init__.pyindex 76f96cc335..5be92f2379 100644--- a/libs/langchain/langchain/agents/agent_toolkits/spark/__init__.py+++ b/libs/langchain/langchain/agents/agent_toolkits/spark/__init__.py@@ -1,19 +1,10 @@-from pathlib import Pathfrom typing import Any-from langchain_core._api.path import as_import_path-def __getattr__(name: str) -> Any:"""Get attr name."""if name == "create_spark_dataframe_agent":- # Get directory of langchain package- HERE = Path(__file__).parents[3]- here = as_import_path(Path(__file__).parent, relative_to=HERE)-- old_path = "langchain." + here + "." + name- new_path = "langchain_experimental." + here + "." + namemsg = ("This agent has been moved to langchain experiment. ""This agent relies on python REPL tool under the hood, so to use it "@@ -21,7 +12,9 @@ def __getattr__(name: str) -> Any:"Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md ""and https://github.com/langchain-ai/langchain/discussions/11680""To keep using this code as is, install langchain experimental and "- f"update your import statement from:\n `{old_path}` to `{new_path}`."+ "update your import statement from:\n"+ f"`langchain.agents.agent_toolkits.spark.{name}` to "+ f"`langchain_experimental.agents.agent_toolkits.{name}`.")raise ImportError(msg)msg = f"{name} does not exist"diff --git a/libs/langchain/langchain/agents/agent_toolkits/xorbits/__init__.py b/libs/langchain/langchain/agents/agent_toolkits/xorbits/__init__.pyindex 7f23f48262..04c235e9bd 100644--- a/libs/langchain/langchain/agents/agent_toolkits/xorbits/__init__.py+++ b/libs/langchain/langchain/agents/agent_toolkits/xorbits/__init__.py@@ -1,19 +1,10 @@-from pathlib import Pathfrom typing import Any-from langchain_core._api.path import as_import_path-def __getattr__(name: str) -> Any:"""Get attr name."""if name == "create_xorbits_agent":- # Get directory of langchain package- HERE = Path(__file__).parents[3]- here = as_import_path(Path(__file__).parent, relative_to=HERE)-- old_path = "langchain." + here + "." + name- new_path = "langchain_experimental." + here + "." + namemsg = ("This agent has been moved to langchain experiment. ""This agent relies on python REPL tool under the hood, so to use it "@@ -21,7 +12,9 @@ def __getattr__(name: str) -> Any:"Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md ""and https://github.com/langchain-ai/langchain/discussions/11680""To keep using this code as is, install langchain experimental and "- f"update your import statement from:\n `{old_path}` to `{new_path}`."+ "update your import statement from:\n"+ f"`langchain.agents.agent_toolkits.xorbits.{name}` to "+ f"`langchain_experimental.agents.agent_toolkits.{name}`.")raise ImportError(msg)msg = f"{name} does not exist"diff --git a/libs/langchain/langchain/callbacks/streamlit/__init__.py b/libs/langchain/langchain/callbacks/streamlit/__init__.pyindex 21f3343612..90319bb310 100644--- a/libs/langchain/langchain/callbacks/streamlit/__init__.py+++ b/libs/langchain/langchain/callbacks/streamlit/__init__.py@@ -9,7 +9,7 @@ if TYPE_CHECKING:from streamlit.delta_generator import DeltaGenerator-def StreamlitCallbackHandler(+def StreamlitCallbackHandler( # noqa: N802parent_container: DeltaGenerator,*,max_thought_containers: int = 4,diff --git a/libs/langchain/langchain/chains/llm.py b/libs/langchain/langchain/chains/llm.pyindex cb7b7dc941..f1729316a3 100644--- a/libs/langchain/langchain/chains/llm.py+++ b/libs/langchain/langchain/chains/llm.py@@ -79,7 +79,7 @@ class LLMChain(Chain):@classmethod@override- def is_lc_serializable(self) -> bool:+ def is_lc_serializable(cls) -> bool:return Trueprompt: BasePromptTemplatediff --git a/libs/langchain/langchain/chat_models/base.py b/libs/langchain/langchain/chat_models/base.pyindex 18ce3418d7..2fbe685fec 100644--- a/libs/langchain/langchain/chat_models/base.py+++ b/libs/langchain/langchain/chat_models/base.py@@ -645,6 +645,7 @@ class _ConfigurableModel(Runnable[LanguageModelInput, Any]):)@property+ @overridedef InputType(self) -> TypeAlias:"""Get the input type for this runnable."""from langchain_core.prompt_values import (diff --git a/libs/langchain/langchain/retrievers/self_query/base.py b/libs/langchain/langchain/retrievers/self_query/base.pyindex dfbe5f709b..7ffa2a43ea 100644--- a/libs/langchain/langchain/retrievers/self_query/base.py+++ b/libs/langchain/langchain/retrievers/self_query/base.py@@ -97,7 +97,7 @@ def _get_builtin_translator(vectorstore: VectorStore) -> Visitor:Pinecone as CommunityPinecone,)- BUILTIN_TRANSLATORS: dict[type[VectorStore], type[Visitor]] = {+ builtin_translators: dict[type[VectorStore], type[Visitor]] = {AstraDB: AstraDBTranslator,PGVector: PGVectorTranslator,CommunityPinecone: PineconeTranslator,@@ -128,8 +128,8 @@ def _get_builtin_translator(vectorstore: VectorStore) -> Visitor:field.name for field in (vectorstore.meta_fields or []) if field.index]return TencentVectorDBTranslator(fields)- if vectorstore.__class__ in BUILTIN_TRANSLATORS:- return BUILTIN_TRANSLATORS[vectorstore.__class__]()+ if vectorstore.__class__ in builtin_translators:+ return builtin_translators[vectorstore.__class__]()try:from langchain_astradb.vectorstores import AstraDBVectorStoreexcept ImportError:diff --git a/libs/langchain/langchain/smith/evaluation/runner_utils.py b/libs/langchain/langchain/smith/evaluation/runner_utils.pyindex b0f5517cf6..b50ae0cbf2 100644--- a/libs/langchain/langchain/smith/evaluation/runner_utils.py+++ b/libs/langchain/langchain/smith/evaluation/runner_utils.py@@ -154,7 +154,7 @@ class TestResult(dict):class EvalError(dict):"""Your architecture raised an error."""- def __init__(self, Error: BaseException, **kwargs: Any) -> None:+ def __init__(self, Error: BaseException, **kwargs: Any) -> None: # noqa: N803"""Initialize the EvalError with an error and additional attributes.Args:diff --git a/libs/langchain/langchain/tools/__init__.py b/libs/langchain/langchain/tools/__init__.pyindex 0934636ac2..6bfdb26f4a 100644--- a/libs/langchain/langchain/tools/__init__.py+++ b/libs/langchain/langchain/tools/__init__.py@@ -38,7 +38,7 @@ from langchain._api.interactive_env import is_interactive_env_DEPRECATED_TOOLS = {"PythonAstREPLTool", "PythonREPLTool"}-def _import_python_tool_PythonAstREPLTool() -> Any:+def _import_python_tool_python_ast_repl_tool() -> Any:msg = ("This tool has been moved to langchain experiment. ""This tool has access to a python REPL. "@@ -50,7 +50,7 @@ def _import_python_tool_PythonAstREPLTool() -> Any:raise ImportError(msg)-def _import_python_tool_PythonREPLTool() -> Any:+def _import_python_tool_python_repl_tool() -> Any:msg = ("This tool has been moved to langchain experiment. ""This tool has access to a python REPL. "@@ -64,9 +64,9 @@ def _import_python_tool_PythonREPLTool() -> Any:def __getattr__(name: str) -> Any:if name == "PythonAstREPLTool":- return _import_python_tool_PythonAstREPLTool()+ return _import_python_tool_python_ast_repl_tool()if name == "PythonREPLTool":- return _import_python_tool_PythonREPLTool()+ return _import_python_tool_python_repl_tool()from langchain_community import tools# If not in interactive env, raise warning.diff --git a/libs/langchain/pyproject.toml b/libs/langchain/pyproject.tomlindex ba52ac1ed8..d7b0f14959 100644--- a/libs/langchain/pyproject.toml+++ b/libs/langchain/pyproject.toml@@ -168,7 +168,6 @@ ignore = ["D3","D4","ERA",- "N","PLC0415", # pylint: import-outside-top-level"TRY301", # tryceratops: raise-within-try]@@ -180,7 +179,7 @@ unfixable = [flake8-annotations.allow-star-arg-any = trueflake8-annotations.mypy-init-return = trueflake8-type-checking.runtime-evaluated-base-classes = ["pydantic.BaseModel","langchain_core.load.serializable.Serializable","langchain_core.runnables.base.RunnableSerializable"]-pep8-naming.classmethod-decorators = [ "classmethod", "langchain_core.utils.pydantic.pre_init", "pydantic.field_validator", "pydantic.v1.root_validator",]+pep8-naming.classmethod-decorators = [ "classmethod", "langchain_core.utils.pre_init", "pydantic.field_validator", "pydantic.v1.root_validator",]pydocstyle.convention = "google"pyupgrade.keep-runtime-typing = truediff --git a/libs/langchain/tests/integration_tests/chat_models/test_base.py b/libs/langchain/tests/integration_tests/chat_models/test_base.pyindex 05d3e00938..21c33aaa99 100644--- a/libs/langchain/tests/integration_tests/chat_models/test_base.py+++ b/libs/langchain/tests/integration_tests/chat_models/test_base.py@@ -11,7 +11,7 @@ from pydantic import BaseModelfrom langchain.chat_models import init_chat_model-class multiply(BaseModel):+class Multiply(BaseModel):"""Product of two ints."""x: int@@ -21,7 +21,7 @@ class multiply(BaseModel):@pytest.mark.requires("langchain_openai", "langchain_anthropic")async def test_init_chat_model_chain() -> None:model = init_chat_model("gpt-4o", configurable_fields="any", config_prefix="bar")- model_with_tools = model.bind_tools([multiply])+ model_with_tools = model.bind_tools([Multiply])model_with_config = model_with_tools.with_config(RunnableConfig(tags=["foo"]),diff --git a/libs/langchain/tests/unit_tests/chains/test_sequential.py b/libs/langchain/tests/unit_tests/chains/test_sequential.pyindex aba6df3761..d63fff0ba5 100644--- a/libs/langchain/tests/unit_tests/chains/test_sequential.py+++ b/libs/langchain/tests/unit_tests/chains/test_sequential.py@@ -203,8 +203,10 @@ def test_simple_sequential_functionality() -> None:assert output == expected_output[email protected]("isAsync", [False, True])-async def test_simple_sequential_functionality_with_callbacks(*, isAsync: bool) -> None:[email protected]("is_async", [False, True])+async def test_simple_sequential_functionality_with_callbacks(+ *, is_async: bool+) -> None:"""Test simple sequential functionality."""handler_1 = FakeCallbackHandler()handler_2 = FakeCallbackHandler()@@ -225,7 +227,7 @@ async def test_simple_sequential_functionality_with_callbacks(*, isAsync: bool)callbacks=[handler_3],)chain = SimpleSequentialChain(chains=[chain_1, chain_2, chain_3])- if isAsync:+ if is_async:output = await chain.ainvoke({"input": "123"})else:output = chain({"input": "123"})diff --git a/libs/langchain/tests/unit_tests/evaluation/comparison/test_eval_chain.py b/libs/langchain/tests/unit_tests/evaluation/comparison/test_eval_chain.pyindex cf1df2509b..369d88b56f 100644--- a/libs/langchain/tests/unit_tests/evaluation/comparison/test_eval_chain.py+++ b/libs/langchain/tests/unit_tests/evaluation/comparison/test_eval_chain.py@@ -27,7 +27,7 @@ def test_resolve_criteria_list_enum() -> None:assert set(val.keys()) == {c.value for c in list(Criteria)}-def test_PairwiseStringResultOutputParser_parse() -> None:+def test_pairwise_string_result_output_parser_parse() -> None:output_parser = PairwiseStringResultOutputParser()text = """I like pie better than cake.[[A]]"""diff --git a/libs/langchain/tests/unit_tests/evaluation/criteria/test_eval_chain.py b/libs/langchain/tests/unit_tests/evaluation/criteria/test_eval_chain.pyindex 9a8dc153bc..1401723d49 100644--- a/libs/langchain/tests/unit_tests/evaluation/criteria/test_eval_chain.py+++ b/libs/langchain/tests/unit_tests/evaluation/criteria/test_eval_chain.py@@ -51,7 +51,7 @@ The criterion is: "Do you like cake?" I like cake.""", # noqa: E501),],)-def test_CriteriaResultOutputParser_parse(text: str, want: dict) -> None:+def test_criteria_result_output_parser_parse(text: str, want: dict) -> None:output_parser = CriteriaResultOutputParser()got = output_parser.parse(text)assert got.get("reasoning") == want["reasoning"]diff --git a/libs/langchain/tests/unit_tests/evaluation/scoring/test_eval_chain.py b/libs/langchain/tests/unit_tests/evaluation/scoring/test_eval_chain.pyindex 932b708c41..67fd54d586 100644--- a/libs/langchain/tests/unit_tests/evaluation/scoring/test_eval_chain.py+++ b/libs/langchain/tests/unit_tests/evaluation/scoring/test_eval_chain.py@@ -12,7 +12,7 @@ from langchain.evaluation.scoring.eval_chain import (from tests.unit_tests.llms.fake_llm import FakeLLM-def test_PairwiseStringResultOutputParser_parse() -> None:+def test_pairwise_string_result_output_parser_parse() -> None:output_parser = ScoreStringResultOutputParser()text = """This answer is really good.Rating: [[10]]"""