Skip to content

Python: Added thread to AgentRunContext#1732

Merged
dmytrostruk merged 7 commits into
microsoft:mainfrom
dmytrostruk:thread-in-middleware
Oct 28, 2025
Merged

Python: Added thread to AgentRunContext#1732
dmytrostruk merged 7 commits into
microsoft:mainfrom
dmytrostruk:thread-in-middleware

Conversation

@dmytrostruk

Copy link
Copy Markdown
Member

Motivation and Context

Resolve: #1504

Added thread property to AgentRunContext class to be able to observe thread state in middleware before and after agent execution.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@dmytrostruk dmytrostruk self-assigned this Oct 27, 2025
Copilot AI review requested due to automatic review settings October 27, 2025 18:34
@dmytrostruk dmytrostruk added the python Usage: [Issues, PRs], Target: Python label Oct 27, 2025
@markwallace-microsoft markwallace-microsoft added the documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs label Oct 27, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a thread property to the AgentRunContext class, enabling middleware to observe and track thread state before and after agent execution. This addresses the need to access conversation history and monitor thread state changes across multiple agent runs.

Key Changes:

  • Added thread parameter to AgentRunContext to expose thread state to middleware
  • Updated middleware pipeline to pass thread context through the execution chain
  • Added comprehensive tests validating thread behavior across multiple agent runs

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
python/packages/core/agent_framework/_middleware.py Added thread parameter to AgentRunContext class and updated middleware pipeline to pass thread context
python/packages/core/tests/core/test_middleware.py Added unit tests for AgentRunContext initialization with thread and thread propagation through middleware pipeline
python/packages/core/tests/core/test_middleware_with_agent.py Added integration test demonstrating thread behavior across multiple agent runs with message accumulation
python/samples/getting_started/middleware/thread_behavior_middleware.py New sample demonstrating thread tracking in middleware across multiple runs
python/samples/getting_started/middleware/README.md Added documentation entry for new thread behavior middleware sample
python/samples/README.md Added reference to new thread behavior middleware sample

Comment thread python/packages/core/tests/core/test_middleware_with_agent.py
Comment thread python/samples/getting_started/middleware/thread_behavior_middleware.py Outdated
@markwallace-microsoft

markwallace-microsoft commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _middleware.py397997%803, 819, 866–867, 1070–1071, 1116, 1266, 1480
TOTAL12297200883% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
1408 98 💤 0 ❌ 0 🔥 27.729s ⏱️

Comment thread python/packages/lab/gaia/agent_framework_lab_gaia/gaia.py
@dmytrostruk dmytrostruk enabled auto-merge October 28, 2025 16:29
@dmytrostruk dmytrostruk added this pull request to the merge queue Oct 28, 2025
Merged via the queue into microsoft:main with commit adba312 Oct 28, 2025
29 checks passed
arisng pushed a commit to arisng/agent-framework that referenced this pull request Feb 2, 2026
* Added thread to agent run context

* Added sample

* Update python/samples/getting_started/middleware/thread_behavior_middleware.py

Co-authored-by: Copilot <[email protected]>

* Small fix

---------

Co-authored-by: Copilot <[email protected]>
@moonbox3 moonbox3 removed the lab label Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Middleware cannot see messages on thread

6 participants