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

fix(core): Fix stream events bug when errors are thrown too quickly during iteration #7617

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,19 +914,48 @@ export abstract class Runnable<
// eslint-disable-next-line no-param-reassign
config.callbacks = copiedCallbacks;
}
const abortController = new AbortController();
// Call the runnable in streaming mode,
// add each chunk to the output stream
const outerThis = this;
async function consumeRunnableStream() {
try {
const runnableStream = await outerThis.stream(input, config);
let signal;
if (options?.signal) {
if ("any" in AbortSignal) {
// Use native AbortSignal.any() if available (Node 19+)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
signal = (AbortSignal as any).any([
abortController.signal,
options.signal,
]);
} else {
// Fallback for Node 18 and below - just use the provided signal
signal = options.signal;
// Ensure we still abort our controller when the parent signal aborts
options.signal.addEventListener(
"abort",
() => {
abortController.abort();
},
{ once: true }
);
}
} else {
signal = abortController.signal;
}
const runnableStream = await outerThis.stream(input, {
...config,
signal,
});
const tappedStream = eventStreamer.tapOutputIterable(
runId,
runnableStream
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of tappedStream) {
// Just iterate so that the callback handler picks up events
if (abortController.signal.aborted) break;
}
} finally {
await eventStreamer.finish();
Expand Down Expand Up @@ -959,6 +988,7 @@ export abstract class Runnable<
yield event;
}
} finally {
abortController.abort();
await runnableStreamConsumePromise;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2263,3 +2263,23 @@ test("Runnable streamEvents method should respect passed signal", async () => {
}
}).rejects.toThrowError();
});

test("streamEvents method handles errors", async () => {
let caughtError: unknown;
const model = new FakeListChatModel({
responses: ["abc"],
});

try {
// eslint-disable-next-line no-unreachable-loop
for await (const _ of model.streamEvents("Hello! Tell me about yourself.", {
version: "v2",
})) {
throw new Error("should catch this error");
}
} catch (e) {
caughtError = e;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((caughtError as any)?.message).toEqual("should catch this error");
});
Loading