Skip to content

Commit 5611117

Browse files
committed
Ensure that run managers are returned from handleLLMStart/handleChatModelStart always in the same order as the prompts passed in
What was happening was that the runManager was only added to the managers array after the callback handlers finished executing, so eg if the callback handlers for the second run finished first the runManager for prompt #2 would actually be in position #1 of the returned array.
1 parent fe7b388 commit 5611117

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

langchain/src/callbacks/manager.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ export class CallbackManager
348348
_parentRunId: string | undefined = undefined,
349349
extraParams: Record<string, unknown> | undefined = undefined
350350
): Promise<CallbackManagerForLLMRun[]> {
351-
const managers: CallbackManagerForLLMRun[] = [];
352-
353-
await Promise.all(
351+
return Promise.all(
354352
prompts.map(async (prompt) => {
355353
const runId = uuidv4();
356354

@@ -377,20 +375,16 @@ export class CallbackManager
377375
)
378376
);
379377

380-
managers.push(
381-
new CallbackManagerForLLMRun(
382-
runId,
383-
this.handlers,
384-
this.inheritableHandlers,
385-
this.tags,
386-
this.inheritableTags,
387-
this._parentRunId
388-
)
378+
return new CallbackManagerForLLMRun(
379+
runId,
380+
this.handlers,
381+
this.inheritableHandlers,
382+
this.tags,
383+
this.inheritableTags,
384+
this._parentRunId
389385
);
390386
})
391387
);
392-
393-
return managers;
394388
}
395389

396390
async handleChatModelStart(
@@ -400,9 +394,7 @@ export class CallbackManager
400394
_parentRunId: string | undefined = undefined,
401395
extraParams: Record<string, unknown> | undefined = undefined
402396
): Promise<CallbackManagerForLLMRun[]> {
403-
const managers: CallbackManagerForLLMRun[] = [];
404-
405-
await Promise.all(
397+
return Promise.all(
406398
messages.map(async (messageGroup) => {
407399
const runId = uuidv4();
408400

@@ -441,20 +433,16 @@ export class CallbackManager
441433
)
442434
);
443435

444-
managers.push(
445-
new CallbackManagerForLLMRun(
446-
runId,
447-
this.handlers,
448-
this.inheritableHandlers,
449-
this.tags,
450-
this.inheritableTags,
451-
this._parentRunId
452-
)
436+
return new CallbackManagerForLLMRun(
437+
runId,
438+
this.handlers,
439+
this.inheritableHandlers,
440+
this.tags,
441+
this.inheritableTags,
442+
this._parentRunId
453443
);
454444
})
455445
);
456-
457-
return managers;
458446
}
459447

460448
async handleChainStart(

0 commit comments

Comments
 (0)