Skip to content

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

Open
ogkranthi wants to merge 6 commits intomicrosoft:mainfrom
ogkranthi:fix/typevar-handler-registration-error-message
Open

Python: Improve error message when TypeVar is used in handler registration#4553
ogkranthi wants to merge 6 commits intomicrosoft:mainfrom
ogkranthi:fix/typevar-handler-registration-error-message

Conversation

@ogkranthi
Copy link
Copy Markdown

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

…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).
Copilot AI review requested due to automatic review settings March 9, 2026 04:18
@markwallace-microsoft
Copy link
Copy Markdown
Member

markwallace-microsoft commented Mar 9, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _executor.py1881094%211, 335, 337, 346, 366, 369, 476, 481, 491, 664
   _function_executor.py78593%125, 160, 166, 172, 189
   _typing_utils.py1543477%173, 197, 242–247, 328, 330–331, 340, 342, 349, 351, 371, 373, 375, 380–387, 390–391, 393–397, 399
   _workflow_context.py1631888%58–59, 67, 71, 75–78, 80–82, 84–86, 161, 203, 314, 433
TOTAL27986335488% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5543 20 💤 0 ❌ 0 🔥 1m 26s ⏱️

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 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.

@ogkranthi
Copy link
Copy Markdown
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
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

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.

Kranthi Kumar Manchikanti added 2 commits March 11, 2026 23:04
- 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.
@ogkranthi
Copy link
Copy Markdown
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?

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).
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.

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

3 participants