Skip to content

[Bug] google_adk: activity_as_tool classifies a method reference by its class's rebound attribute (main only) #362

Closed

Description

@campoy

Package: python/google_adk (temporalio-google-adk), main at 88745dc (since #312). The 0.0.1 release is not affected.

What happens

Since #312, activity_as_tool tells a method passed as Class.method (drop self) from a staticmethod (keep it) by finding the class that the function's __qualname__ names, in the function's globals, and reading that attribute. It doesn't check that the attribute is the function it was given. If the class's module-level name, or that attribute, was rebound after Workflow code took the reference (for example by importlib.reload of a changed module), the reference is classified by the new object:

  • A staticmethod echo(self, value) whose class's name now holds a class with a method echo gets a tool without self. The model's value reaches self, and the call returns a wrong result with no error.
  • In the reverse case, a method whose rebound class holds a staticmethod of that name keeps self in the tool.

0.0.1 offered every parameter of the staticmethod.

Reproduction

One file, no Temporal server. Outside a Workflow the tool calls the reference itself, as in a local ADK run. The script calls the tool with every offered parameter set to "model-<name>", and exits 1 when the tool drops the staticmethod's self.

"""google_adk activity_as_tool with a staticmethod reference whose class's name was later rebound.

One file, no Temporal server. Outside a Workflow the tool calls the reference itself, as in a local
ADK run, so this calls the tool with every offered parameter set to "model-<name>". Exits 1 when the
tool drops the staticmethod's `self`, so the model's value lands in the wrong parameter.
"""

import asyncio
import inspect
from datetime import timedelta

from temporalio import activity
from temporalio.google_adk.workflow import activity_as_tool


class Holder:
    @staticmethod
    @activity.defn
    async def echo(self, value: str = "default") -> str:
        return f"self={self} value={value}"


echo = Holder.echo  # the reference Workflow code passes to activity_as_tool


class Holder:  # the name now holds another class, as after importlib.reload of a changed module
    @activity.defn
    async def echo(self, value: str = "default") -> str:
        return "other class"


async def main() -> int:
    print("the reference takes:", list(inspect.signature(echo).parameters))
    try:
        tool = activity_as_tool(echo, start_to_close_timeout=timedelta(seconds=30))
    except ValueError as e:
        print("activity_as_tool raised ValueError:", e)
        return 0
    offered = list(inspect.signature(tool).parameters)
    print("the tool offers:", offered)
    print("the tool returns:", await tool(**{name: f"model-{name}" for name in offered}))
    return 0 if "self" in offered else 1


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))

Output:

### 0.0.1
the reference takes: ['self', 'value']
the tool offers: ['self', 'value']
the tool returns: self=model-self value=model-value
exit 0
### main at 88745dc
the reference takes: ['self', 'value']
the tool offers: ['value']
the tool returns: self=model-value value=default
exit 1

Expected

The tool offers the parameters that calling the reference takes. If activity_as_tool can't tell what the reference is, it should refuse it, as it does for a class it can't reach.

Possible fix

Check that the attribute found is the function, or a staticmethod whose __func__ is the function, and raise ValueError otherwise.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions