Skip to content

Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate - #4958

Closed
L. Elaine Dazzio (LEDazzio01) wants to merge 5 commits into
microsoft:mainfrom
LEDazzio01:feature/agent-response-finish-reason
Closed

Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate#4958
L. Elaine Dazzio (LEDazzio01) wants to merge 5 commits into
microsoft:mainfrom
LEDazzio01:feature/agent-response-finish-reason

Conversation

@LEDazzio01

Copy link
Copy Markdown
Contributor

Fixes #4622

This PR brings python agent responses closer to parity with the .NET SDK by supporting finish_reason. It correctly captures and propagates finish_reason through AgentResponse, AgentResponseUpdate, _process_update() merging logic, and the map_chat_to_agent_update() translation layer.

Changes Made

  • Added finish_reason to AgentResponse and AgentResponseUpdate constructors.
  • Added propagation logic to merge finish_reason correctly in _types.py:_process_update().
  • Forwarded finish_reason through the map_chat_to_agent_update() function.
  • Verified everything with comprehensive unit tests for initialization, mapping, merging, and serialization in test_finish_reason.py.

…GENTS.md

Fixes #4789

- Update python/packages/anthropic/AGENTS.md to document RawAnthropicClient
  and the layered client architecture
- Add Chat Client Layer Architecture section to python/packages/core/AGENTS.md
  explaining the standard layer ordering (FunctionInvocationLayer ->
  ChatMiddlewareLayer -> ChatTelemetryLayer -> Raw/Base), per-call middleware
  routing via client_kwargs, and raw vs public client pattern
Copilot AI review requested due to automatic review settings March 28, 2026 01:45
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Mar 28, 2026
@github-actions github-actions Bot changed the title Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate Mar 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds Python support for propagating finish_reason through agent response types to align behavior with the .NET SDK, and documents the standard public chat client layering model.

Changes:

  • Added finish_reason to AgentResponse and AgentResponseUpdate (init + update merge) and forwarded it in map_chat_to_agent_update().
  • Added unit tests covering init/mapping/merge/serialization scenarios for finish_reason.
  • Expanded AGENTS documentation to describe the public client layer stack and clarified Anthropic client types.

Reviewed changes

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

File Description
python/packages/core/tests/core/test_finish_reason.py New tests for finish_reason behavior across agent responses/updates.
python/packages/core/agent_framework/_types.py Plumbs finish_reason through AgentResponse/AgentResponseUpdate, _process_update(), and map_chat_to_agent_update().
python/packages/core/AGENTS.md Documents chat client layer architecture and raw vs public client responsibilities.
python/packages/anthropic/AGENTS.md Clarifies AnthropicClient vs RawAnthropicClient and documents their layer composition.

Comment thread python/packages/core/tests/core/test_finish_reason.py Outdated
Comment thread python/packages/core/tests/core/test_finish_reason.py Outdated
Comment thread python/packages/core/tests/core/test_finish_reason.py Outdated
Comment thread python/packages/core/tests/core/test_finish_reason.py Outdated
… serialization API

- Remove unused imports: `pytest`, `BaseModel` (F401), `ChatResponse` (F401)
- Replace `dataclasses.asdict()` with `.to_dict()` for both
  `AgentResponse` and `AgentResponseUpdate` serialization tests, since
  these are `SerializationMixin` classes, not dataclasses
@LEDazzio01

Copy link
Copy Markdown
Contributor Author

Addressed all 4 Copilot comments in 83255b2:

  1. ✅ Removed unused pytest import (F401)
  2. ✅ Removed unused BaseModel import (F401)
  3. ✅ Removed unused ChatResponse import (F401)
  4. ✅ Replaced dataclasses.asdict() with .to_dict() in both serialization tests — AgentResponse and AgentResponseUpdate are SerializationMixin classes, not dataclasses, so asdict() would raise TypeError

@eavanvalkenburg

Copy link
Copy Markdown
Member

L. Elaine Dazzio (@LEDazzio01) there are some merge conflicts, could you check those?

@LEDazzio01

Copy link
Copy Markdown
Contributor Author

Closing in favor of #5211 which rebases these changes cleanly onto the current main branch (resolving all merge conflicts).\n\nThe new PR contains the same feature — adding finish_reason to AgentResponse and AgentResponseUpdate — but applied to the updated _types.py structure.

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

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Feature]: Python: add finish reason to AgentResponse and AgentResponseUpdate

4 participants