From 21e439dffa4eb94a9b1eba6b3557f41d4d3a4aea Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 24 Jul 2024 10:13:16 -0700 Subject: [PATCH] Use console.warn for swallowed errors in a callback handler --- langchain-core/src/callbacks/manager.ts | 96 +++++++++++++++---- .../src/callbacks/tests/callbacks.test.ts | 18 ++++ 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/langchain-core/src/callbacks/manager.ts b/langchain-core/src/callbacks/manager.ts index 6e784c0f90f0..a28ba3f9a1e3 100644 --- a/langchain-core/src/callbacks/manager.ts +++ b/langchain-core/src/callbacks/manager.ts @@ -27,7 +27,8 @@ if ( /* #__PURE__ */ getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") !== "true" ) { - /* #__PURE__ */ console.warn( + const logFunction = handler.raiseError ? console.error : console.warn; + /* #__PURE__ */ logFunction( [ "[WARN]: You have enabled LangSmith tracing without backgrounding callbacks.", "[WARN]: If you are not using a serverless environment where you must wait for tracing calls to finish,", @@ -140,7 +141,10 @@ export class BaseRunManager { this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleText: ${err}` ); if (handler.raiseError) { @@ -185,7 +189,10 @@ export class CallbackManagerForRetrieverRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleRetriever` ); if (handler.raiseError) { @@ -211,7 +218,10 @@ export class CallbackManagerForRetrieverRun this.tags ); } catch (error) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleRetrieverError: ${error}` ); if (handler.raiseError) { @@ -251,7 +261,10 @@ export class CallbackManagerForLLMRun fields ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}` ); if (handler.raiseError) { @@ -277,7 +290,10 @@ export class CallbackManagerForLLMRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleLLMError: ${err}` ); if (handler.raiseError) { @@ -303,7 +319,10 @@ export class CallbackManagerForLLMRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleLLMEnd: ${err}` ); if (handler.raiseError) { @@ -353,7 +372,10 @@ export class CallbackManagerForChainRun kwargs ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleChainError: ${err}` ); if (handler.raiseError) { @@ -386,7 +408,10 @@ export class CallbackManagerForChainRun kwargs ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleChainEnd: ${err}` ); if (handler.raiseError) { @@ -412,7 +437,10 @@ export class CallbackManagerForChainRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleAgentAction: ${err}` ); if (handler.raiseError) { @@ -438,7 +466,10 @@ export class CallbackManagerForChainRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleAgentEnd: ${err}` ); if (handler.raiseError) { @@ -481,7 +512,10 @@ export class CallbackManagerForToolRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleToolError: ${err}` ); if (handler.raiseError) { @@ -508,7 +542,10 @@ export class CallbackManagerForToolRun this.tags ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleToolEnd: ${err}` ); if (handler.raiseError) { @@ -640,7 +677,10 @@ export class CallbackManager runName ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleLLMStart: ${err}` ); if (handler.raiseError) { @@ -727,7 +767,10 @@ export class CallbackManager ); } } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleLLMStart: ${err}` ); if (handler.raiseError) { @@ -794,7 +837,10 @@ export class CallbackManager runName ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleChainStart: ${err}` ); if (handler.raiseError) { @@ -856,7 +902,10 @@ export class CallbackManager runName ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleToolStart: ${err}` ); if (handler.raiseError) { @@ -918,7 +967,10 @@ export class CallbackManager runName ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}` ); if (handler.raiseError) { @@ -963,7 +1015,10 @@ export class CallbackManager this.metadata ); } catch (err) { - console.error( + const logFunction = handler.raiseError + ? console.error + : console.warn; + logFunction( `Error in handler ${handler.constructor.name}, handleCustomEvent: ${err}` ); if (handler.raiseError) { @@ -1217,7 +1272,8 @@ export function ensureHandler( * ]; * * // Run the example - * processQuestions(questions).catch(console.error); +const logFunction = handler.raiseError ? console.error : console.warn; +* processQuestions(questions).catch(consolelogFunction; * * ``` */ diff --git a/langchain-core/src/callbacks/tests/callbacks.test.ts b/langchain-core/src/callbacks/tests/callbacks.test.ts index 58644caa40e1..37e9e5d84410 100644 --- a/langchain-core/src/callbacks/tests/callbacks.test.ts +++ b/langchain-core/src/callbacks/tests/callbacks.test.ts @@ -9,6 +9,7 @@ import type { ChainValues } from "../../utils/types/index.js"; import type { AgentAction, AgentFinish } from "../../agents.js"; import { BaseMessage, HumanMessage } from "../../messages/index.js"; import type { LLMResult } from "../../outputs.js"; +import { RunnableLambda } from "../../runnables/base.js"; class FakeCallbackHandler extends BaseCallbackHandler { name = `fake-${uuid.v4()}`; @@ -518,3 +519,20 @@ test("error handling in llm start", async () => { await manager.handleLLMStart(serialized, ["test"]); }).rejects.toThrowError(); }); + +test("chain should still run if a normal callback handler throws an error", async () => { + const chain = RunnableLambda.from(async () => "hello world"); + const res = await chain.invoke( + {}, + { + callbacks: [ + { + handleChainStart: () => { + throw new Error("Bad"); + }, + }, + ], + } + ); + expect(res).toEqual("hello world"); +});