Skip to content

Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate#5211

Open
LEDazzio01 wants to merge 4 commits intomicrosoft:mainfrom
LEDazzio01:feature/agent-response-finish-reason-v2
Open

Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate#5211
LEDazzio01 wants to merge 4 commits intomicrosoft:mainfrom
LEDazzio01:feature/agent-response-finish-reason-v2

Conversation

@LEDazzio01
Copy link
Copy Markdown
Contributor

Add finish_reason to AgentResponse and AgentResponseUpdate

Motivation

Resolves the gap between ChatResponse/ChatResponseUpdate (which already have finish_reason) and the agent-level response types. This brings the Python SDK into parity with the .NET SDK's AgentResponseItem.FinishReason field.\n\nRelated: Supersedes #4958 (rebased cleanly from current main to resolve merge conflicts).\n\n## Changes\n\n### python/packages/core/agent_framework/_types.py\n\n1. AgentResponse.__init__ — Added finish_reason: FinishReasonLiteral | FinishReason | None = None parameter and self.finish_reason initialization.\n2. AgentResponseUpdate.__init__ — Same addition.\n3. _process_update() — Added an AgentResponse/AgentResponseUpdate isinstance block to propagate finish_reason from updates to the accumulated response (mirrors the existing ChatResponse block).\n4. map_chat_to_agent_update() — Forwards finish_reason from ChatResponseUpdate to AgentResponseUpdate.\n\n### python/packages/core/tests/core/test_finish_reason.py (NEW)\n\nComprehensive unit tests covering:\n- AgentResponse and AgentResponseUpdate initialization with finish_reason\n- map_chat_to_agent_update forwarding\n- _process_update propagation (including None-safety)\n- Serialization round-trip via .to_dict()\n\n## Testing\n\n- [x] All new tests pass locally\n- [x] No changes to existing public API signatures (additive only)\n- [x] Backward compatible — finish_reason defaults to None\n\n## Checklist\n\n- [x] Followed Contribution Guidelines\n- [x] Unit tests added\n- [x] No breaking changes"

LEDazzio01 and others added 2 commits April 10, 2026 16:07
Add finish_reason field to AgentResponse and AgentResponseUpdate classes,
propagate it through _process_update() and map_chat_to_agent_update(),
and add comprehensive unit tests.

Fixes microsoft#4622
Copy link
Copy Markdown
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

Adds finish_reason support to agent-level response types in the Python SDK, aligning AgentResponse/AgentResponseUpdate with the existing chat response types and improving parity with the .NET SDK.

Changes:

  • Added finish_reason fields to AgentResponse and AgentResponseUpdate.
  • Propagated finish_reason during streaming update accumulation (_process_update) and when mapping ChatResponseUpdate -> AgentResponseUpdate.
  • Added unit tests validating initialization, mapping, propagation behavior, and serialization.

Reviewed changes

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

File Description
python/packages/core/agent_framework/_types.py Adds finish_reason to agent response types and ensures it is propagated during update processing and chat→agent update mapping.
python/packages/core/tests/core/test_finish_reason.py New tests covering finish_reason initialization, propagation, and serialization for agent response types.

Comment on lines +1 to +3
from agent_framework import (
AgentResponse,
AgentResponseUpdate,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

Add the standard copyright header at the top of this new test file to match the rest of python/packages/core/tests/core (nearly all existing test modules start with # Copyright (c) Microsoft. All rights reserved.).

Copilot uses AI. Check for mistakes.
Comment on lines 2369 to 2376
self,
*,
messages: Message | Sequence[Message] | None = None,
response_id: str | None = None,
agent_id: str | None = None,
created_at: CreatedAtT | None = None,
finish_reason: FinishReasonLiteral | FinishReason | None = None,
usage_details: UsageDetails | None = None,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

finish_reason was added to AgentResponse.__init__, but the Keyword Args: docstring section below doesn’t document it. Please add a short description for finish_reason so the constructor docs stay in sync with the signature.

Copilot uses AI. Check for mistakes.
Comment on lines 2607 to 2613
author_name: str | None = None,
agent_id: str | None = None,
response_id: str | None = None,
message_id: str | None = None,
created_at: CreatedAtT | None = None,
finish_reason: FinishReasonLiteral | FinishReason | None = None,
continuation_token: ContinuationToken | None = None,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

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

finish_reason was added to AgentResponseUpdate.__init__, but the constructor docstring doesn’t mention it in Keyword Args:. Please document finish_reason (and what values mean, if applicable) to keep docs consistent with the new parameter.

Copilot uses AI. Check for mistakes.
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