Skip to content

Commit

Permalink
Use console.warn for swallowed errors in a callback handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Jul 24, 2024
1 parent cc76721 commit 21e439d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 20 deletions.
96 changes: 76 additions & 20 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
*
* ```
*/
Expand Down
18 changes: 18 additions & 0 deletions langchain-core/src/callbacks/tests/callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
Expand Down Expand Up @@ -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");
});

0 comments on commit 21e439d

Please sign in to comment.