Skip to content

fix(middleware): fall back to configurable thread_id in MemoryMiddleware (#1425)#1426

Open
ggnnggez wants to merge 2 commits intobytedance:mainfrom
ggnnggez:fix-memory-middleware-thread-id-fallback
Open

fix(middleware): fall back to configurable thread_id in MemoryMiddleware (#1425)#1426
ggnnggez wants to merge 2 commits intobytedance:mainfrom
ggnnggez:fix-memory-middleware-thread-id-fallback

Conversation

@ggnnggez
Copy link
Copy Markdown

@ggnnggez ggnnggez commented Mar 26, 2026

Summary

Fix issue #1425

Fix MemoryMiddleware.after_agent() so memory writes are not skipped on LangGraph
runs.wait calls when runtime.context does not contain thread_id.

What changed

  • keep reading thread_id from runtime.context first
  • fall back to config.configurable.thread_id when it is missing

Why

On direct LangGraph API POST /threads/{thread_id}/runs/wait calls, runtime.context may
not include thread_id. In that case, the previous implementation skipped memory updates
before enqueue.

Verification

  • reproduced the issue locally with make dev
  • confirmed the pre-fix behavior on POST /threads/{thread_id}/runs/wait:
    • MemoryMiddleware: No thread_id in context, skipping memory update
  • applied the fallback to config.configurable.thread_id
  • confirmed the post-fix behavior:
    • memory update was enqueued from MemoryMiddleware.after_agent()
    • the queued write job executed successfully
    • backend/.deer-flow/memory.json was written successfully

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 26, 2026

CLA assistant check
All committers have signed the CLA.

@WillemJiang WillemJiang requested a review from Copilot March 27, 2026 06:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes DeerFlow’s MemoryMiddleware.after_agent() so memory updates are still enqueued for direct LangGraph runs.wait calls where runtime.context may not include thread_id, by falling back to LangGraph config’s configurable.thread_id.

Changes:

  • Import LangGraph get_config and use it as a fallback source for thread_id when runtime.context lacks it.
  • Keep the existing behavior of preferring runtime.context["thread_id"] when present.

config_data = get_config()
thread_id = config_data.get("configurable", {}).get("thread_id")
if not thread_id:
print("MemoryMiddleware: No thread_id in context, skipping memory update")
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The skip log message is now potentially misleading: if thread_id is missing in both runtime.context and config.configurable, it still says "No thread_id in context". Consider updating the message to mention both sources (context and config.configurable) to make debugging runs.wait vs other call paths clearer.

Suggested change
print("MemoryMiddleware: No thread_id in context, skipping memory update")
print(
"MemoryMiddleware: No thread_id found in runtime context or config.configurable, "
"skipping memory update"
)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants