diff --git a/langchain/callbacks/tracers/langchain.py b/langchain/callbacks/tracers/langchain.py index 9f734e29..1bdeb352 100644 --- a/langchain/callbacks/tracers/langchain.py +++ b/langchain/callbacks/tracers/langchain.py @@ -16,6 +16,16 @@ from langchain.env import get_runtime_environment from langchain.schema import BaseMessage, messages_to_dict logger = logging.getLogger(__name__) +_LOGGED = set() + + +def log_error_once(method: str, exception: Exception) -> None: + """Log an error once.""" + global _LOGGED + if (method, type(exception)) in _LOGGED: + return + _LOGGED.add((method, type(exception))) + logger.error(exception) class LangChainTracer(BaseTracer): @@ -76,11 +86,21 @@ class LangChainTracer(BaseTracer): 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("post", e) + raise 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("patch", e) + raise def _on_llm_start(self, run: Run) -> None: """Persist an LLM run."""