Fix issue #2383: Add invoke method to BaseTool for models without function calling support

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-17 02:49:10 +00:00
parent 24f1a19310
commit 3f25e535f4
2 changed files with 96 additions and 1 deletions

View File

@@ -0,0 +1,55 @@
from typing import Type
import pytest
from pydantic import BaseModel, Field
from crewai.tools import BaseTool
class TestToolInput(BaseModel):
param: str = Field(description="A test parameter")
class TestTool(BaseTool):
name: str = "Test Tool"
description: str = "A tool for testing the invoke method"
args_schema: Type[BaseModel] = TestToolInput
def _run(self, param: str) -> str:
return f"Tool executed with: {param}"
def test_invoke_with_dict():
"""Test that invoke works with a dictionary input."""
tool = TestTool()
result = tool.invoke(input={"param": "test value"})
assert result == "Tool executed with: test value"
def test_invoke_with_json_string():
"""Test that invoke works with a JSON string input."""
tool = TestTool()
result = tool.invoke(input='{"param": "test value"}')
assert result == "Tool executed with: test value"
def test_invoke_with_raw_string():
"""Test that invoke works with a raw string input."""
tool = TestTool()
result = tool.invoke(input="test value")
assert result == "Tool executed with: test value"
def test_invoke_with_empty_dict():
"""Test that invoke handles empty dict input appropriately."""
tool = TestTool()
with pytest.raises(Exception):
# Should raise an exception since param is required
tool.invoke(input={})
def test_invoke_with_extra_args():
"""Test that invoke filters out extra arguments not in the schema."""
tool = TestTool()
result = tool.invoke(input={"param": "test value", "extra": "ignored"})
assert result == "Tool executed with: test value"