From 64c23511162b8ae5d6e7b526d313ebe95510511e Mon Sep 17 00:00:00 2001 From: Olmo Maldonado Date: Mon, 16 Dec 2024 11:26:36 -0800 Subject: [PATCH] fix(core): Make sure to return a callback manager if configure hooks (#7366) Co-authored-by: jacoblee93 --- langchain-core/src/callbacks/manager.ts | 33 +++++++++++-------- .../src/callbacks/tests/manager.test.ts | 18 ++++++++-- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/langchain-core/src/callbacks/manager.ts b/langchain-core/src/callbacks/manager.ts index dcabf602b838..55ad0484e480 100644 --- a/langchain-core/src/callbacks/manager.ts +++ b/langchain-core/src/callbacks/manager.ts @@ -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, @@ -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; } } diff --git a/langchain-core/src/callbacks/tests/manager.test.ts b/langchain-core/src/callbacks/tests/manager.test.ts index 7e947049006f..12f32890f2a7 100644 --- a/langchain-core/src/callbacks/tests/manager.test.ts +++ b/langchain-core/src/callbacks/tests/manager.test.ts @@ -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"; @@ -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);