fix(sdk): stale optimistic values due to history not being refetched (#1720)

This commit is contained in:
David Duong
2025-10-12 22:07:49 +02:00
committed by GitHub
parent 9ddfafc501
commit 47cdce771a
2 changed files with 29 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@langchain/langgraph-sdk": patch
---
Fix stale values being received by optimistic values callback in `stream.submit(...)`
+24 -19
View File
@@ -369,24 +369,35 @@ export function useStreamLGP<
: ""
);
stream.setStreamValues(() => {
if (submitOptions?.optimisticValues != null) {
return {
...historyValues,
...(typeof submitOptions.optimisticValues === "function"
? submitOptions.optimisticValues(historyValues)
: submitOptions.optimisticValues),
};
}
return { ...historyValues };
});
// When `fetchStateHistory` is requested, thus we assume that branching
// is enabled. We then need to include the implicit branch.
const includeImplicitBranch =
historyLimit === true || typeof historyLimit === "number";
const shouldRefetch =
// We're expecting the whole thread state in onFinish
options.onFinish != null ||
// We're fetching history, thus we need the latest checkpoint
// to ensure we're not accidentally submitting to a wrong branch
includeImplicitBranch;
stream.setStreamValues(() => {
const prev = shouldRefetch
? historyValues
: { ...historyValues, ...stream.values };
if (submitOptions?.optimisticValues != null) {
return {
...prev,
...(typeof submitOptions.optimisticValues === "function"
? submitOptions.optimisticValues(prev)
: submitOptions.optimisticValues),
};
}
return { ...prev };
});
let callbackMeta: RunCallbackMeta | undefined;
let rejoinKey: `lg:stream:${string}` | undefined;
let usableThreadId = threadId;
@@ -486,12 +497,6 @@ export function useStreamLGP<
async onSuccess() {
if (rejoinKey) runMetadataStorage?.removeItem(rejoinKey);
const shouldRefetch =
// We're expecting the whole thread state in onFinish
options.onFinish != null ||
// We're fetching history, thus we need the latest checkpoint
// to ensure we're not accidentally submitting to a wrong branch
includeImplicitBranch;
if (shouldRefetch) {
const newHistory = await history.mutate(usableThreadId!);