From 4a58bf0a102756068d594c0a6375d719f44c1184 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Sep 2024 08:49:41 -0700 Subject: [PATCH 1/4] core: Fix "argument of type 'NoneType' is not iterable" error in LangChainTracer --- libs/core/langchain_core/tracers/schemas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/langchain_core/tracers/schemas.py b/libs/core/langchain_core/tracers/schemas.py index 5f6c8ed8c6b7d..92ade771f7bfe 100644 --- a/libs/core/langchain_core/tracers/schemas.py +++ b/libs/core/langchain_core/tracers/schemas.py @@ -135,7 +135,7 @@ class Run(BaseRunV2): @root_validator(pre=True) def assign_name(cls, values: dict) -> dict: """Assign name to the run.""" - if values.get("name") is None: + if values.get("name") is None and values["serialized"] is not None: if "name" in values["serialized"]: values["name"] = values["serialized"]["name"] elif "id" in values["serialized"]: From 6018b911b20aaa9c1e9fc65430b70ab9f0459d9d Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Sep 2024 08:58:16 -0700 Subject: [PATCH 2/4] Add default name --- libs/core/langchain_core/tracers/schemas.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/core/langchain_core/tracers/schemas.py b/libs/core/langchain_core/tracers/schemas.py index 92ade771f7bfe..8e3692d7ac1af 100644 --- a/libs/core/langchain_core/tracers/schemas.py +++ b/libs/core/langchain_core/tracers/schemas.py @@ -140,6 +140,8 @@ def assign_name(cls, values: dict) -> dict: values["name"] = values["serialized"]["name"] elif "id" in values["serialized"]: values["name"] = values["serialized"]["id"][-1] + if values.get("name") is None: + values["name"] = "Unnamed" if values.get("events") is None: values["events"] = [] return values From ccc74e449119dffad92a650eef2a0d134d578734 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Sep 2024 09:02:03 -0700 Subject: [PATCH 3/4] Add some missing names --- .../langchain_community/agents/openai_assistant/base.py | 4 ++-- libs/langchain/langchain/agents/openai_assistant/base.py | 4 ++-- libs/langchain/langchain/chains/llm.py | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/community/langchain_community/agents/openai_assistant/base.py b/libs/community/langchain_community/agents/openai_assistant/base.py index 136917eaceccf..d2b1b7c02ace3 100644 --- a/libs/community/langchain_community/agents/openai_assistant/base.py +++ b/libs/community/langchain_community/agents/openai_assistant/base.py @@ -301,7 +301,7 @@ def invoke( inheritable_metadata=config.get("metadata"), ) run_manager = callback_manager.on_chain_start( - dumpd(self), input, name=config.get("run_name") + dumpd(self), input, name=config.get("run_name") or self.get_name() ) files = _convert_file_ids_into_attachments(kwargs.get("file_ids", [])) @@ -437,7 +437,7 @@ async def ainvoke( inheritable_metadata=config.get("metadata"), ) run_manager = callback_manager.on_chain_start( - dumpd(self), input, name=config.get("run_name") + dumpd(self), input, name=config.get("run_name") or self.get_name() ) files = _convert_file_ids_into_attachments(kwargs.get("file_ids", [])) diff --git a/libs/langchain/langchain/agents/openai_assistant/base.py b/libs/langchain/langchain/agents/openai_assistant/base.py index e7d4d2bfa9c9b..0fbbbe85e8bf3 100644 --- a/libs/langchain/langchain/agents/openai_assistant/base.py +++ b/libs/langchain/langchain/agents/openai_assistant/base.py @@ -310,7 +310,7 @@ def invoke( inheritable_metadata=config.get("metadata"), ) run_manager = callback_manager.on_chain_start( - dumpd(self), input, name=config.get("run_name") + dumpd(self), input, name=config.get("run_name") or self.get_name() ) try: # Being run within AgentExecutor and there are tool outputs to submit. @@ -429,7 +429,7 @@ async def ainvoke( inheritable_metadata=config.get("metadata"), ) run_manager = callback_manager.on_chain_start( - dumpd(self), input, name=config.get("run_name") + dumpd(self), input, name=config.get("run_name") or self.get_name() ) try: # Being run within AgentExecutor and there are tool outputs to submit. diff --git a/libs/langchain/langchain/chains/llm.py b/libs/langchain/langchain/chains/llm.py index 29ba6b1772381..123cacc58e519 100644 --- a/libs/langchain/langchain/chains/llm.py +++ b/libs/langchain/langchain/chains/llm.py @@ -242,6 +242,7 @@ def apply( run_manager = callback_manager.on_chain_start( None, {"input_list": input_list}, + name=self.get_name(), ) try: response = self.generate(input_list, run_manager=run_manager) @@ -262,6 +263,7 @@ async def aapply( run_manager = await callback_manager.on_chain_start( None, {"input_list": input_list}, + name=self.get_name(), ) try: response = await self.agenerate(input_list, run_manager=run_manager) From 2e158af1c1148b03173bc933d38c9e7ae3fc19e6 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Tue, 17 Sep 2024 09:05:55 -0700 Subject: [PATCH 4/4] Fix test --- .../tests/unit_tests/callbacks/test_callback_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/community/tests/unit_tests/callbacks/test_callback_manager.py b/libs/community/tests/unit_tests/callbacks/test_callback_manager.py index dbc414f053bbf..9f7f53554a7c5 100644 --- a/libs/community/tests/unit_tests/callbacks/test_callback_manager.py +++ b/libs/community/tests/unit_tests/callbacks/test_callback_manager.py @@ -121,4 +121,4 @@ def test_callback_manager_configure_context_vars( assert cb.completion_tokens == 1 assert cb.total_cost > 0 wait_for_all_tracers() - assert LangChainTracer._persist_run_single.call_count == 1 # type: ignore + assert LangChainTracer._persist_run_single.call_count == 4 # type: ignore