Skip to content

Python: Improve error message when TypeVar is used in handler registration - #4553

Merged
moonbox3 merged 6 commits into
microsoft:mainfrom
ogkranthi:fix/typevar-handler-registration-error-message
Jul 8, 2026
Merged

Python: Improve error message when TypeVar is used in handler registration#4553
moonbox3 merged 6 commits into
microsoft:mainfrom
ogkranthi:fix/typevar-handler-registration-error-message

Conversation

@ogkranthi

Copy link
Copy Markdown
Contributor

Summary

  • Adds early detection of unresolved TypeVar instances during handler/executor registration, surfacing a clear ValueError with actionable guidance instead of a confusing TypeCompatibilityError at edge validation time
  • Covers @handler, @executor, and WorkflowContext[T] type argument validation (both direct and union members)

Fixes #4547

Changes

  • _executor.py — TypeVar check in @handler decorator for both explicit type params and introspected annotation paths
  • _function_executor.py — TypeVar check in FunctionExecutor.__init__ for both explicit and introspected paths
  • _workflow_context.py — TypeVar check in validate_workflow_context_annotation for direct type args and union members

Test plan

  • Verify @handler(input=SomeTypeVar) raises ValueError with actionable message
  • Verify handler with TypeVar annotation (introspection path) raises ValueError
  • Verify @executor(input=SomeTypeVar) raises ValueError
  • Verify WorkflowContext[SomeTypeVar] raises ValueError
  • Verify concrete types continue to work without regression
  • Existing test suite passes

Copilot AI review requested due to automatic review settings March 9, 2026 04:18
@markwallace-microsoft markwallace-microsoft added the python Usage: [Issues, PRs], Target: Python label Mar 9, 2026
@markwallace-microsoft

markwallace-microsoft commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _executor.py2001194%224, 363, 365, 374, 394, 397, 504, 509, 519, 692, 699
   _function_executor.py83692%125, 132, 160, 166, 172, 189
   _typing_utils.py1583478%188, 212, 257–262, 343, 345–346, 355, 357, 364, 366, 386, 388, 390, 395–402, 405–406, 408–412, 414
   _workflow_context.py1691889%58–59, 67, 71, 75–78, 80–82, 84–86, 161, 195, 307, 451
TOTAL44395533887% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8779 33 💤 0 ❌ 0 🔥 2m 10s ⏱️

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 earlier, more actionable validation errors when unresolved TypeVar objects are used in workflow handler/executor registration, aiming to fail fast (during registration) rather than later during workflow edge/type validation.

Changes:

  • Add TypeVar detection in @handler registration for both explicit decorator parameters and introspected annotations.
  • Add TypeVar detection in FunctionExecutor registration for both explicit decorator parameters and introspected annotations.
  • Add TypeVar detection in WorkflowContext[...] generic argument validation, including union members.

Reviewed changes

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

File Description
python/packages/core/agent_framework/_workflows/_workflow_context.py Adds early TypeVar checks for WorkflowContext type arguments (direct + union members) to raise clearer ValueErrors.
python/packages/core/agent_framework/_workflows/_function_executor.py Adds early TypeVar checks for @executor registration (explicit params + introspected message type).
python/packages/core/agent_framework/_workflows/_executor.py Adds early TypeVar checks for @handler registration (explicit params + introspected message type).

You can also share your feedback on Copilot code review. Take the survey.

Comment thread python/packages/core/agent_framework/_workflows/_workflow_context.py Outdated
Comment thread python/packages/core/agent_framework/_workflows/_executor.py
Comment thread python/packages/core/agent_framework/_workflows/_executor.py
@ogkranthi

Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copilot AI review requested due to automatic review settings March 11, 2026 21:55

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

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


You can also share your feedback on Copilot code review. Take the survey.

Comment thread python/packages/core/agent_framework/_workflows/_executor.py
Comment thread python/packages/core/agent_framework/_workflows/_workflow_context.py Outdated
Comment thread python/packages/core/agent_framework/_workflows/_executor.py
@ogkranthi

Copy link
Copy Markdown
Contributor Author

@markwallace-microsoft This PR is ready for review — all review threads are resolved, the pyright CI issue has been fixed, and tests are passing. Could you take a look when you get a chance?

Kranthi Kumar Manchikanti added 4 commits June 1, 2026 11:08
…ation

Fixes microsoft#4547. Adds early detection of unresolved TypeVar instances in:
- @handler decorator (both explicit and introspected type paths)
- @executor decorator (both explicit and introspected type paths)
- WorkflowContext type argument validation (direct and union members)

When a TypeVar is detected, a clear ValueError is raised with actionable
guidance to use concrete types via @handler(input=ConcreteType, output=ConcreteType).
- Add shared is_typevar() helper in _typing_utils.py that safely detects
  TypeVar from both typing and typing_extensions modules
- Replace all isinstance(x, TypeVar) calls with is_typevar() in
  _executor.py, _function_executor.py, and _workflow_context.py
- Add 18 unit tests covering TypeVar validation for @handler, @executor,
  and WorkflowContext[T] (explicit params, introspection, union members)
Pyright's reportUnknownVariableType flagged the inferred type as
partially unknown. Adding an explicit `tuple[type, ...]` annotation
resolves the strict-mode check.
Pyright cannot infer the runtime type of TypeVar constructors, so the
tuple elements resolve to type[Unknown]. A type annotation alone does
not satisfy strict mode — add an inline suppression for this specific
diagnostic since the unknown types are intentional (runtime TypeVar
class detection).
@eavanvalkenburg
eavanvalkenburg force-pushed the fix/typevar-handler-registration-error-message branch from 1080b6d to 907c318 Compare June 1, 2026 09:08
Comment thread python/packages/core/agent_framework/_workflows/_workflow_context.py Outdated
@moonbox3
moonbox3 enabled auto-merge July 8, 2026 01:44
@moonbox3
moonbox3 added this pull request to the merge queue Jul 8, 2026
Merged via the queue into microsoft:main with commit de39be9 Jul 8, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Improve error message when TypeVar is used in handler registration

5 participants