-
Notifications
You must be signed in to change notification settings - Fork 132
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(messages): reuse the first valid message ID for subsequent chunks #798
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8564,24 +8564,6 @@ graph TD; | |
tags: ["c_two_chat_model"], | ||
}, | ||
], | ||
[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. were these being emitted by mistake? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so? @jacoblee93 can clarify, but it doesn't make sense to emit the message twice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool yea def don't want to emit things twice, can you point me to the line that was causing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue happens on when we emit the actual chunk instead of a raw message: https://github.com/langchain-ai/langgraphjs/pull/798/files?w=1#diff-fdc54140c541e7ddfea663ff6751691ca2971772df39a299c904947b27873560R127 |
||
new _AnyIdAIMessageChunk({ | ||
content: "2", | ||
}), | ||
{ | ||
langgraph_step: 2, | ||
langgraph_node: "c_two", | ||
langgraph_triggers: ["c_one"], | ||
langgraph_path: [PULL, "c_two"], | ||
langgraph_checkpoint_ns: expect.stringMatching(/^p_two:.*\|c_two:.*/), | ||
__pregel_resuming: false, | ||
__pregel_task_id: expect.any(String), | ||
checkpoint_ns: expect.stringMatching(/^p_two:/), | ||
name: "c_two", | ||
tags: ["graph:step:2"], | ||
ls_stop: undefined, | ||
}, | ||
], | ||
[ | ||
new _AnyIdAIMessageChunk({ | ||
content: "x", | ||
|
@@ -8737,27 +8719,6 @@ graph TD; | |
}, | ||
], | ||
], | ||
[ | ||
"messages", | ||
[ | ||
new _AnyIdAIMessageChunk({ | ||
content: "2", | ||
}), | ||
{ | ||
langgraph_step: 2, | ||
langgraph_node: "c_two", | ||
langgraph_triggers: ["c_one"], | ||
langgraph_path: [PULL, "c_two"], | ||
langgraph_checkpoint_ns: | ||
expect.stringMatching(/^p_two:.*\|c_two:.*/), | ||
__pregel_resuming: false, | ||
__pregel_task_id: expect.any(String), | ||
checkpoint_ns: expect.stringMatching(/^p_two:/), | ||
tags: ["graph:step:2"], | ||
name: "c_two", | ||
}, | ||
], | ||
], | ||
[ | ||
"messages", | ||
[ | ||
|
@@ -9470,6 +9431,37 @@ graph TD; | |
expect(oneCount).toEqual(1); | ||
expect(twoCount).toEqual(0); | ||
}); | ||
|
||
it.each(["omit", "first-only", "always"] as const)( | ||
"`messages` inherits message ID - %p", | ||
async (streamMessageId) => { | ||
const checkpointer = await createCheckpointer(); | ||
|
||
const graph = new StateGraph(MessagesAnnotation) | ||
.addNode("one", async () => { | ||
const model = new FakeChatModel({ | ||
responses: [new AIMessage({ id: "123", content: "Output" })], | ||
streamMessageId, | ||
}); | ||
|
||
const invoke = await model.invoke([new HumanMessage("Input")]); | ||
return { messages: invoke }; | ||
}) | ||
.addEdge(START, "one") | ||
.compile({ checkpointer }); | ||
|
||
const messages = await gatherIterator( | ||
graph.stream( | ||
{ messages: [] }, | ||
{ configurable: { thread_id: "1" }, streamMode: "messages" } | ||
) | ||
); | ||
|
||
const messageIds = [...new Set(messages.map(([m]) => m.id))]; | ||
expect(messageIds).toHaveLength(1); | ||
if (streamMessageId !== "omit") expect(messageIds[0]).toBe("123"); | ||
} | ||
); | ||
} | ||
|
||
runPregelTests(() => new MemorySaverAssertImmutable()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this suffice instead of
==
? It seems like the original condition was checking for undefined as well, so this might be better to catch both cases.