File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
libs/langgraph/src/pregel Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -321,6 +321,31 @@ describe("StreamMessagesHandler", () => {
321
321
// Should clean up metadata
322
322
expect ( handler . metadatas [ runId ] ) . toBeUndefined ( ) ;
323
323
} ) ;
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
+ } ) ;
324
349
} ) ;
325
350
326
351
describe ( "handleLLMError" , ( ) => {
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ export class StreamMessagesHandler extends BaseCallbackHandler {
39
39
40
40
streamFn : ( streamChunk : StreamChunk ) => void ;
41
41
42
- metadatas : Record < string , Meta > = { } ;
42
+ metadatas : Record < string , Meta | undefined > = { } ;
43
43
44
44
seen : Record < string , BaseMessage > = { } ;
45
45
@@ -146,6 +146,9 @@ export class StreamMessagesHandler extends BaseCallbackHandler {
146
146
}
147
147
148
148
handleLLMEnd ( output : LLMResult , runId : string ) {
149
+ // Filter out runs that we do not have metadata for
150
+ if ( this . metadatas [ runId ] === undefined ) return ;
151
+
149
152
// In JS, non-streaming runs do not call handleLLMNewToken at the model level
150
153
if ( ! this . emittedChatModelRunIds [ runId ] ) {
151
154
const chatGeneration = output . generations ?. [ 0 ] ?. [ 0 ] as ChatGeneration ;
You can’t perform that action at this time.
0 commit comments