A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
refactor(ai): add tests, refactor function (#10597)
Shahmir Varqha9d34d44112de
diff --git a/marimo/_ai/_pydantic_ai_utils.py b/marimo/_ai/_pydantic_ai_utils.pyindex 2282820e29..2fa433c3e8 100644--- a/marimo/_ai/_pydantic_ai_utils.py+++ b/marimo/_ai/_pydantic_ai_utils.py@@ -2,12 +2,13 @@from __future__ import annotationsimport uuid+from dataclasses import dataclassfrom functools import lru_cachefrom typing import TYPE_CHECKING, Any, get_argsfrom marimo import _loggersfrom marimo._messaging.msgspec_encoder import asdict-from marimo._server.ai.tools.types import ToolDefinition+from marimo._server.ai.tools.types import ToolDefinition, ToolSourcefrom marimo._server.models.completion import UIMessage as ServerUIMessageif TYPE_CHECKING:@@ -28,7 +29,7 @@ def profile_get(profile: ModelProfile, key: str, default: Any) -> Any:if TYPE_CHECKING:- from collections.abc import Callable+ from collections.abc import Awaitable, Callablefrom pydantic_ai import FunctionToolsetfrom pydantic_ai.ui.vercel_ai.request_types import UIMessage, UIMessagePart@@ -47,59 +48,50 @@ def format_inline_context(plain_text: str) -> str:return f"<context>\n{plain_text.strip()}\n</context>"+@dataclass(frozen=True)+class _ToolFunction:+ name: str+ source: ToolSource+ tool_invoker: Callable[[str, dict[str, Any]], Awaitable[Any]]++ async def __call__(self, **kwargs: Any) -> Any:+ if self.source == "frontend":+ from pydantic_ai import CallDeferred++ raise CallDeferred(+ metadata={+ "source": "frontend",+ "tool_name": self.name,+ "kwargs": kwargs,+ }+ )++ result = await self.tool_invoker(self.name, kwargs)+ return asdict(result)++def form_toolsets(tools: list[ToolDefinition],- tool_invoker: Callable[[str, dict[str, Any]], Any],+ tool_invoker: Callable[[str, dict[str, Any]], Awaitable[Any]],) -> tuple[FunctionToolset, bool]:- """- Because we have a list of tool definitions and call them in a separate event loop,- we create a closure to invoke the tool (backend) or raise a CallDeferred (frontend).- Ref: https://ai.pydantic.dev/toolsets/#function-toolset-- Returns a tuple of the toolset and whether deferred tool requests are needed.- """- from pydantic_ai import CallDeferred, FunctionToolset, Tool+ from pydantic_ai import FunctionToolset, Tooltoolset = FunctionToolset()- deferred_tool_requests = False-for tool in tools:- if tool.source == "frontend":- deferred_tool_requests = True-- async def tool_fn(- _tool_name: str = tool.name, **kwargs: Any- ) -> Any:- raise CallDeferred(- metadata={- "source": "frontend",- "tool_name": _tool_name,- "kwargs": kwargs,- }- )- else:-- async def tool_fn(- _tool_name: str = tool.name, **kwargs: Any- ) -> Any:- result = await tool_invoker(_tool_name, kwargs)- # Convert to JSON-serializable object- return asdict(result)-- tool_fn.__name__ = tool.name- # Use the tool's real JSON schema instead of letting pydantic-ai- # infer one from tool_fn's signature (which is always the generic- # `(_tool_name, **kwargs)` closure above, regardless of the actual- # tool's parameters).toolset.add_tool(Tool.from_schema(- function=tool_fn,+ function=_ToolFunction(+ name=tool.name,+ source=tool.source,+ tool_invoker=tool_invoker,+ ),name=tool.name,description=tool.description,json_schema=tool.parameters,))- return toolset, deferred_tool_requests++ return toolset, any(tool.source == "frontend" for tool in tools)def convert_to_pydantic_messages(diff --git a/tests/_ai/test_pydantic_utils.py b/tests/_ai/test_pydantic_utils.pyindex e6628a6822..6fa6db40bf 100644--- a/tests/_ai/test_pydantic_utils.py+++ b/tests/_ai/test_pydantic_utils.py@@ -2,7 +2,7 @@from __future__ import annotationsfrom dataclasses import dataclass-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, callimport pytest@@ -114,8 +114,14 @@ class TestFormToolsets:assert toolset is not Noneassert deferred is True # has frontend tool- def test_form_toolsets_with_only_backend_and_mcp_tools(self):- tool_invoker = AsyncMock()+ async def test_tools_dispatch_by_registered_name(self):+ @dataclass+ class MockResult:+ value: str++ tool_invoker = AsyncMock(+ side_effect=[MockResult("backend"), MockResult("mcp")]+ )tools = [ToolDefinition(name="backend_tool",@@ -133,8 +139,22 @@ class TestFormToolsets:),]toolset, deferred = form_toolsets(tools, tool_invoker)- assert toolset is not None- assert deferred is False # no frontend tools+ pydantic_tools = toolset.tools+ tools[0].name = "mutated_tool"+ tools[0].source = "frontend"++ backend_result = await pydantic_tools["backend_tool"].function(+ _tool_name="mcp_tool"+ )+ mcp_result = await pydantic_tools["mcp_tool"].function()++ assert deferred is False+ assert backend_result == {"value": "backend"}+ assert mcp_result == {"value": "mcp"}+ assert tool_invoker.await_args_list == [+ call("backend_tool", {"_tool_name": "mcp_tool"}),+ call("mcp_tool", {}),+ ]async def test_backend_tool_invokes_tool_invoker(self):@dataclass@@ -159,8 +179,7 @@ class TestFormToolsets:assert backend_tool.name == "backend_tool"assert backend_tool.description == "A backend tool"- # Actually call the tool function- result = await backend_tool.function(arg1="test", arg2=123) # type: ignore[call-arg]+ result = await backend_tool.function(arg1="test", arg2=123)# Verify tool_invoker was called with correct argumentstool_invoker.assert_called_once_with(@@ -190,9 +209,8 @@ class TestFormToolsets:assert frontend_tool.name == "frontend_tool"assert frontend_tool.description == "A frontend tool"- # Call the tool function and verify it raises CallDeferredwith pytest.raises(CallDeferred) as exc_info:- await frontend_tool.function(arg="value") # type: ignore[call-arg]+ await frontend_tool.function(arg="value")# Verify CallDeferred has correct metadataassert exc_info.value.metadata == {@@ -203,11 +221,7 @@ class TestFormToolsets:# Verify tool_invoker was NOT called for frontend toolstool_invoker.assert_not_called()- def test_form_toolsets_preserves_tool_schema(self):- # Regression test: the JSON schema pydantic-ai exposes to the model- # must be the tool's real `parameters`, not a generic schema- # inferred from the internal `tool_fn(_tool_name, **kwargs)`- # closure signature.+ def test_form_toolsets_uses_tool_schema(self):tool_invoker = AsyncMock()schema = {"type": "object",@@ -216,6 +230,7 @@ class TestFormToolsets:"max_results": {"type": "integer"},},"required": ["query"],+ "additionalProperties": False,}tool = ToolDefinition(name="search_docs",@@ -227,11 +242,7 @@ class TestFormToolsets:toolset, _ = form_toolsets([tool], tool_invoker)pydantic_tool = toolset.tools["search_docs"]- exposed_schema = pydantic_tool.function_schema.json_schema-- assert exposed_schema["properties"] == schema["properties"]- assert exposed_schema.get("required") == ["query"]- assert "_tool_name" not in exposed_schema.get("properties", {})+ assert pydantic_tool.tool_def.parameters_json_schema == schemaclass TestConvertToPydanticMessages: