Python: Feat: Add finish_reason support to AgentResponse and AgentResponseUpdate#5211
Conversation
There was a problem hiding this comment.
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_reasonfields toAgentResponseandAgentResponseUpdate. - Propagated
finish_reasonduring streaming update accumulation (_process_update) and when mappingChatResponseUpdate -> 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. |
|
and please rebase from main, there is a conflict |
3447c14 to
e19fc57
Compare
|
@eavanvalkenburg Done — both items addressed:
|
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
@LEDazzio01 lint is failing in core |
|
@moonbox3 Fixed in d29c620 — the |
|
Another fail, please run the same checks locally. Uv run poe typing -P core |
|
@eavanvalkenburg Fixed in c134789 — the pyright Ran |
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
…back Move all finish_reason test cases from the separate test_finish_reason.py file into test_types.py as requested by eavanvalkenburg. Tests are placed in a new '# region finish_reason' section at the end of the file.
Address PR review feedback from @eavanvalkenburg — ChatResponse and ChatResponseUpdate both use 'model', not 'model_id'.
Combine nested if statements for AgentResponse finish_reason check to satisfy ruff SIM102 rule, with line wrapping to stay under 120 chars.
Add type: ignore[arg-type] for FinishReason NewType widening when passing ChatResponseUpdate.finish_reason to AgentResponseUpdate. Matches existing patterns in the codebase (40+ similar ignores).
c134789 to
197a918
Compare
…ponseUpdate (#5211) * feat: add finish_reason support to AgentResponse and AgentResponseUpdate 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 #4622 * feat: add finish_reason to AgentResponse and AgentResponseUpdate * style: add copyright header to test_finish_reason.py * docs: add finish_reason to AgentResponse and AgentResponseUpdate docstrings * refactor: move finish_reason tests into test_types.py per review feedback Move all finish_reason test cases from the separate test_finish_reason.py file into test_types.py as requested by eavanvalkenburg. Tests are placed in a new '# region finish_reason' section at the end of the file. * fix: use model instead of model_id in _process_update Address PR review feedback from @eavanvalkenburg — ChatResponse and ChatResponseUpdate both use 'model', not 'model_id'. * fix: resolve SIM102 lint error in _process_update Combine nested if statements for AgentResponse finish_reason check to satisfy ruff SIM102 rule, with line wrapping to stay under 120 chars. * fix: resolve pyright reportArgumentType in map_chat_to_agent_update Add type: ignore[arg-type] for FinishReason NewType widening when passing ChatResponseUpdate.finish_reason to AgentResponseUpdate. Matches existing patterns in the codebase (40+ similar ignores).
Add
finish_reasontoAgentResponseandAgentResponseUpdateMotivation
Resolves the gap between
ChatResponse/ChatResponseUpdate(which already havefinish_reason) and the agent-level response types. This brings the Python SDK into parity with the .NET SDK'sAgentResponseItem.FinishReasonfield.\n\nRelated: Supersedes #4958 (rebased cleanly from currentmainto resolve merge conflicts).\n\n## Changes\n\n###python/packages/core/agent_framework/_types.py\n\n1.AgentResponse.__init__— Addedfinish_reason: FinishReasonLiteral | FinishReason | None = Noneparameter andself.finish_reasoninitialization.\n2.AgentResponseUpdate.__init__— Same addition.\n3._process_update()— Added anAgentResponse/AgentResponseUpdateisinstance block to propagatefinish_reasonfrom updates to the accumulated response (mirrors the existingChatResponseblock).\n4.map_chat_to_agent_update()— Forwardsfinish_reasonfromChatResponseUpdatetoAgentResponseUpdate.\n\n###python/packages/core/tests/core/test_finish_reason.py(NEW)\n\nComprehensive unit tests covering:\n-AgentResponseandAgentResponseUpdateinitialization withfinish_reason\n-map_chat_to_agent_updateforwarding\n-_process_updatepropagation (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_reasondefaults toNone\n\n## Checklist\n\n- [x] Followed Contribution Guidelines\n- [x] Unit tests added\n- [x] No breaking changes"