From c2a7684e125929bf4f6ac75d77d9d291e48658ae Mon Sep 17 00:00:00 2001 From: vowelparrot <130414180+vowelparrot@users.noreply.github.com> Date: Mon, 12 Jun 2023 14:49:56 -0700 Subject: [PATCH 1/3] Log tracer errors --- langchain/callbacks/tracers/langchain.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/langchain/callbacks/tracers/langchain.py b/langchain/callbacks/tracers/langchain.py index 9f734e2983b5e..08d1b3d393b2d 100644 --- a/langchain/callbacks/tracers/langchain.py +++ b/langchain/callbacks/tracers/langchain.py @@ -1,6 +1,7 @@ """A Tracer implementation that records to LangChain endpoint.""" from __future__ import annotations +import functools import logging import os from concurrent.futures import ThreadPoolExecutor @@ -18,6 +19,12 @@ logger = logging.getLogger(__name__) +@functools.lru_cache(None) +def log_error_once(message: str) -> None: + """Log an error once.""" + logger.error(message) + + class LangChainTracer(BaseTracer): """An implementation of the SharedTracer that POSTS to the langchain endpoint.""" @@ -76,7 +83,12 @@ def _persist_run_single(self, run: Run) -> None: extra = run_dict.get("extra", {}) extra["runtime"] = get_runtime_environment() run_dict["extra"] = extra - run = self.client.create_run(**run_dict, session_name=self.session_name) + try: + run = self.client.create_run(**run_dict, session_name=self.session_name) + except Exception as e: + # Errors are swallowed by the thread executor so we need to log them here + log_error_once(str(e)) + raise def _update_run_single(self, run: Run) -> None: """Update a run.""" From ad4460678229d9c7b7c55547dcc8e3ad654c7111 Mon Sep 17 00:00:00 2001 From: vowelparrot <130414180+vowelparrot@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:08:02 -0700 Subject: [PATCH 2/3] add to update --- langchain/callbacks/tracers/langchain.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/langchain/callbacks/tracers/langchain.py b/langchain/callbacks/tracers/langchain.py index 08d1b3d393b2d..55f1229ca562d 100644 --- a/langchain/callbacks/tracers/langchain.py +++ b/langchain/callbacks/tracers/langchain.py @@ -92,7 +92,12 @@ def _persist_run_single(self, run: Run) -> None: def _update_run_single(self, run: Run) -> None: """Update a run.""" - self.client.update_run(run.id, **run.dict()) + try: + self.client.update_run(run.id, **run.dict()) + except Exception as e: + # Errors are swallowed by the thread executor so we need to log them here + log_error_once(str(e)) + raise def _on_llm_start(self, run: Run) -> None: """Persist an LLM run.""" From 271cdaab06798a1b61ee558d420906bcc43c7e93 Mon Sep 17 00:00:00 2001 From: vowelparrot <130414180+vowelparrot@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:23:29 -0700 Subject: [PATCH 3/3] key on (method, type) --- langchain/callbacks/tracers/langchain.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/langchain/callbacks/tracers/langchain.py b/langchain/callbacks/tracers/langchain.py index 55f1229ca562d..1bdeb352b071e 100644 --- a/langchain/callbacks/tracers/langchain.py +++ b/langchain/callbacks/tracers/langchain.py @@ -1,7 +1,6 @@ """A Tracer implementation that records to LangChain endpoint.""" from __future__ import annotations -import functools import logging import os from concurrent.futures import ThreadPoolExecutor @@ -17,12 +16,16 @@ from langchain.schema import BaseMessage, messages_to_dict logger = logging.getLogger(__name__) +_LOGGED = set() -@functools.lru_cache(None) -def log_error_once(message: str) -> None: +def log_error_once(method: str, exception: Exception) -> None: """Log an error once.""" - logger.error(message) + global _LOGGED + if (method, type(exception)) in _LOGGED: + return + _LOGGED.add((method, type(exception))) + logger.error(exception) class LangChainTracer(BaseTracer): @@ -87,7 +90,7 @@ def _persist_run_single(self, run: Run) -> None: run = self.client.create_run(**run_dict, session_name=self.session_name) except Exception as e: # Errors are swallowed by the thread executor so we need to log them here - log_error_once(str(e)) + log_error_once("post", e) raise def _update_run_single(self, run: Run) -> None: @@ -96,7 +99,7 @@ def _update_run_single(self, run: Run) -> None: self.client.update_run(run.id, **run.dict()) except Exception as e: # Errors are swallowed by the thread executor so we need to log them here - log_error_once(str(e)) + log_error_once("patch", e) raise def _on_llm_start(self, run: Run) -> None: