Skip to content

Commit 636b994

Browse files
authored
fix(core): use dynamic import for p-retry to support CJS (#9495)
1 parent 31240d4 commit 636b994

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

.changeset/long-mails-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/core": patch
3+
---
4+
5+
fix: use dynamic import for p-retry to support CommonJS environments

libs/langchain-core/src/utils/async_caller.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import pRetry from "p-retry";
21
import PQueueMod from "p-queue";
32

43
import { getAbortSignalError } from "./signal.js";
54

5+
// p-retry is ESM-only, so we use dynamic import for CJS compatibility.
6+
// The module is cached after first import, so subsequent calls are essentially free.
7+
// This approach is recommended by the p-retry author for async contexts:
8+
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
9+
let pRetryModule: typeof import("p-retry") | null = null;
10+
11+
async function getPRetry() {
12+
if (!pRetryModule) {
13+
pRetryModule = await import("p-retry");
14+
}
15+
return pRetryModule.default;
16+
}
17+
618
const STATUS_NO_RETRY = [
719
400, // Bad Request
820
401, // Unauthorized
@@ -103,10 +115,11 @@ export class AsyncCaller {
103115
}
104116

105117
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106-
call<A extends any[], T extends (...args: A) => Promise<any>>(
118+
async call<A extends any[], T extends (...args: A) => Promise<any>>(
107119
callable: T,
108120
...args: Parameters<T>
109121
): Promise<Awaited<ReturnType<T>>> {
122+
const pRetry = await getPRetry();
110123
return this.queue.add(
111124
() =>
112125
pRetry(

0 commit comments

Comments
 (0)