-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix AddImageTool injection for multimodal Agent.kickoff (#3936) #4034
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
Open
ProCO-RPHopkins
wants to merge
3
commits into
crewAIInc:main
Choose a base branch
from
ProCO-RPHopkins:feature/multimodal-agent-kickoff-3936
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−6
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
lib/crewai/tests/agents/test_agent_kickoff_multimodal.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from crewai.agent.core import Agent | ||
| from crewai.tools.agent_tools.add_image_tool import AddImageTool | ||
| from crewai.tools.base_tool import BaseTool | ||
|
|
||
|
|
||
| class _SpyLiteAgent: | ||
| """LiteAgent stand-in that just records the tools passed in.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *args: Any, | ||
| tools: list[Any] | None = None, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| self.tools = list(tools or []) | ||
|
|
||
| def kickoff(self, messages: Any) -> dict[str, Any]: | ||
| # We don't care about LLM behaviour here, only tool wiring. | ||
| return {"messages": messages, "tools": self.tools} | ||
|
|
||
| async def kickoff_async(self, messages: Any) -> dict[str, Any]: | ||
| # Mirror the sync API for async tests. | ||
| return {"messages": messages, "tools": self.tools} | ||
|
|
||
|
|
||
| class _DummyTool(BaseTool): | ||
| name: str = "DummyTool" | ||
| description: str = "A dummy tool for testing" | ||
|
|
||
| def _run(self, *args: Any, **kwargs: Any) -> Any: | ||
| # Minimal implementation; we only care that the tool is instantiable. | ||
| return "dummy output" | ||
|
|
||
| name: str = "dummy" | ||
| description: str = "A dummy tool for testing" | ||
ProCO-RPHopkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _patch_lite_agent(monkeypatch, spy_cls=_SpyLiteAgent) -> None: | ||
| import crewai.agent.core as agent_core | ||
|
|
||
| monkeypatch.setattr(agent_core, "LiteAgent", spy_cls) | ||
|
|
||
|
|
||
| def test_agent_kickoff_multimodal_adds_add_image_tool_once(monkeypatch) -> None: | ||
| _patch_lite_agent(monkeypatch) | ||
|
|
||
| agent = Agent( | ||
| role="Image agent", | ||
| goal="Handle image inputs", | ||
| backstory="Test agent for multimodal kickoff wiring.", | ||
| tools=[], # start with no tools | ||
| multimodal=True, | ||
| ) | ||
|
|
||
| result1 = agent.kickoff("Describe https://example.com/image.png") | ||
| tools1 = result1["tools"] | ||
|
|
||
| # We should get exactly one AddImageTool | ||
| add_image_tools_1 = [t for t in tools1 if isinstance(t, AddImageTool)] | ||
| assert add_image_tools_1, "Expected AddImageTool to be injected for multimodal agent kickoff" | ||
| assert len(add_image_tools_1) == 1, "Expected exactly one AddImageTool on first kickoff run" | ||
|
|
||
| # A second kickoff should not accumulate extra AddImageTool instances. | ||
| result2 = agent.kickoff("Describe https://example.com/another.png") | ||
| tools2 = result2["tools"] | ||
| add_image_tools_2 = [t for t in tools2 if isinstance(t, AddImageTool)] | ||
| assert len(add_image_tools_2) == 1, "Expected no duplicate AddImageTool instances across multiple kickoffs" | ||
|
|
||
|
|
||
| def test_agent_kickoff_multimodal_does_not_duplicate_existing_add_image_tool( | ||
| monkeypatch, | ||
| ) -> None: | ||
| _patch_lite_agent(monkeypatch) | ||
|
|
||
| # Agent already has an AddImageTool in its tools list. | ||
| agent = Agent( | ||
| role="Image agent", | ||
| goal="Handle image inputs", | ||
| backstory="Test agent with existing AddImageTool.", | ||
| tools=[AddImageTool()], | ||
| multimodal=True, | ||
| ) | ||
|
|
||
| result = agent.kickoff("Describe https://example.com/existing.png") | ||
| tools = result["tools"] | ||
| add_image_tools = [t for t in tools if isinstance(t, AddImageTool)] | ||
| assert ( | ||
| len(add_image_tools) == 1 | ||
| ), "Agent.kickoff should not duplicate an existing AddImageTool" | ||
|
|
||
|
|
||
| def test_agent_kickoff_does_not_mutate_agent_tools(monkeypatch) -> None: | ||
| _patch_lite_agent(monkeypatch) | ||
|
|
||
| # Start with a single dummy tool in the agent's tools list. | ||
| dummy = _DummyTool() | ||
| agent = Agent( | ||
| role="Image agent", | ||
| goal="Handle image inputs", | ||
| backstory="Agent with existing non-image tool.", | ||
| tools=[dummy], | ||
| multimodal=True, | ||
| ) | ||
|
|
||
| original_tools_id = id(agent.tools) | ||
| original_len = len(agent.tools) | ||
|
|
||
| result = agent.kickoff("Describe https://example.com/with-dummy.png") | ||
| tools = result["tools"] | ||
|
|
||
| # LiteAgent should see both the original tool and AddImageTool. | ||
| assert any(isinstance(t, _DummyTool) for t in tools), "Expected DummyTool to be preserved for kickoff" | ||
| assert any(isinstance(t, AddImageTool) for t in tools), "Expected AddImageTool to be injected for kickoff" | ||
|
|
||
| # Agent.tools should not be mutated in-place or grow with extra tools. | ||
| assert len(agent.tools) == original_len, "Agent.tools length should not change during kickoff" | ||
| assert id(agent.tools) == original_tools_id, "Agent.tools reference should not be replaced during kickoff" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_agent_kickoff_async_multimodal_adds_add_image_tool_once(monkeypatch) -> None: | ||
| _patch_lite_agent(monkeypatch) | ||
|
|
||
| agent = Agent( | ||
| role="Image agent", | ||
| goal="Handle image inputs async", | ||
| backstory="Test agent for async multimodal kickoff wiring.", | ||
| tools=[], | ||
| multimodal=True, | ||
| ) | ||
|
|
||
| result1 = await agent.kickoff_async("Describe https://example.com/image.png") | ||
| tools1 = result1["tools"] | ||
| add_image_tools_1 = [t for t in tools1 if isinstance(t, AddImageTool)] | ||
| assert add_image_tools_1, "Expected AddImageTool to be injected for multimodal agent kickoff_async" | ||
| assert len(add_image_tools_1) == 1, "Expected exactly one AddImageTool on first kickoff_async run" | ||
|
|
||
| result2 = await agent.kickoff_async("Describe https://example.com/another.png") | ||
| tools2 = result2["tools"] | ||
| add_image_tools_2 = [t for t in tools2 if isinstance(t, AddImageTool)] | ||
| assert len(add_image_tools_2) == 1, "Expected no duplicate AddImageTool instances across multiple kickoff_async runs" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_agent_kickoff_async_multimodal_does_not_duplicate_existing_add_image_tool( | ||
| monkeypatch, | ||
| ) -> None: | ||
| _patch_lite_agent(monkeypatch) | ||
|
|
||
| agent = Agent( | ||
| role="Image agent", | ||
| goal="Handle image inputs async", | ||
| backstory="Async agent with existing AddImageTool.", | ||
| tools=[AddImageTool()], | ||
| multimodal=True, | ||
| ) | ||
|
|
||
| result = await agent.kickoff_async("Describe https://example.com/existing.png") | ||
| tools = result["tools"] | ||
| add_image_tools = [t for t in tools if isinstance(t, AddImageTool)] | ||
| assert ( | ||
| len(add_image_tools) == 1 | ||
| ), "Agent.kickoff_async should not duplicate an existing AddImageTool" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.