Skip to content

Python: Type checking fails for WorkflowContext annotation with from __future__ import annotations in Python 3.14 #1808

@surendra-dell-org

Description

@surendra-dell-org

Description:
When using Python 3.14 and from future import annotations, type annotations are stored as strings. The type checking in _workflow_context.py expects actual type objects and fails when it encounters a string annotation.

Error Example:

Text
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\surendra.katari\AppData\Roaming\Python\Python314\site-packages\agent_framework\_workflows\_workflow_context.py", line 249, in validate_function_signature
    output_types, workflow_output_types = validate_workflow_context_annotation(
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        ctx_param.annotation, f"parameter '{ctx_param.name}'", context_description
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\surendra.katari\AppData\Roaming\Python\Python314\site-packages\agent_framework\_workflows\_workflow_context.py", line 149, in validate_workflow_context_annotation
    raise ValueError(
    ...<3 lines>...
    )
ValueError: Handler method parameter 'ctx' must be annotated as WorkflowContext, WorkflowContext[T], or WorkflowContext[T, U], got WorkflowContext

Root Cause:
The check at line 124 is:

Python
return annotation is WorkflowContext
This fails when annotation is "WorkflowContext" (a string) instead of the class object.

Suggested Solution:
Use typing.get_type_hints to resolve string annotations to their actual types before comparison. Update _is_workflow_context_type to handle both string and type cases.

Example Fix:

Python

import typing

def _is_workflow_context_type(annotation: object) -> bool:
    if annotation is None:
        return False
    if isinstance(annotation, str):
        return annotation == "WorkflowContext"
    origin = typing.get_origin(annotation)
    return origin is WorkflowContext or annotation is WorkflowContext

And always resolve type hints before checking:

Python

hints = typing.get_type_hints(handler_method)
annotation = hints.get("ctx")

References:

Python docs: Forward references and get_type_hints
Code location

Metadata

Metadata

Assignees

Labels

pythonworkflowsRelated to Workflows in agent-framework

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions