Skip to content

Commit

Permalink
Log tracer errors (#6066)
Browse files Browse the repository at this point in the history
Example (would log several times if not for the helper fn. Would emit no
logs due to mulithreading previously)

![image](https://github.com/hwchase17/langchain/assets/130414180/070d25ae-1f06-4487-9617-0a6f66f3f01e)
  • Loading branch information
vowelparrot authored Jun 13, 2023
1 parent b023f0c commit 2f00880
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions langchain/callbacks/tracers/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
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):
Expand Down Expand Up @@ -76,11 +86,21 @@ 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("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."""
Expand Down

0 comments on commit 2f00880

Please sign in to comment.