Skip to content

Commit

Permalink
fix(core): Make sure to return a callback manager if configure hooks (#…
Browse files Browse the repository at this point in the history
…7366)

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
ibolmo and jacoblee93 authored Dec 16, 2024
1 parent 715ef37 commit 64c2351
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
33 changes: 19 additions & 14 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,18 +1245,6 @@ export class CallbackManager
}
}
}
if (inheritableTags || localTags) {
if (callbackManager) {
callbackManager.addTags(inheritableTags ?? []);
callbackManager.addTags(localTags ?? [], false);
}
}
if (inheritableMetadata || localMetadata) {
if (callbackManager) {
callbackManager.addMetadata(inheritableMetadata ?? {});
callbackManager.addMetadata(localMetadata ?? {}, false);
}
}

for (const {
contextVar,
Expand All @@ -1276,12 +1264,29 @@ export class CallbackManager
handler = new (handlerClass as any)({});
}
if (handler !== undefined) {
if (!callbackManager?.handlers.some((h) => h.name === handler!.name)) {
callbackManager?.addHandler(handler, inheritable);
if (!callbackManager) {
callbackManager = new CallbackManager();
}

if (!callbackManager.handlers.some((h) => h.name === handler!.name)) {
callbackManager.addHandler(handler, inheritable);
}
}
}

if (inheritableTags || localTags) {
if (callbackManager) {
callbackManager.addTags(inheritableTags ?? []);
callbackManager.addTags(localTags ?? [], false);
}
}
if (inheritableMetadata || localMetadata) {
if (callbackManager) {
callbackManager.addMetadata(inheritableMetadata ?? {});
callbackManager.addMetadata(localMetadata ?? {}, false);
}
}

return callbackManager;
}
}
Expand Down
18 changes: 16 additions & 2 deletions langchain-core/src/callbacks/tests/manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-process-env */
import { expect, test, beforeAll, afterEach } from "@jest/globals";
import { afterEach, beforeAll, expect, test } from "@jest/globals";

import { setContextVariable, registerConfigureHook } from "../../context.js";
import { registerConfigureHook, setContextVariable } from "../../context.js";
import { BaseCallbackHandler } from "../base.js";
import { CallbackManager } from "../manager.js";

Expand All @@ -26,6 +26,20 @@ test("configure with empty array", async () => {
expect(manager?.handlers.length).toBe(0);
});

test("configure with no arguments", async () => {
const manager = CallbackManager.configure();
expect(manager).toBe(undefined);
});

test("configure with no arguments but with handler", async () => {
setContextVariable("my_test_handler", handlerInstance);
registerConfigureHook({
contextVar: "my_test_handler",
});
const manager = CallbackManager.configure();
expect(manager?.handlers[0]).toBe(handlerInstance);
});

test("configure with one handler", async () => {
const manager = CallbackManager.configure([handlerInstance]);
expect(manager?.handlers[0]).toBe(handlerInstance);
Expand Down

0 comments on commit 64c2351

Please sign in to comment.