Python: fix: resolve string annotations in FunctionExecutor#2308
Merged
eavanvalkenburg merged 4 commits intomicrosoft:mainfrom Nov 19, 2025
Merged
Conversation
Enhance type hint validation in FunctionExecutor by importing `typing` and using `get_type_hints` to correctly resolve annotations. This fixes validation failures when `from __future__ import annotations` is enabled, which stores annotations as strings. Fixes microsoft#1808
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes type hint validation in FunctionExecutor to support Python's from __future__ import annotations (PEP 563), which stores type annotations as strings rather than type objects.
Key Changes:
- Updates
_validate_function_signatureto usetyping.get_type_hints()for resolving string annotations to actual type objects - Adds comprehensive test coverage for future annotations with both simple and complex type hints
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/core/agent_framework/_workflows/_function_executor.py |
Imports typing module and modifies signature validation to resolve stringified annotations using get_type_hints() |
python/packages/core/tests/workflow/test_function_executor_future.py |
Adds new test suite with from __future__ import annotations to verify the fix works with stringified type hints |
python/packages/core/tests/workflow/test_function_executor_future.py
Outdated
Show resolved
Hide resolved
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
…ure.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
eavanvalkenburg
approved these changes
Nov 19, 2025
moonbox3
approved these changes
Nov 19, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…t#2308) * fix: resolve string annotations in FunctionExecutor Enhance type hint validation in FunctionExecutor by importing `typing` and using `get_type_hints` to correctly resolve annotations. This fixes validation failures when `from __future__ import annotations` is enabled, which stores annotations as strings. Fixes microsoft#1808 * Update python/packages/core/tests/workflow/test_function_executor_future.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ran pre commit --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
moonbox3
added a commit
to moonbox3/agent-framework
that referenced
this pull request
Feb 26, 2026
microsoft#3898) Use typing.get_type_hints() in _validate_handler_signature to resolve string annotations from `from __future__ import annotations`. This mirrors the fix applied to FunctionExecutor in microsoft#2308. When __future__ annotations are enabled, type annotations are stored as strings. The handler decorator was passing these strings directly to validate_workflow_context_annotation, which uses typing.get_origin and returns None for strings, causing a ValueError. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-merge-queue bot
pushed a commit
that referenced
this pull request
Feb 27, 2026
…e__ import annotations` (#4317) * Python: Fix Executor handler type checking with __future__ annotations (#3898) Use typing.get_type_hints() in _validate_handler_signature to resolve string annotations from `from __future__ import annotations`. This mirrors the fix applied to FunctionExecutor in #2308. When __future__ annotations are enabled, type annotations are stored as strings. The handler decorator was passing these strings directly to validate_workflow_context_annotation, which uses typing.get_origin and returns None for strings, causing a ValueError. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address PR review feedback for #3898: improve error handling and test coverage - Wrap typing.get_type_hints() in try/except to provide a descriptive ValueError mentioning the handler name when annotations cannot be resolved - Strengthen bare context test to assert output_types and workflow_output_types - Add test for @handler(input=..., output=...) with future annotations covering the skip_message_annotation branch - Add test for union-type context annotations with future annotations Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Narrow exception catch and add test for unresolvable annotations (#3898) - Narrow except clause from bare Exception to (NameError, AttributeError, TypeError) to avoid masking unexpected errors. - Add test_handler_unresolvable_annotation_raises to verify that a handler with a forward-reference to a non-existent type raises ValueError with the expected message. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix #3898: fall back to raw annotations when get_type_hints fails When typing.get_type_hints(func) raises NameError (unresolvable forward ref), AttributeError, RecursionError, or any other exception, fall back to the raw parameter annotations instead of raising a ValueError. This matches the suggestion from @moonbox3 on PR #4317. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix test to match new fallback behavior when get_type_hints fails (#3898) The code now falls back to raw string annotations instead of raising 'Failed to resolve type annotations'. A ValueError is still raised when the raw string ctx annotation is not a valid WorkflowContext type, so update the test to match on ValueError without checking the message. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Apply pyupgrade: remove unnecessary string annotation quote * Add noqa for intentionally undefined name in annotation test --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enhance type hint validation in FunctionExecutor by importing
typingand usingget_type_hintsto correctly resolve annotations.This fixes validation failures when
from __future__ import annotationsis enabled, which stores annotations as strings.Fixes #1808
Motivation and Context
This change is required to support Python codebases that use
from __future__ import annotations(PEP 563).Why is this change required?
When
from __future__ import annotationsis enabled, type annotations are stored as strings rather than type objects. The current implementation ofFunctionExecutorinspectsparam.annotationdirectly, which fails when it encounters a string (e.g.,"WorkflowContext[str]") instead of the actual type object.What problem does it solve?
It solves the
ValueErrorthat occurs duringFunctionExecutorinitialization when users enable future annotations. The error looks like this:What scenario does it contribute to?
It enables developers to use modern Python typing practices (PEP 563) and forward references within their agent workflows without runtime errors.
Issue Link: Fixes Python: Type checking fails for WorkflowContext annotation with from __future__ import annotations in Python 3.14 #1808
Description
The fix involves updating
_validate_function_signaturein_function_executor.pyto correctly resolve type hints.inspect.signature(...).parameters[...].annotation, I now usetyping.get_type_hints(func)to resolve the annotations into their actual type objects.typing.type_hints = typing.get_type_hints(func)inside_validate_function_signature.messageandctxparameter annotations from the resolvedtype_hintsdictionary.test_function_executor_future.pythat explicitly usesfrom __future__ import annotationsto verify the fix and prevent regression.Contribution Checklist