Skip to content

fix: handle runtime.context being None in ThreadDataMiddleware and related middlewares#1825

Open
highland0971 wants to merge 1 commit intobytedance:mainfrom
highland0971:fix/thread-data-middleware-context-none
Open

fix: handle runtime.context being None in ThreadDataMiddleware and related middlewares#1825
highland0971 wants to merge 1 commit intobytedance:mainfrom
highland0971:fix/thread-data-middleware-context-none

Conversation

@highland0971
Copy link
Copy Markdown

Problem

When runtime.context is None (user-defined static context is empty), several middlewares throw errors:

  • ThreadDataMiddleware: AttributeError: NoneType object has no attribute get
  • uploads_middleware: _get_context_value infinite recursion
  • loop_detection_middleware: similar None check missing

Solution

  1. ThreadDataMiddleware: Fallback to get_config() when runtime.context is None
  2. uploads_middleware: Fix infinite recursion in _get_context_value
  3. loop_detection_middleware: Add None check for runtime.context

Testing

  • ✅ DeerFlow API health check passes
  • ✅ Test chat request returns AI response successfully
  • ✅ Thread data correctly returned

Files Changed

  • backend/packages/harness/deerflow/agents/middlewares/thread_data_middleware.py
  • backend/packages/harness/deerflow/agents/middlewares/uploads_middleware.py
  • backend/packages/harness/deerflow/agents/middlewares/loop_detection_middleware.py

Problem:
ThreadDataMiddleware.before_agent() assumes runtime.context is always
set and contains thread_id. However, runtime.context defaults to None
when no custom context is provided, causing AttributeError when
accessing runtime.context.get().

Root Cause:
In LangGraph API, runtime.context is user-defined static context and
defaults to None. The actual thread_id is stored in config.configurable
under the key thread_id (CONFIG_KEY_THREAD_ID).

Solution:
1. Check if runtime.context is not None before accessing
2. Fallback to get_config() to retrieve thread_id from config.configurable
3. Provide clear error message if thread_id is not found in either location

Testing:
- Verified with curl request to LangGraph API
- thread_data paths are correctly generated
- AI agent responds normally

Co-authored-by: 多慧 <duohui@openclaw.ai>
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


root seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@WillemJiang
Copy link
Copy Markdown
Collaborator

WillemJiang commented Apr 4, 2026

@highland0971, thanks for your contribution. Please click the CLA assistant button to sign the CLA and change the Chinese comments into English. There are some conflicts that need to be resolved.

@WillemJiang WillemJiang added the question Further information is requested label Apr 4, 2026
@highland0971
Copy link
Copy Markdown
Author

@highland0971, thanks for your contribution. Please click the CLA assistant button to sign the CLA and change the Chinese comments into English. There are some conflicts that need to be resolved.

CLA assistant button,在哪里点击?我在Github上有自己的邮箱,PR是通过OpenClaw来提交,有啥需要特别处理的吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

question Further information is requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants