Skip to content

Commit 0bade90

Browse files
authored
chore(core): bin p-retry (#9575)
1 parent 6c40d00 commit 0bade90

File tree

11 files changed

+694
-95
lines changed

11 files changed

+694
-95
lines changed

.changeset/cool-cobras-bathe.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+
bin p-retry

libs/langchain-core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"langsmith": "^0.3.64",
3939
"mustache": "^4.2.0",
4040
"p-queue": "^6.6.2",
41-
"p-retry": "^7.0.0",
4241
"uuid": "^10.0.0",
4342
"zod": "^3.25.76 || ^4"
4443
},

libs/langchain-core/src/runnables/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { z } from "zod/v3";
2-
import pRetry from "p-retry";
32
import { v4 as uuidv4 } from "uuid";
43

54
import {
@@ -27,6 +26,7 @@ import {
2726
isStreamEventsHandler,
2827
} from "../tracers/event_stream.js";
2928
import { Serializable } from "../load/serializable.js";
29+
import pRetry from "../utils/p-retry/index.js";
3030
import {
3131
IterableReadableStream,
3232
concat,

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import PQueueMod from "p-queue";
22

33
import { getAbortSignalError } from "./signal.js";
4-
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-
}
4+
import pRetry from "./p-retry/index.js";
175

186
const STATUS_NO_RETRY = [
197
400, // Bad Request
@@ -119,7 +107,6 @@ export class AsyncCaller {
119107
callable: T,
120108
...args: Parameters<T>
121109
): Promise<Awaited<ReturnType<T>>> {
122-
const pRetry = await getPRetry();
123110
return this.queue.add(
124111
() =>
125112
pRetry(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
Check if an error is a [Fetch network error](https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions)
3+
4+
@return Returns `true` if the given value is a Fetch network error, otherwise `false`.
5+
6+
@example
7+
```
8+
import isNetworkError from 'is-network-error';
9+
10+
async function getUnicorns() {
11+
try {
12+
const response = await fetch('unicorns.json');
13+
return await response.json();
14+
} catch (error) {
15+
if (isNetworkError(error)) {
16+
return localStorage.getItem('…');
17+
}
18+
19+
throw error;
20+
}
21+
}
22+
23+
console.log(await getUnicorns());
24+
```
25+
*/
26+
export default function isNetworkError(value: unknown): value is TypeError;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-disable */
2+
const objectToString = Object.prototype.toString;
3+
4+
const isError = (value) => objectToString.call(value) === "[object Error]";
5+
6+
const errorMessages = new Set([
7+
"network error", // Chrome
8+
"Failed to fetch", // Chrome
9+
"NetworkError when attempting to fetch resource.", // Firefox
10+
"The Internet connection appears to be offline.", // Safari 16
11+
"Network request failed", // `cross-fetch`
12+
"fetch failed", // Undici (Node.js)
13+
"terminated", // Undici (Node.js)
14+
" A network error occurred.", // Bun (WebKit)
15+
"Network connection lost", // Cloudflare Workers (fetch)
16+
]);
17+
18+
export default function isNetworkError(error) {
19+
const isValid =
20+
error &&
21+
isError(error) &&
22+
error.name === "TypeError" &&
23+
typeof error.message === "string";
24+
25+
if (!isValid) {
26+
return false;
27+
}
28+
29+
const { message, stack } = error;
30+
31+
// Safari 17+ has generic message but no stack for network errors
32+
if (message === "Load failed") {
33+
return (
34+
stack === undefined ||
35+
// Sentry adds its own stack trace to the fetch error, so also check for that
36+
"__sentry_captured__" in error
37+
);
38+
}
39+
40+
// Deno network errors start with specific text
41+
if (message.startsWith("error sending request for url")) {
42+
return true;
43+
}
44+
45+
// Standard network error messages
46+
return errorMessages.has(message);
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 commit comments

Comments
 (0)