Skip to content

Python: Add explicit input, output, and workflow_output parameters to @handler, @executor and request_info#3472

Merged
moonbox3 merged 10 commits intomicrosoft:mainfrom
moonbox3:support-decorator-type-specify
Feb 3, 2026
Merged

Python: Add explicit input, output, and workflow_output parameters to @handler, @executor and request_info#3472
moonbox3 merged 10 commits intomicrosoft:mainfrom
moonbox3:support-decorator-type-specify

Conversation

@moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented Jan 28, 2026

Motivation and Context

  • Add optional input, output, and workflow_output parameters to @handler and @executor decorators
  • Explicit types take precedence over introspection from function signatures
  • Support union types via both str | int and Union[str, int] syntax
  • Add normalize_type_to_list utility for converting union types to lists

Example

# You can specify all three types explicitly:
@handler(input_type=str, output_type=int, workflow_output_type=bool)
async def handle_full(self, message, ctx) -> None:
    await ctx.send_message(42)      # int - matches output_type
    await ctx.yield_output(True)    # bool - matches workflow_output_type

# Or for function executors:
@executor(input_type=str, output_type=int, workflow_output_type=bool)
async def process(message, ctx):
    await ctx.send_message(42)
    await ctx.yield_output(True)

Description

See above.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings January 28, 2026 02:04
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jan 28, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _agent_executor.py1712386%95, 117, 151, 167–168, 219–220, 222–223, 255–257, 265–267, 277–279, 281, 285, 289, 293–294
   _executor.py1731094%212, 334, 336, 345, 365, 368, 475, 480, 490, 649
   _function_executor.py73593%112, 139, 145, 151, 168
   _magentic.py6149185%69–78, 83, 87–98, 263, 274, 278, 298, 359, 368, 370, 412, 429, 438–439, 441–443, 445, 456, 598, 600, 640, 688, 724–726, 728, 736–739, 743–746, 789, 816–819, 910, 916, 922, 961, 999, 1028, 1045, 1056, 1110–1111, 1115–1117, 1141, 1162–1163, 1176, 1192, 1214, 1262–1263, 1301–1302, 1458, 1467, 1470, 1475, 1871, 1913, 1928, 1957
   _orchestrator_helpers.py21290%90–91
   _request_info_mixin.py107595%62, 99, 335, 342, 349
   _typing_utils.py1242877%141, 165, 244, 246–247, 256, 258, 265, 267, 287, 289, 291, 296–303, 306–307, 309–313, 315
   _validation.py146695%86, 133, 154, 248, 329, 332
   _workflow.py2481793%89, 259–261, 263–264, 282, 310, 411, 688, 722, 727, 730, 749–751, 816
   _workflow_context.py1772486%63–64, 72, 76, 90, 166, 191, 309, 428, 471–473, 475, 477–478, 480–481, 490–492, 494–496, 498
   _workflow_executor.py1734375%96, 445, 456, 468–471, 474–476, 479–480, 482, 485–487, 490–494, 498–499, 508, 513, 547, 573–578, 581, 584, 592, 597, 608, 618, 622, 628, 632, 642, 646
TOTAL16335198487% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3811 221 💤 0 ❌ 0 🔥 1m 6s ⏱️

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 pull request adds explicit input_type and output_type parameters to the @handler and @executor decorators in the Python workflow framework. These optional parameters allow developers to specify types explicitly instead of relying on type introspection from function signatures, with explicit types taking precedence over introspected types. The feature supports union types via both str | int and Union[str, int] syntax.

Changes:

  • Added normalize_type_to_list utility function to convert union types to lists
  • Enhanced @handler and @executor decorators with optional input_type and output_type parameters
  • Modified signature validation functions to support skipping message annotation when explicit types are provided
  • Added comprehensive test coverage for explicit type parameters
  • Updated sample code to demonstrate the new feature

Reviewed changes

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

Show a summary per file
File Description
python/packages/core/agent_framework/_workflows/_typing_utils.py Added normalize_type_to_list utility function for handling union types; changed logger import pattern
python/packages/core/agent_framework/_workflows/_executor.py Enhanced @handler decorator with input_type and output_type parameters; updated handler validation logic; refactored exception handling in _discover_handlers
python/packages/core/agent_framework/_workflows/_function_executor.py Enhanced @executor decorator and FunctionExecutor class with explicit type parameters; updated function validation logic
python/packages/core/tests/workflow/test_typing_utils.py Added comprehensive tests for normalize_type_to_list function covering single types, union types, and optional types
python/packages/core/tests/workflow/test_executor.py Added comprehensive test suite for @handler with explicit types, covering precedence, union types, and partial specifications
python/packages/core/tests/workflow/test_function_executor.py Added comprehensive test suite for @executor with explicit types, covering various edge cases and usage patterns
python/samples/getting_started/workflows/_start-here/step1_executors_and_edges.py Added documentation and example demonstrating explicit type parameters with a new ExclamationAdder executor

@moonbox3 moonbox3 self-assigned this Jan 28, 2026
@moonbox3 moonbox3 moved this to Sprint: In Review in Semantic Kernel Jan 29, 2026
@moonbox3 moonbox3 added the workflows Related to Workflows in agent-framework label Jan 30, 2026
@moonbox3 moonbox3 moved this to In Review in Agent Framework Jan 30, 2026
@TaoChenOSU
Copy link
Contributor

Should we also update response_handler in this PR?

@moonbox3
Copy link
Contributor Author

moonbox3 commented Feb 2, 2026

Should we also update response_handler in this PR?

Yes will add now.

@moonbox3 moonbox3 changed the title Python: Add explicit input_type and output_type parameters to @handler and @executor decorators Python: Add explicit input, output, and workflow_output parameters to @handler, @executor and request_info Feb 2, 2026
Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

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

one question, but overall looks good

@moonbox3 moonbox3 added this pull request to the merge queue Feb 3, 2026
Merged via the queue into microsoft:main with commit f56218f Feb 3, 2026
23 checks passed
@github-project-automation github-project-automation bot moved this from In Review to Done in Agent Framework Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python workflows Related to Workflows in agent-framework

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Python: Add type handling to @handler and @executor decorators

5 participants