The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
core: Add ruff rules TRY (tryceratops) (#29388)
Christophe Bornetdbb6b7b103d9
diff --git a/libs/core/langchain_core/callbacks/manager.py b/libs/core/langchain_core/callbacks/manager.pyindex 1d06f4e6aa..f675b230c8 100644--- a/libs/core/langchain_core/callbacks/manager.py+++ b/libs/core/langchain_core/callbacks/manager.py@@ -127,7 +127,7 @@ def trace_as_chain_group(except Exception as e:if not group_cm.ended:run_manager.on_chain_error(e)- raise e+ raiseelse:if not group_cm.ended:run_manager.on_chain_end({})@@ -207,7 +207,7 @@ async def atrace_as_chain_group(except Exception as e:if not group_cm.ended:await run_manager.on_chain_error(e)- raise e+ raiseelse:if not group_cm.ended:await run_manager.on_chain_end({})@@ -289,7 +289,7 @@ def handle_event(f" {repr(e)}")if handler.raise_error:- raise e+ raisefinally:if coros:try:@@ -388,7 +388,7 @@ async def _ahandle_event_for_handler(f"Error in {handler.__class__.__name__}.{event_name} callback: {repr(e)}")if handler.raise_error:- raise e+ raiseasync def ahandle_event(diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.pyindex f3d94e49f0..6aaaf7d4ca 100644--- a/libs/core/langchain_core/language_models/chat_models.py+++ b/libs/core/langchain_core/language_models/chat_models.py@@ -268,7 +268,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):f"Invalid input type {type(input)}. ""Must be a PromptValue, str, or list of BaseMessages.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004def invoke(self,@@ -407,9 +407,6 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):generation = chunkelse:generation += chunk- if generation is None:- msg = "No generation chunks were returned"- raise ValueError(msg)except BaseException as e:run_manager.on_llm_error(e,@@ -417,9 +414,14 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):generations=[[generation]] if generation else []),)- raise e- else:- run_manager.on_llm_end(LLMResult(generations=[[generation]]))+ raise++ if generation is None:+ err = ValueError("No generation chunks were returned")+ run_manager.on_llm_error(err, response=LLMResult(generations=[]))+ raise err++ run_manager.on_llm_end(LLMResult(generations=[[generation]]))async def astream(self,@@ -485,19 +487,21 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):generation = chunkelse:generation += chunk- if generation is None:- msg = "No generation chunks were returned"- raise ValueError(msg)except BaseException as e:await run_manager.on_llm_error(e,response=LLMResult(generations=[[generation]] if generation else []),)- raise e- else:- await run_manager.on_llm_end(- LLMResult(generations=[[generation]]),- )+ raise++ if generation is None:+ err = ValueError("No generation chunks were returned")+ await run_manager.on_llm_error(err, response=LLMResult(generations=[]))+ raise err++ await run_manager.on_llm_end(+ LLMResult(generations=[[generation]]),+ )# --- Custom methods ---@@ -641,7 +645,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):except BaseException as e:if run_managers:run_managers[i].on_llm_error(e, response=LLMResult(generations=[]))- raise e+ raiseflattened_outputs = [LLMResult(generations=[res.generations], llm_output=res.llm_output) # type: ignore[list-item]for res in results@@ -1022,7 +1026,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):return generation.messageelse:msg = "Unexpected generation type"- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004async def _call_async(self,@@ -1039,7 +1043,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):return generation.messageelse:msg = "Unexpected generation type"- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004@deprecated("0.1.7", alternative="invoke", removal="1.0")def call_as_llm(@@ -1057,7 +1061,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):return result.contentelse:msg = "Cannot use predict when output is not a string."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004@deprecated("0.1.7", alternative="invoke", removal="1.0")def predict_messages(@@ -1082,7 +1086,7 @@ class BaseChatModel(BaseLanguageModel[BaseMessage], ABC):return result.contentelse:msg = "Cannot use predict when output is not a string."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004@deprecated("0.1.7", alternative="ainvoke", removal="1.0")async def apredict_messages(diff --git a/libs/core/langchain_core/language_models/fake_chat_models.py b/libs/core/langchain_core/language_models/fake_chat_models.pyindex a77a12d9d1..9bd62f1267 100644--- a/libs/core/langchain_core/language_models/fake_chat_models.py+++ b/libs/core/langchain_core/language_models/fake_chat_models.py@@ -242,7 +242,7 @@ class GenericFakeChatModel(BaseChatModel):f"Expected generate to return a ChatResult, "f"but got {type(chat_result)} instead.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004message = chat_result.generations[0].message@@ -251,7 +251,7 @@ class GenericFakeChatModel(BaseChatModel):f"Expected invoke to return an AIMessage, "f"but got {type(message)} instead.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004content = message.contentdiff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.pyindex c4867c6cbb..4ba16f5169 100644--- a/libs/core/langchain_core/language_models/llms.py+++ b/libs/core/langchain_core/language_models/llms.py@@ -337,7 +337,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):f"Invalid input type {type(input)}. ""Must be a PromptValue, str, or list of BaseMessages.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004def _get_ls_params(self,@@ -448,7 +448,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):if return_exceptions:return cast(list[str], [e for _ in inputs])else:- raise e+ raiseelse:batches = [inputs[i : i + max_concurrency]@@ -494,7 +494,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):if return_exceptions:return cast(list[str], [e for _ in inputs])else:- raise e+ raiseelse:batches = [inputs[i : i + max_concurrency]@@ -562,9 +562,6 @@ class BaseLLM(BaseLanguageModel[str], ABC):generation = chunkelse:generation += chunk- if generation is None:- msg = "No generation chunks were returned"- raise ValueError(msg)except BaseException as e:run_manager.on_llm_error(e,@@ -572,9 +569,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):generations=[[generation]] if generation else []),)- raise e- else:- run_manager.on_llm_end(LLMResult(generations=[[generation]]))+ raise++ if generation is None:+ err = ValueError("No generation chunks were returned")+ run_manager.on_llm_error(err, response=LLMResult(generations=[]))+ raise err++ run_manager.on_llm_end(LLMResult(generations=[[generation]]))async def astream(self,@@ -632,17 +634,19 @@ class BaseLLM(BaseLanguageModel[str], ABC):generation = chunkelse:generation += chunk- if generation is None:- msg = "No generation chunks were returned"- raise ValueError(msg)except BaseException as e:await run_manager.on_llm_error(e,response=LLMResult(generations=[[generation]] if generation else []),)- raise e- else:- await run_manager.on_llm_end(LLMResult(generations=[[generation]]))+ raise++ if generation is None:+ err = ValueError("No generation chunks were returned")+ await run_manager.on_llm_error(err, response=LLMResult(generations=[]))+ raise err++ await run_manager.on_llm_end(LLMResult(generations=[[generation]]))# --- Custom methods ---@@ -790,7 +794,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):except BaseException as e:for run_manager in run_managers:run_manager.on_llm_error(e, response=LLMResult(generations=[]))- raise e+ raiseflattened_outputs = output.flatten()for manager, flattened_output in zip(run_managers, flattened_outputs):manager.on_llm_end(flattened_output)@@ -850,7 +854,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):"Argument 'prompts' is expected to be of type List[str], received"f" argument of type {type(prompts)}.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004# Create callback managersif isinstance(metadata, list):metadata = [@@ -1036,7 +1040,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):for run_manager in run_managers])- raise e+ raiseflattened_outputs = output.flatten()await asyncio.gather(*[@@ -1289,7 +1293,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):f"{type(prompt)}. If you want to run the LLM on multiple prompts, use ""`generate` instead.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return (self.generate([prompt],diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.pyindex 074bbf9a56..c317bf099c 100644--- a/libs/core/langchain_core/messages/ai.py+++ b/libs/core/langchain_core/messages/ai.py@@ -363,6 +363,17 @@ class AIMessageChunk(AIMessage, BaseMessageChunk):return selftool_calls = []invalid_tool_calls = []++ def add_chunk_to_invalid_tool_calls(chunk: ToolCallChunk) -> None:+ invalid_tool_calls.append(+ create_invalid_tool_call(+ name=chunk["name"],+ args=chunk["args"],+ id=chunk["id"],+ error=None,+ )+ )+for chunk in self.tool_call_chunks:try:args_ = parse_partial_json(chunk["args"]) if chunk["args"] != "" else {} # type: ignore[arg-type]@@ -375,17 +386,9 @@ class AIMessageChunk(AIMessage, BaseMessageChunk):))else:- msg = "Malformed args."- raise ValueError(msg)+ add_chunk_to_invalid_tool_calls(chunk)except Exception:- invalid_tool_calls.append(- create_invalid_tool_call(- name=chunk["name"],- args=chunk["args"],- id=chunk["id"],- error=None,- )- )+ add_chunk_to_invalid_tool_calls(chunk)self.tool_calls = tool_callsself.invalid_tool_calls = invalid_tool_callsreturn selfdiff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.pyindex 2559cb12d8..f7d823bf80 100644--- a/libs/core/langchain_core/messages/utils.py+++ b/libs/core/langchain_core/messages/utils.py@@ -124,7 +124,7 @@ def get_buffer_string(role = m.roleelse:msg = f"Got unsupported message type: {m}"- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004message = f"{role}: {m.content}"if isinstance(m, AIMessage) and "function_call" in m.additional_kwargs:message += f"{m.additional_kwargs['function_call']}"@@ -1400,7 +1400,7 @@ def _get_message_openai_role(message: BaseMessage) -> str:return message.roleelse:msg = f"Unknown BaseMessage type {message.__class__}."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004def _convert_to_openai_tool_calls(tool_calls: list[ToolCall]) -> list[dict]:diff --git a/libs/core/langchain_core/output_parsers/openai_tools.py b/libs/core/langchain_core/output_parsers/openai_tools.pyindex 9b05ed813f..da1d8609fe 100644--- a/libs/core/langchain_core/output_parsers/openai_tools.py+++ b/libs/core/langchain_core/output_parsers/openai_tools.py@@ -282,19 +282,20 @@ class PydanticToolsParser(JsonOutputToolsParser):name_dict = {tool.__name__: tool for tool in self.tools}pydantic_objects = []for res in json_results:+ if not isinstance(res["args"], dict):+ if partial:+ continue+ msg = (+ f"Tool arguments must be specified as a dict, received: "+ f"{res['args']}"+ )+ raise ValueError(msg)try:- if not isinstance(res["args"], dict):- msg = (- f"Tool arguments must be specified as a dict, received: "- f"{res['args']}"- )- raise ValueError(msg)pydantic_objects.append(name_dict[res["type"]](**res["args"]))- except (ValidationError, ValueError) as e:+ except (ValidationError, ValueError):if partial:continue- else:- raise e+ raiseif self.first_tool_only:return pydantic_objects[0] if pydantic_objects else Noneelse:diff --git a/libs/core/langchain_core/output_parsers/pydantic.py b/libs/core/langchain_core/output_parsers/pydantic.pyindex 844fc9a703..a23bdc24aa 100644--- a/libs/core/langchain_core/output_parsers/pydantic.py+++ b/libs/core/langchain_core/output_parsers/pydantic.py@@ -66,10 +66,10 @@ class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):try:json_object = super().parse_result(result)return self._parse_obj(json_object)- except OutputParserException as e:+ except OutputParserException:if partial:return None- raise e+ raisedef parse(self, text: str) -> TBaseModel:"""Parse the output of an LLM call to a pydantic object.diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.pyindex 8133d3b368..11924e3328 100644--- a/libs/core/langchain_core/prompts/chat.py+++ b/libs/core/langchain_core/prompts/chat.py@@ -244,7 +244,7 @@ class MessagesPlaceholder(BaseMessagePromptTemplate):f"variable {self.variable_name} should be a list of base messages, "f"got {value} of type {type(value)}")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004value = convert_to_messages(value)if self.n_messages:value = value[-self.n_messages :]@@ -577,7 +577,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):return cls(prompt=prompt, **kwargs)else:msg = f"Invalid template: {template}"- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004@classmethoddef from_template_file(@@ -1225,7 +1225,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate):result.extend(message)else:msg = f"Unexpected input: {message_template}"- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return resultasync def aformat_messages(self, **kwargs: Any) -> list[BaseMessage]:@@ -1253,7 +1253,7 @@ class ChatPromptTemplate(BaseChatPromptTemplate):result.extend(message)else:msg = f"Unexpected input: {message_template}"- raise ValueError(msg)+ raise ValueError(msg) # noqa:TRY004return resultdef partial(self, **kwargs: Any) -> ChatPromptTemplate:@@ -1399,7 +1399,7 @@ def _create_template_from_message_type(var_name_wrapped, is_optional = templateif not isinstance(var_name_wrapped, str):msg = f"Expected variable name to be a string. Got: {var_name_wrapped}"- raise ValueError(msg)+ raise ValueError(msg) # noqa:TRY004if var_name_wrapped[0] != "{" or var_name_wrapped[-1] != "}":msg = (f"Invalid placeholder template: {var_name_wrapped}."diff --git a/libs/core/langchain_core/prompts/loading.py b/libs/core/langchain_core/prompts/loading.pyindex d6928c7271..da89972e75 100644--- a/libs/core/langchain_core/prompts/loading.py+++ b/libs/core/langchain_core/prompts/loading.py@@ -78,7 +78,7 @@ def _load_examples(config: dict) -> dict:config["examples"] = exampleselse:msg = "Invalid examples format. Only list or string are supported."- raise ValueError(msg)+ raise ValueError(msg) # noqa:TRY004return configdiff --git a/libs/core/langchain_core/retrievers.py b/libs/core/langchain_core/retrievers.pyindex 0f70c6e492..d1a5cb8cf9 100644--- a/libs/core/langchain_core/retrievers.py+++ b/libs/core/langchain_core/retrievers.py@@ -262,7 +262,7 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):result = self._get_relevant_documents(input, **_kwargs)except Exception as e:run_manager.on_retriever_error(e)- raise e+ raiseelse:run_manager.on_retriever_end(result,@@ -325,7 +325,7 @@ class BaseRetriever(RunnableSerializable[RetrieverInput, RetrieverOutput], ABC):result = await self._aget_relevant_documents(input, **_kwargs)except Exception as e:await run_manager.on_retriever_error(e)- raise e+ raiseelse:await run_manager.on_retriever_end(result,diff --git a/libs/core/langchain_core/runnables/fallbacks.py b/libs/core/langchain_core/runnables/fallbacks.pyindex 97d450015e..f932ce3589 100644--- a/libs/core/langchain_core/runnables/fallbacks.py+++ b/libs/core/langchain_core/runnables/fallbacks.py@@ -188,7 +188,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):last_error = eexcept BaseException as e:run_manager.on_chain_error(e)- raise e+ raiseelse:run_manager.on_chain_end(output)return output@@ -241,7 +241,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):last_error = eexcept BaseException as e:await run_manager.on_chain_error(e)- raise e+ raiseelse:await run_manager.on_chain_end(output)return output@@ -488,7 +488,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):last_error = eexcept BaseException as e:run_manager.on_chain_error(e)- raise e+ raiseelse:first_error = Nonebreak@@ -507,7 +507,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):output = Noneexcept BaseException as e:run_manager.on_chain_error(e)- raise e+ raiserun_manager.on_chain_end(output)async def astream(@@ -558,7 +558,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):last_error = eexcept BaseException as e:await run_manager.on_chain_error(e)- raise e+ raiseelse:first_error = Nonebreak@@ -577,7 +577,7 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):output = Noneexcept BaseException as e:await run_manager.on_chain_error(e)- raise e+ raiseawait run_manager.on_chain_end(output)def __getattr__(self, name: str) -> Any:diff --git a/libs/core/langchain_core/runnables/graph.py b/libs/core/langchain_core/runnables/graph.pyindex 53399a52fc..84b86994db 100644--- a/libs/core/langchain_core/runnables/graph.py+++ b/libs/core/langchain_core/runnables/graph.py@@ -50,9 +50,9 @@ def is_uuid(value: str) -> bool:"""try:UUID(value)- return Trueexcept ValueError:return False+ return Trueclass Edge(NamedTuple):diff --git a/libs/core/langchain_core/runnables/history.py b/libs/core/langchain_core/runnables/history.pyindex e6a88e4d22..d2040a3f3f 100644--- a/libs/core/langchain_core/runnables/history.py+++ b/libs/core/langchain_core/runnables/history.py@@ -481,7 +481,7 @@ class RunnableWithMessageHistory(RunnableBindingBase):f"Expected str, BaseMessage, List[BaseMessage], or Tuple[BaseMessage]. "f"Got {input_val}.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004def _get_output_messages(self, output_val: Union[str, BaseMessage, Sequence[BaseMessage], dict]@@ -517,7 +517,7 @@ class RunnableWithMessageHistory(RunnableBindingBase):f"Expected str, BaseMessage, List[BaseMessage], or Tuple[BaseMessage]. "f"Got {output_val}.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004def _enter_history(self, input: Any, config: RunnableConfig) -> list[BaseMessage]:hist: BaseChatMessageHistory = config["configurable"]["message_history"]diff --git a/libs/core/langchain_core/runnables/passthrough.py b/libs/core/langchain_core/runnables/passthrough.pyindex 9881def466..b0da175ae3 100644--- a/libs/core/langchain_core/runnables/passthrough.py+++ b/libs/core/langchain_core/runnables/passthrough.py@@ -474,7 +474,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):) -> dict[str, Any]:if not isinstance(input, dict):msg = "The input to RunnablePassthrough.assign() must be a dict."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return {**input,@@ -502,7 +502,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):) -> dict[str, Any]:if not isinstance(input, dict):msg = "The input to RunnablePassthrough.assign() must be a dict."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return {**input,@@ -555,7 +555,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):for chunk in for_passthrough:if not isinstance(chunk, dict):msg = "The input to RunnablePassthrough.assign() must be a dict."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004# remove mapper keys from passthrough chunk, to be overwritten by mapfiltered = AddableDict({k: v for k, v in chunk.items() if k not in mapper_keys}@@ -605,7 +605,7 @@ class RunnableAssign(RunnableSerializable[dict[str, Any], dict[str, Any]]):async for chunk in for_passthrough:if not isinstance(chunk, dict):msg = "The input to RunnablePassthrough.assign() must be a dict."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004# remove mapper keys from passthrough chunk, to be overwritten by map outputfiltered = AddableDict(@@ -708,7 +708,7 @@ class RunnablePick(RunnableSerializable[dict[str, Any], dict[str, Any]]):def _pick(self, input: dict[str, Any]) -> Any:if not isinstance(input, dict):msg = "The input to RunnablePassthrough.assign() must be a dict."- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004if isinstance(self.keys, str):return input.get(self.keys)diff --git a/libs/core/langchain_core/runnables/utils.py b/libs/core/langchain_core/runnables/utils.pyindex 9c7afea107..75063c7db5 100644--- a/libs/core/langchain_core/runnables/utils.py+++ b/libs/core/langchain_core/runnables/utils.py@@ -397,9 +397,9 @@ def get_lambda_source(func: Callable) -> Optional[str]:tree = ast.parse(textwrap.dedent(code))visitor = GetLambdaSource()visitor.visit(tree)- return visitor.source if visitor.count == 1 else nameexcept (SyntaxError, TypeError, OSError, SystemError):return name+ return visitor.source if visitor.count == 1 else name@lru_cache(maxsize=256)@@ -440,10 +440,11 @@ def get_function_nonlocals(func: Callable) -> list[Any]:breakelse:values.append(vv)- return valuesexcept (SyntaxError, TypeError, OSError, SystemError):return []+ return values+def indent_lines_after_first(text: str, prefix: str) -> str:"""Indent all lines of text after the first line.diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.pyindex 625d532da3..f0833cdc24 100644--- a/libs/core/langchain_core/tools/base.py+++ b/libs/core/langchain_core/tools/base.py@@ -680,6 +680,7 @@ class ChildTool(BaseTool):content = Noneartifact = None+ status = "success"error_to_raise: Union[Exception, KeyboardInterrupt, None] = Nonetry:child_config = patch_config(config, callbacks=run_manager.get_child())@@ -699,26 +700,25 @@ class ChildTool(BaseTool):f"expected. Instead generated response of type: "f"{type(response)}.")- raise ValueError(msg)- content, artifact = response+ error_to_raise = ValueError(msg)+ else:+ content, artifact = responseelse:content = response- status = "success"except (ValidationError, ValidationErrorV1) as e:if not self.handle_validation_error:error_to_raise = eelse:content = _handle_validation_error(e, flag=self.handle_validation_error)- status = "error"+ status = "error"except ToolException as e:if not self.handle_tool_error:error_to_raise = eelse:content = _handle_tool_error(e, flag=self.handle_tool_error)- status = "error"+ status = "error"except (Exception, KeyboardInterrupt) as e:error_to_raise = e- status = "error"if error_to_raise:run_manager.on_tool_error(error_to_raise)@@ -789,6 +789,7 @@ class ChildTool(BaseTool):)content = Noneartifact = None+ status = "success"error_to_raise: Optional[Union[Exception, KeyboardInterrupt]] = Nonetry:tool_args, tool_kwargs = self._to_args_and_kwargs(tool_input, tool_call_id)@@ -816,26 +817,25 @@ class ChildTool(BaseTool):f"expected. Instead generated response of type: "f"{type(response)}.")- raise ValueError(msg)- content, artifact = response+ error_to_raise = ValueError(msg)+ else:+ content, artifact = responseelse:content = response- status = "success"except ValidationError as e:if not self.handle_validation_error:error_to_raise = eelse:content = _handle_validation_error(e, flag=self.handle_validation_error)- status = "error"+ status = "error"except ToolException as e:if not self.handle_tool_error:error_to_raise = eelse:content = _handle_tool_error(e, flag=self.handle_tool_error)- status = "error"+ status = "error"except (Exception, KeyboardInterrupt) as e:error_to_raise = e- status = "error"if error_to_raise:await run_manager.on_tool_error(error_to_raise)@@ -873,7 +873,7 @@ def _handle_validation_error(f"Got unexpected type of `handle_validation_error`. Expected bool, "f"str or callable. Received: {flag}")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return content@@ -893,7 +893,7 @@ def _handle_tool_error(f"Got unexpected type of `handle_tool_error`. Expected bool, str "f"or callable. Received: {flag}")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return contentdiff --git a/libs/core/langchain_core/tracers/evaluation.py b/libs/core/langchain_core/tracers/evaluation.pyindex b7bd4508a1..425c1c0122 100644--- a/libs/core/langchain_core/tracers/evaluation.py+++ b/libs/core/langchain_core/tracers/evaluation.py@@ -139,7 +139,7 @@ class EvaluatorCallbackHandler(BaseTracer):f"{evaluator.__class__.__name__}: {repr(e)}",exc_info=True,)- raise e+ raiseexample_id = str(run.reference_example_id)with self.lock:for res in eval_results:diff --git a/libs/core/langchain_core/utils/html.py b/libs/core/langchain_core/utils/html.pyindex b3ff3e5b30..f54e52024c 100644--- a/libs/core/langchain_core/utils/html.py+++ b/libs/core/langchain_core/utils/html.py@@ -97,8 +97,7 @@ def extract_sub_links(if continue_on_failure:logger.warning(f"Unable to load link {link}. Raised exception:\n\n{e}")continue- else:- raise e+ raiseresults = []for path in absolute_paths:diff --git a/libs/core/langchain_core/utils/mustache.py b/libs/core/langchain_core/utils/mustache.pyindex aa54e4ede6..ee2ed8f252 100644--- a/libs/core/langchain_core/utils/mustache.py+++ b/libs/core/langchain_core/utils/mustache.py@@ -53,13 +53,14 @@ def grab_literal(template: str, l_del: str) -> tuple[str, str]:# Look for the next tag and move the template to itliteral, template = template.split(l_del, 1)_CURRENT_LINE += literal.count("\n")- return (literal, template)# There are no more tags in the template?except ValueError:# Then the rest of the template is a literalreturn (template, "")+ return (literal, template)+def l_sa_check(template: str, literal: str, is_standalone: bool) -> bool:"""Do a preliminary check to see if a tag could be a standalone.diff --git a/libs/core/langchain_core/utils/usage.py b/libs/core/langchain_core/utils/usage.pyindex ce198e268b..38c38209a5 100644--- a/libs/core/langchain_core/utils/usage.py+++ b/libs/core/langchain_core/utils/usage.py@@ -33,5 +33,5 @@ def _dict_int_op(msg = (f"Unknown value types: {types}. Only dict and int values are supported.")- raise ValueError(msg)+ raise ValueError(msg) # noqa: TRY004return combineddiff --git a/libs/core/langchain_core/vectorstores/utils.py b/libs/core/langchain_core/vectorstores/utils.pyindex add5e2ef4a..6e407d4492 100644--- a/libs/core/langchain_core/vectorstores/utils.py+++ b/libs/core/langchain_core/vectorstores/utils.py@@ -54,11 +54,6 @@ def _cosine_similarity(x: Matrix, y: Matrix) -> np.ndarray:raise ValueError(msg)try:import simsimd as simd # type: ignore-- x = np.array(x, dtype=np.float32)- y = np.array(y, dtype=np.float32)- z = 1 - np.array(simd.cdist(x, y, metric="cosine"))- return zexcept ImportError:logger.debug("Unable to import simsimd, defaulting to NumPy implementation. If you want "@@ -72,6 +67,10 @@ def _cosine_similarity(x: Matrix, y: Matrix) -> np.ndarray:similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0return similarity+ x = np.array(x, dtype=np.float32)+ y = np.array(y, dtype=np.float32)+ return 1 - np.array(simd.cdist(x, y, metric="cosine"))+def maximal_marginal_relevance(query_embedding: np.ndarray,diff --git a/libs/core/pyproject.toml b/libs/core/pyproject.tomlindex a14282b847..f9b731c730 100644--- a/libs/core/pyproject.toml+++ b/libs/core/pyproject.toml@@ -44,7 +44,7 @@ python = ">=3.12.4"[tool.poetry.extras][tool.ruff.lint]-select = [ "ASYNC", "B", "C4", "COM", "DJ", "E", "EM", "EXE", "F", "FLY", "FURB", "I", "ICN", "INT", "LOG", "N", "NPY", "PD", "PIE", "Q", "RSE", "S", "SIM", "SLOT", "T10", "T201", "TID", "UP", "W", "YTT",]+select = [ "ASYNC", "B", "C4", "COM", "DJ", "E", "EM", "EXE", "F", "FLY", "FURB", "I", "ICN", "INT", "LOG", "N", "NPY", "PD", "PIE", "Q", "RSE", "S", "SIM", "SLOT", "T10", "T201", "TID", "TRY", "UP", "W", "YTT",]ignore = [ "COM812", "UP007", "S110", "S112",][tool.coverage.run]diff --git a/libs/core/tests/unit_tests/runnables/test_fallbacks.py b/libs/core/tests/unit_tests/runnables/test_fallbacks.pyindex ec1d516587..1826f883e7 100644--- a/libs/core/tests/unit_tests/runnables/test_fallbacks.py+++ b/libs/core/tests/unit_tests/runnables/test_fallbacks.py@@ -103,7 +103,7 @@ def _runnable(inputs: dict) -> str:if inputs["text"] == "bar":return "second"if isinstance(inputs["exception"], ValueError):- raise RuntimeError+ raise RuntimeError # noqa: TRY004return "third"diff --git a/libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py b/libs/core/tests/unit_tests/runnables/test_runnable_events_v1.pyindex 6168793247..59c5e765e2 100644--- a/libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py+++ b/libs/core/tests/unit_tests/runnables/test_runnable_events_v1.py@@ -1600,7 +1600,7 @@ async def test_event_stream_with_retry() -> None:def fail(inputs: str) -> None:"""Simple func."""msg = "fail"- raise Exception(msg)+ raise ValueError(msg)chain = RunnableLambda(success) | RunnableLambda(fail).with_retry(stop_after_attempt=1,diff --git a/libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py b/libs/core/tests/unit_tests/runnables/test_runnable_events_v2.pyindex 8e88eb11a6..698a4c4dda 100644--- a/libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py+++ b/libs/core/tests/unit_tests/runnables/test_runnable_events_v2.py@@ -1556,7 +1556,7 @@ async def test_event_stream_with_retry() -> None:def fail(inputs: str) -> None:"""Simple func."""msg = "fail"- raise Exception(msg)+ raise ValueError(msg)chain = RunnableLambda(success) | RunnableLambda(fail).with_retry(stop_after_attempt=1,@@ -1906,7 +1906,7 @@ async def test_runnable_with_message_history() -> None:return fn(*args, **kwargs)except Exception as e:raised_errors.append(e)- raise e+ raisereturn _get_output_messages@@ -2097,7 +2097,7 @@ class StreamingRunnable(Runnable[Input, Output]):final_output = Nonefor element in self.iterable:if isinstance(element, BaseException):- raise element+ raise element # noqa: TRY301yield elementif final_output is None:@@ -2409,10 +2409,10 @@ async def test_break_astream_events() -> None:self.started = Truetry:await asyncio.sleep(0.5)- return inputexcept asyncio.CancelledError:self.cancelled = Trueraise+ return inputdef reset(self) -> None:self.started = False@@ -2474,10 +2474,10 @@ async def test_cancel_astream_events() -> None:self.started = Truetry:await asyncio.sleep(0.5)- return inputexcept asyncio.CancelledError:self.cancelled = Trueraise+ return inputdef reset(self) -> None:self.started = False