microsoft/autogen · error · NotImplementedError

String based function with requirement objects are not direc

Error message

String based function with requirement objects are not directly callable

What it means

FunctionWithRequirementsStr intentionally implements __call__ to raise NotImplementedError: it is a description of code to be executed inside a code executor (with its declared python_packages), not a callable in the current process. Calling `f(...)` locally is always an error by design — the compiled function exists only for inspection/transport.

Source

Thrown at python/packages/autogen-core/src/autogen_core/code_executor/_func_with_reqs.py:126

        if spec is None:
            raise ValueError("Could not create spec")
        module = module_from_spec(spec)
        if spec.loader is None:
            raise ValueError("Could not create loader")

        try:
            spec.loader.exec_module(module)
        except Exception as e:
            raise ValueError(f"Could not compile function: {e}") from e

        functions = inspect.getmembers(module, inspect.isfunction)
        if len(functions) != 1:
            raise ValueError("The string must contain exactly one function")

        self._func_name, self.compiled_func = functions[0]

    def __call__(self, *args: Any, **kwargs: Any) -> None:
        raise NotImplementedError("String based function with requirement objects are not directly callable")


@dataclass
class FunctionWithRequirements(Generic[T, P]):
    func: Callable[P, T]
    python_packages: Sequence[str] = field(default_factory=list)
    global_imports: Sequence[Import] = field(default_factory=list)

    @classmethod
    def from_callable(
        cls, func: Callable[P, T], python_packages: Sequence[str] = [], global_imports: Sequence[Import] = []
    ) -> FunctionWithRequirements[T, P]:
        return cls(python_packages=python_packages, global_imports=global_imports, func=func)

    @staticmethod
    def from_str(
        func: str, python_packages: Sequence[str] = [], global_imports: Sequence[Import] = []
    ) -> FunctionWithRequirementsStr:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Execute it via a code executor (e.g. LocalCommandLineCodeExecutor / container executor) that receives functions_with_requirements, rather than calling it in-process.
  2. If you need a locally callable version, use FunctionWithRequirements.from_callable with a real Python function.
  3. Type-annotate executor inputs so type checkers flag direct calls (FunctionWithRequirementsStr is not Callable).

Example fix

# before
f = FunctionWithRequirementsStr("def run(x): return x * 2")
result = f(3)  # NotImplementedError

# after
from autogen_core.code_executor import LocalCommandLineCodeExecutor
executor = LocalCommandLineCodeExecutor(functions=[f_to_executor_format])
# hand f to the executor's function list; it runs in the executor, not in-process
Defensive patterns

Strategy: type-guard

Validate before calling

def is_directly_callable(f: object) -> bool:
    from autogen_core.code_executor import FunctionWithRequirementsStr, FunctionWithRequirements
    return not isinstance(f, FunctionWithRequirementsStr) and callable(f)

Type guard

from autogen_core.code_executor import FunctionWithRequirementsStr

def assert_executor_bound(f: object) -> None:
    if isinstance(f, FunctionWithRequirementsStr):
        assert not callable(getattr(f, "__call__", None)) or True  # __call__ always raises
        return  # only pass to code executors

Prevention

When it happens

Trigger: `f = FunctionWithRequirementsStr(...); f(1, 2)` anywhere in application or test code; passing the object to an API that invokes its arguments (map, pandas.apply, a callback parameter typed Callable).

Common situations: Treating FunctionWithRequirementsStr interchangeably with FunctionWithRequirements (whose func IS callable); unit tests trying to assert on behavior by calling it directly instead of running it in a code executor.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/cdf0ee1bc0b1a6f8. Report an issue: GitHub.