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

Ensure that run managers are returned from handleLLMStart/handleChatModelStart always in the same order as the prompts passed in #1779

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Changes from all 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
44 changes: 16 additions & 28 deletions langchain/src/callbacks/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ export class CallbackManager
_parentRunId: string | undefined = undefined,
extraParams: Record<string, unknown> | undefined = undefined
): Promise<CallbackManagerForLLMRun[]> {
const managers: CallbackManagerForLLMRun[] = [];

await Promise.all(
return Promise.all(
Copy link
Contributor

Choose a reason for hiding this comment

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

That's tricky. So the callback in Promise.map() is executed whenever that promise is actually finished but then the result of it all is still ordered?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the promises in promise.all are all executed simultaneously, all starting at the same time. But promise.all does guarantee that no matter what the order in which they finish the returned array will hold the return values of each in the order they were passed in.

prompts.map(async (prompt) => {
const runId = uuidv4();

Expand All @@ -377,20 +375,16 @@ export class CallbackManager
)
);

managers.push(
new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
)
return new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
);
})
);

return managers;
}

async handleChatModelStart(
Expand All @@ -400,9 +394,7 @@ export class CallbackManager
_parentRunId: string | undefined = undefined,
extraParams: Record<string, unknown> | undefined = undefined
): Promise<CallbackManagerForLLMRun[]> {
const managers: CallbackManagerForLLMRun[] = [];

await Promise.all(
return Promise.all(
messages.map(async (messageGroup) => {
const runId = uuidv4();

Expand Down Expand Up @@ -441,20 +433,16 @@ export class CallbackManager
)
);

managers.push(
new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
)
return new CallbackManagerForLLMRun(
runId,
this.handlers,
this.inheritableHandlers,
this.tags,
this.inheritableTags,
this._parentRunId
);
})
);

return managers;
}

async handleChainStart(
Expand Down