-
Notifications
You must be signed in to change notification settings - Fork 302
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
bug: Response duplication when using ChatAnthropic with langgraph #1227
Comments
diff --git a/packages/react-langgraph/src/useLangGraphRuntime.ts b/packages/react-langgraph/src/useLangGraphRuntime.ts
index 5b2b5ae0..2da681e3 100644
--- a/packages/react-langgraph/src/useLangGraphRuntime.ts
+++ b/packages/react-langgraph/src/useLangGraphRuntime.ts
@@ -92,6 +92,34 @@ export const useLangGraphRuntime = ({
stream,
});
+ // Add message deduplication logic with content-based comparison for AI messages
+ const dedupedMessages = messages.filter((msg, index, self) => {
+ // Find the first message that matches these criteria
+ return (
+ index ===
+ self.findIndex((m) => {
+ // For non-AI messages, compare type and ID
+ if (msg.type !== "ai") {
+ return m.type === msg.type && m.id === msg.id;
+ }
+
+ // For AI messages, compare content regardless of ID
+ if (m.type === "ai" && msg.type === "ai") {
+ const msgContent = Array.isArray(msg.content)
+ ? msg.content[0]?.text
+ : msg.content;
+ const mContent = Array.isArray(m.content)
+ ? m.content[0]?.text
+ : m.content;
+
+ return msgContent === mContent;
+ }
+
+ return false;
+ })
+ );
+ });
+
const [isRunning, setIsRunning] = useState(false);
const handleSendMessage = async (messages: LangChainMessage[]) => {
try {
@@ -106,7 +134,7 @@ export const useLangGraphRuntime = ({
const threadMessages = useExternalMessageConverter({
callback: convertLangchainMessages,
- messages,
+ messages: dedupedMessages,
isRunning,
}); For now I'm band-aiding with yalc above, but couldn't get to the root cause 🙏 |
@masato-io could you open the network tab in your browser, make a request, and share here the full response stream from langgraph? that should help me troubleshoot and fix this error Thanks a lot for reporting this! |
@Yonom of course, here is the stream comparison with with: const model = new ChatAnthropic({
model: "claude-3-5-sonnet-20241022",
}).bindTools(tools);
with: const model = new ChatOpenAI({
model: "gpt-4o",
}).bindTools(tools);
Please let me know if you need more additional information 🙏 Appreciate taking a look! |
Hey, it turns out that for anthropic, the "messages/partial" events have a different id than the final "messages/complete" event... ("run-9f1d3999-9fdc-40f0-bf37-e41dab043a8e" vs "msg_01CaPVGfmUsXUgzxFt3SAy7i") I'll check with the LangChain team to see how to handle this |
Gotcha, that makes sense; thank you for pinpointing the root cause and appreciate you checking with LangChain team! I'll explore if I can adjust from the LangGraph code as well. Please feel free to close this issue if you think this issue is more towards the LangChain side 👋 |
Closing as I was able to workaround it by extending |
Hi 👋 Thank you for creating this fantastic library! It looks promising. I had one follow-up question regarding langgraph integration using the Anthropic model.
I was following this guide:
https://www.assistant-ui.com/docs/runtimes/langgraph#new-project-from-template
with this langgraph starter:
https://github.com/langchain-ai/react-agent-js
It works great, and everything works as expected with the OpenAI model, such as:
But if I switch the model to:
Response from AI duplicates as attached image (there are two "Let me help..." blocks.):
Which blocks using assistant-ui with langgraph & Anthropic combination. Is there any way to fix this issue by any chance?
The text was updated successfully, but these errors were encountered: