Skip to content

Python: Fix HandoffBuilder silently dropping context_provider during agent cloning#3721

Open
Copilot wants to merge 7 commits intomainfrom
copilot/fix-handoffbuilder-context-provider
Open

Python: Fix HandoffBuilder silently dropping context_provider during agent cloning#3721
Copilot wants to merge 7 commits intomainfrom
copilot/fix-handoffbuilder-context-provider

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

  • Review the bug in _handoff.py line 312 where context_providers= should be context_provider=
  • Create a test to validate the context_provider is properly preserved during agent cloning
  • Fix the parameter name from context_providers to context_provider in _clone_chat_agent method
  • Address code review feedback to improve test quality
  • Pass code review with no issues
  • Pass security scanning with no vulnerabilities
  • Fix linter error: Remove unused events variable from test
  • Fix formatting errors: Remove trailing whitespace

Summary

Successfully fixed the bug where HandoffBuilder was silently ignoring the context_provider due to using context_providers= (plural) instead of context_provider= (singular) when cloning agents. Added comprehensive test to prevent regression. Fixed linter error and formatting issues.

Security Summary

No security vulnerabilities were introduced or discovered during this change. The codeql_checker found 0 alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Python: [Bug]: HandoffBuilder ignores context_provider due to parameter name mismatch in agent cloning</issue_title>
<issue_description>### Description

What happened?

When using HandoffBuilder with agents that have a context_provider configured, the context provider is silently ignored during the workflow execution. The issue occurs in the agent cloning logic at line 315 of python/packages/orchestrations/agent_framework_orchestrations/_handoff.py in the HandoffAgentExecutor._clone_chat_agent() method.

The code incorrectly uses context_providers= (plural) when passing the context provider to the cloned ChatAgent, but the ChatAgent constructor expects context_provider= (singular).

What did you expect to happen?

When a ChatAgent is created with a context_provider and passed to HandoffBuilder, the context provider should be preserved and function correctly throughout the handoff workflow. The cloned agents should maintain the same context provider as the original agent.

Steps to reproduce the issue

  1. Create a ChatAgent with a context_provider (e.g., memory provider, RAG provider)
  2. Pass the agent to HandoffBuilder using .participants() or register_participants()
  3. Build and run the workflow
  4. Observe that the context provider functionality is not working (e.g., memory not being retrieved, RAG not being used)

Code Sample

import asyncio
from agent_framework import ChatAgent, Context, ContextProvider, HandoffBuilder
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential

# Create custom context provider
class SimpleContextProvider(ContextProvider):
    def __init__(self):
        self.called = False
    
    async def invoking(self, messages, **kwargs):
        self.called = True
        return Context(instructions="User name is John.")


async def run():
    client = AzureOpenAIChatClient(credential=AzureCliCredential())
    mem_provider = SimpleContextProvider()

    # Create agent with context provider
    agent = ChatAgent(
        name="test_agent",
        chat_client=client,
        instructions="You are an assistant.",
        context_provider=mem_provider
    )
    
    workflow = HandoffBuilder(participants=[agent]).with_start_agent(agent).build()
    
    # Run workflow - context provider is not used
    async for event in workflow.run_stream("What is my name?"):
        if hasattr(event, 'data') and hasattr(event.data, 'text'):
            print(event.data.text, end='', flush=True)
    
    print(f"\n\nContext provider called: {mem_provider.called}")


asyncio.run(run())

Bug location:
File: python/packages/orchestrations/agent_framework_orchestrations/_handoff.py
Line: 315

Current code (incorrect, context_providers parameter was renamed in PR #3139 ):

return ChatAgent(
    chat_client=agent.chat_client,
    id=agent.id,
    name=agent.name,
    description=agent.description,
    chat_message_store_factory=agent.chat_message_store_factory,
    context_providers=agent.context_provider,  
    middleware=middleware,
    default_options=cloned_options,
)

Error Messages / Stack Traces

No error is raised. This is a silent failure, the incorrect parameter name causes the context provider to be ignored without any warning, making the issue difficult to debug.

Package Versions

agent-framework-core: 1.0.0b260130

Python Version

Python 3.12.10

Additional Context

No response</issue_description>

<agent_instructions>Fix the bug described in the issue.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 6, 2026 11:08
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix context provider parameter mismatch in HandoffBuilder Python: Fix HandoffBuilder silently dropping context_provider during agent cloning Feb 6, 2026
@markwallace-microsoft
Copy link
Member

@copilot Fix this error

F841 Local variable events is assigned to but never used
--> tests/test_handoff.py:341:5
|
340 | # Run workflow with a simple message to trigger context provider
341 | events = await _drain(workflow.run("Test message", stream=True))
| ^^^^^^
342 |
343 | # Verify context provider was invoked during the workflow execution
|
help: Remove assignment to unused variable events

…ng_handoff

Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

@copilot Fix this error

F841 Local variable events is assigned to but never used
...

Fixed in commit cc8f127. Removed the unused events variable assignment - the workflow is now executed without storing the result since we only need to verify that provider_calls was populated.

@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/orchestrations/agent_framework_orchestrations
   _handoff.py3825785%104–105, 107, 136–137, 159–169, 171, 173, 175, 180, 280, 334, 359, 387, 395–396, 410, 461–462, 494, 541–543, 730, 737, 742, 829, 832, 841–844, 854, 859, 866, 872–875, 910, 915, 1112, 1115, 1123, 1141, 1148, 1223
TOTAL16583202487% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3913 225 💤 0 ❌ 0 🔥 1m 10s ⏱️

@markwallace-microsoft
Copy link
Member

@copilot Run the python pre-commit hook on the changed files to fix formatting errors

Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

@copilot Run the python pre-commit hook on the changed files to fix formatting errors

Fixed in commit (hash will be shown after push). Removed trailing whitespace from lines 325 and 342 in test_handoff.py.

@markwallace-microsoft markwallace-microsoft marked this pull request as ready for review February 6, 2026 15:30
Copilot AI review requested due to automatic review settings February 6, 2026 15:30
Copy link
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 a bug in the orchestrations handoff flow where HandoffBuilder-created agent clones silently lost their configured context_provider due to a parameter name mismatch, and adds a regression test to ensure context providers are preserved during cloning.

Changes:

  • Fix _clone_chat_agent to pass context_provider= (singular) to ChatAgent instead of the incorrect context_providers= argument.
  • Add a new orchestration test validating that a context provider is invoked when running a handoff workflow, confirming it survived cloning.

Reviewed changes

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

File Description
python/packages/orchestrations/agent_framework_orchestrations/_handoff.py Corrects the constructor kwarg so cloned ChatAgents retain context_provider.
python/packages/orchestrations/tests/test_handoff.py Adds regression coverage verifying context provider invocation during handoff workflow execution.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: HandoffBuilder ignores context_provider due to parameter name mismatch in agent cloning

3 participants