Skip to content

Commit e48b6ca

Browse files
authored
fix(langgraph): avoid calling _emit for runs without metadata (#1340)
1 parent 27c9e81 commit e48b6ca

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

libs/langgraph/src/pregel/messages.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,31 @@ describe("StreamMessagesHandler", () => {
321321
// Should clean up metadata
322322
expect(handler.metadatas[runId]).toBeUndefined();
323323
});
324+
325+
it("should not emit when metadata is missing", () => {
326+
const streamFn = vi.fn();
327+
const handler = new StreamMessagesHandler(streamFn);
328+
329+
// Spy on _emit
330+
const emitSpy = vi.spyOn(handler, "_emit");
331+
332+
handler.handleLLMEnd(
333+
{
334+
generations: [
335+
[
336+
{
337+
text: "test output",
338+
message: new AIMessage({ content: "result" }),
339+
},
340+
],
341+
],
342+
} as unknown as LLMResult,
343+
"run-123"
344+
);
345+
346+
// Should not emit anything
347+
expect(emitSpy).not.toHaveBeenCalled();
348+
});
324349
});
325350

326351
describe("handleLLMError", () => {

libs/langgraph/src/pregel/messages.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class StreamMessagesHandler extends BaseCallbackHandler {
3939

4040
streamFn: (streamChunk: StreamChunk) => void;
4141

42-
metadatas: Record<string, Meta> = {};
42+
metadatas: Record<string, Meta | undefined> = {};
4343

4444
seen: Record<string, BaseMessage> = {};
4545

@@ -146,6 +146,9 @@ export class StreamMessagesHandler extends BaseCallbackHandler {
146146
}
147147

148148
handleLLMEnd(output: LLMResult, runId: string) {
149+
// Filter out runs that we do not have metadata for
150+
if (this.metadatas[runId] === undefined) return;
151+
149152
// In JS, non-streaming runs do not call handleLLMNewToken at the model level
150153
if (!this.emittedChatModelRunIds[runId]) {
151154
const chatGeneration = output.generations?.[0]?.[0] as ChatGeneration;

0 commit comments

Comments
 (0)