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

fix(core): Make sure to return a callback manager if configure hooks #7366

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions langchain-core/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,12 @@ 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this can potentially create a callback manager - let's move this whole block above where we add the inheritableTags and inheritableMetadata so that those are added properly

callbackManager = new CallbackManager();
}

if (!callbackManager.handlers.some((h) => h.name === handler!.name)) {
callbackManager.addHandler(handler, inheritable);
}
}
}
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