Python: fix: set session_context._response before _run_after_providers (#4248)#4277
Open
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: fix: set session_context._response before _run_after_providers (#4248)#4277LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…soft#4248)\n\nWorkflowAgent was not saving workflow responses to session history\nbecause session_context._response was never set before calling\n_run_after_providers. The InMemoryHistoryProvider (and any\nBaseHistoryProvider) reads context.response to determine which\noutput messages to persist.\n\nChanges:\n- Non-streaming (_run_impl): set session_context._response = result\n before calling _run_after_providers\n- Streaming (_run_stream_impl): collect updates into a list, build\n the response via AgentResponse.from_updates(), and set _response\n before calling _run_after_providers\n- Add test file with 3 test cases covering non-streaming, streaming,\n and multi-turn scenarios\n\nFixes microsoft#4248
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #4248 where WorkflowAgent was not saving workflow responses to session history. The root cause was that session_context._response was never set before calling _run_after_providers(), which meant the BaseHistoryProvider.after_run() method found context.response == None and only saved user input messages, skipping the assistant response messages.
Changes:
- Set
session_context._responsebefore_run_after_providersin both streaming and non-streaming execution paths - Added comprehensive test coverage for non-streaming, streaming, and multi-turn scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_workflows/_agent.py | Sets session_context._response before calling _run_after_providers in both _run_impl (non-streaming) and _run_stream_impl (streaming) to ensure history providers can persist output messages |
| python/packages/core/tests/workflow/test_workflow_agent_session_history.py | Adds comprehensive tests validating that WorkflowAgent correctly persists both user and assistant messages to session history in non-streaming, streaming, and multi-turn scenarios |
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\n\nFixes #4248 —
WorkflowAgentwas not saving workflow responses to session history.\n\n## Root Cause\n\nBaseHistoryProvider.after_run()reads output messages fromcontext.response:\n\npython\nif self.store_outputs and context.response and context.response.messages:\n messages_to_store.extend(context.response.messages)\n\n\nThe regularAgentsetssession_context._response = AgentResponse(...)before calling_run_after_providers, butWorkflowAgent._run_impland_run_stream_implcalled_run_after_providerswithout setting_response. The history provider foundcontext.response == Noneand saved no output messages.\n\n## Changes\n\n###_agent.py\n\nNon-streaming (_run_impl) — 1 line added:\npython\nresult = self._convert_workflow_events_to_agent_response(response_id, output_events)\nsession_context._response = result # <-- NEW\nawait self._run_after_providers(session=provider_session, context=session_context)\nreturn result\n\n\nStreaming (_run_stream_impl) — collect updates and build response:\npython\ncollected_updates: list[AgentResponseUpdate] = [] # <-- NEW\nasync for event in self._run_core(...):\n updates = self._convert_workflow_event_to_agent_response_updates(response_id, event)\n for update in updates:\n collected_updates.append(update) # <-- NEW\n yield update\nif collected_updates: # <-- NEW\n session_context._response = AgentResponse.from_updates(collected_updates) # <-- NEW\nawait self._run_after_providers(session=provider_session, context=session_context)\n\n\n### Tests\n\nNew test filetest_workflow_agent_session_history.pywith 3 test cases:\n1. Non-streaming: Verifiessession.state[\"in_memory\"][\"messages\"]contains both user and assistant messages\n2. Streaming: Same for the streaming path\n3. Multi-turn: Two successive runs accumulate all 4 messages (2 user + 2 assistant)