Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cf.run tools cannot take in a built-in/imported functions as input #356

Open
afrizalhasbi opened this issue Oct 12, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@afrizalhasbi
Copy link

afrizalhasbi commented Oct 12, 2024

Description

Running this code would give it a validation error:

import numpy as np

cf.run(
    "Convert to np array: [1,2,3]",
    result_type=str,
    agents=[agent_name],
    tools=[np.array],
)
{
	"name": "ValidationError",
	"message": "1 validation error for Task
tools
  Value error, Invalid tool: <built-in function array> [type=value_error, input_value=[<built-in function array>], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error",
	"stack": "---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[18], line 1
----> 1 cf.run(
      2     \"Convert to np array: [1,2,3]\",
      3     result_type=str,
      4     agents=[thinker],
      5     tools=[np.array],
      6 )

File ~/.local/lib/python3.12/site-packages/controlflow/run.py:109, in run(objective, turn_strategy, max_llm_calls, max_agent_turns, raise_on_failure, handlers, **task_kwargs)
     99 def run(
    100     objective: str,
    101     *,
   (...)
    107     **task_kwargs,
    108 ) -> Any:
--> 109     task = Task(objective=objective, **task_kwargs)
    110     results = run_tasks(
    111         tasks=[task],
    112         raise_on_failure=raise_on_failure,
   (...)
    116         handlers=handlers,
    117     )
    118     return results[0]

File ~/.local/lib/python3.12/site-packages/controlflow/tasks/task.py:215, in Task.__init__(self, objective, user_access, **kwargs)
    209     warnings.warn(
    210         \"The `user_access` argument is deprecated. Use `interactive=True` instead.\",
    211         DeprecationWarning,
    212     )
    213     kwargs[\"interactive\"] = True
--> 215 super().__init__(**kwargs)
    217 # create dependencies to tasks passed in as depends_on
    218 for task in self.depends_on:

File ~/.local/lib/python3.12/site-packages/pydantic/main.py:209, in BaseModel.__init__(self, **data)
    207 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    208 __tracebackhide__ = True
--> 209 validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    210 if self is not validated_self:
    211     warnings.warn(
    212         'A custom validator is returning a value other than `self`.\
'
    213         \"Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\
\"
    214         'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
    215         category=None,
    216     )

ValidationError: 1 validation error for Task
tools
  Value error, Invalid tool: <built-in function array> [type=value_error, input_value=[<built-in function array>], input_type=list]
    For further information visit https://errors.pydantic.dev/2.9/v/value_error"
}

The same is true for built-in functions, like print, and functions import with from module import function

A workaround is creating a wrapper that just does the original imported function:

import numpy as np

def getarray(input):
    return np.array(input)

cf.run(
    "Convert to np array: [1,2,3]",
    result_type=str,
    agents=[agent_name],
    tools=[getarray],
)

I was trying to add all modules from os to the run so the model can interact with my file system, but I stumbled upon this error. To do that I had to manually create 1-line functions that just wrap over the original which became tedious. Would love to see some fix/explanation to this.

Version Information

controlflow==0.10.0
pydantic==2.9.2
@afrizalhasbi afrizalhasbi added the bug Something isn't working label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant