Skip to content

Python: [Bug]: Executor type checking fails when using __future__ annotations in Python 3.12 #3898

@wpunr

Description

@wpunr

Description

When using from __future__ import annotations, type annotations are stored as strings. Type checking in _executor.py will get a str not a class object. This is passed to _workflow_context.py, which uses typing.get_origin and returns None.

This was mentioned in #1808 and fixed in #2308 for FunctionExecutor not the regular Executor. That fix uses typing.get_type_hints before the call to validate_workflow_context_annotation.

Code Sample

from __future__ import annotations

from pydantic import BaseModel

from agent_framework import Executor, WorkflowContext, handler

class MyTypeA(BaseModel):
    pass
class MyTypeB(BaseModel):
    pass

class MyExecutor(Executor):
    @handler
    async def example(self, input: str, ctx: WorkflowContext[MyTypeA, MyTypeB]) -> None:
        pass

Error Messages / Stack Traces

/usr/bin/python3.12 /IdeaProjects/problem.py 
Traceback (most recent call last):
  File "/IdeaProjects/problem.py", line 12, in <module>
    class MyExecutor(Executor):
  File "/IdeaProjects/problem.py", line 13, in MyExecutor
    @handler
     ^^^^^^^
  File "/home/ubuntu/.local/lib/python3.12/site-packages/agent_framework/_workflows/_executor.py", line 584, in handler
    return decorator(func)
           ^^^^^^^^^^^^^^^
  File "/home/ubuntu/.local/lib/python3.12/site-packages/agent_framework/_workflows/_executor.py", line 558, in decorator
    _validate_handler_signature(func)
  File "/home/ubuntu/.local/lib/python3.12/site-packages/agent_framework/_workflows/_executor.py", line 619, in _validate_handler_signature
    output_types, workflow_output_types = validate_workflow_context_annotation(
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/.local/lib/python3.12/site-packages/agent_framework/_workflows/_workflow_context.py", line 154, in validate_workflow_context_annotation
    raise ValueError(
ValueError: Handler parameter 'ctx' must be annotated as WorkflowContext, WorkflowContext[T], or WorkflowContext[T, U], got WorkflowContext[MyTypeA, MyTypeB]

Process finished with exit code 1

Package Versions

agent-framework: 1.0.0b260128

Python Version

Python 3.12

Additional Context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpythonworkflowsRelated to Workflows in agent-framework

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions