Skip to content

Python: fix: set session_context._response before _run_after_providers (#4248)#4277

Open
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/4248-workflow-agent-session-history
Open

Python: fix: set session_context._response before _run_after_providers (#4248)#4277
LEDazzio01 wants to merge 1 commit intomicrosoft:mainfrom
LEDazzio01:fix/4248-workflow-agent-session-history

Conversation

@LEDazzio01
Copy link
Contributor

Summary\n\nFixes #4248WorkflowAgent was not saving workflow responses to session history.\n\n## Root Cause\n\nBaseHistoryProvider.after_run() reads output messages from context.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 regular Agent sets session_context._response = AgentResponse(...) before calling _run_after_providers, but WorkflowAgent._run_impl and _run_stream_impl called _run_after_providers without setting _response. The history provider found context.response == None and 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 file test_workflow_agent_session_history.py with 3 test cases:\n1. Non-streaming: Verifies session.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)

…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
Copilot AI review requested due to automatic review settings February 25, 2026 21:50
@github-actions github-actions bot changed the title fix: set session_context._response before _run_after_providers (#4248) Python: fix: set session_context._response before _run_after_providers (#4248) Feb 25, 2026
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

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._response before _run_after_providers in 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

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.

3 participants