Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: Use console.warn for swallowed errors in a callback handler #6192

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 74 additions & 19 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,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 +188,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 +217,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 +260,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 +289,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 +318,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 +371,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 +407,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 +436,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 +465,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 +511,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 +541,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 +676,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 +766,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 +836,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 +901,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 +966,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 +1014,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 +1271,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");
});
Loading