Python: fix: clear service_session_id in _agent_wrapper when propagate_session=True#5875
Open
benke520 wants to merge 1 commit into
Open
Python: fix: clear service_session_id in _agent_wrapper when propagate_session=True#5875benke520 wants to merge 1 commit into
benke520 wants to merge 1 commit into
Conversation
…n=True When propagate_session=True, the child agent inherits the parent's service_session_id. After the parent's first LLM call, MAF auto-populates this from the Responses API conversation_id. The child sends it as previous_response_id which the server rejects because the parent's tool_call is still pending (400 error). This fix saves and clears service_session_id before calling the child agent and restores it in a finally block, preserving session.state sharing while isolating the server-side conversation pointer. Fixes microsoft#5874
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Python agent-as-tool session propagation issue where a child agent can inherit the parent’s session.service_session_id, causing invalid previous_response_id usage and 400 errors with Responses-style APIs.
Changes:
- Update
Agent.as_tool(... propagate_session=True)wrapper to temporarily clearsession.service_session_idduring the child agent run and restore it afterward. - Add tests covering save/clear/restore behavior (including restore on child error).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_agents.py | Clears and restores service_session_id around delegated run() when propagate_session=True. |
| python/packages/core/tests/core/test_agents.py | Adds tests validating service_session_id isolation/restoration behavior for agent-as-tool. |
Comments suppressed due to low confidence (1)
python/packages/core/agent_framework/_agents.py:577
- The finally block only restores service_session_id when the original value was non-None. If the parent session starts with service_session_id=None, the child run can still set session.service_session_id (e.g., from streaming conversation_id propagation) and it will leak back to the parent. Consider always restoring the original value (including None), using a sentinel to distinguish “no session” vs “saved None”.
finally:
if session is not None and saved_service_session_id is not None:
session.service_session_id = saved_service_session_id
Comment on lines
+556
to
+560
| saved_service_session_id = None | ||
| if session is not None and session.service_session_id is not None: | ||
| saved_service_session_id = session.service_session_id | ||
| session.service_session_id = None | ||
|
|
Comment on lines
+1075
to
+1079
| async def test_chat_agent_as_tool_propagate_session_no_service_session_id(client: SupportsChatGetResponse) -> None: | ||
| """Test that when service_session_id is None, no save/restore is needed.""" | ||
| agent = Agent(client=client, name="SubAgent", description="Sub agent") | ||
| tool = agent.as_tool(propagate_session=True) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When propagate_session=True, the child agent inherits the parent's session.service_session_id. After the parent's first LLM call, MAF auto-populates this from the Responses API's conversation_id. The child sends it as previous_response_id, which the server rejects because the parent's ool_call is still pending — resulting in a 400 error.
Changes
Tests
Added 3 new tests:
All 106 tests in est_agents.py pass.
Fixes #5874