Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
py 1,151 lines 38.8 KB
Raw
1 """Test Gemini provider functionality.
2
3 Verifies:
4 - Provider initialization and attributes
5 - wrap_tool returns Python callables with correct metadata
6 - wrap_tools returns list of callables
7 - Callables are AFC-compatible (work with google-genai's function map)
8 - handle_response dispatches function calls via stored executors (backward compat)
9 and runs each call for the user or session the caller passes
10 - _process_execution_result helper
11 """
12
13 import inspect
14 from unittest.mock import MagicMock, Mock, PropertyMock, patch
15
16 import pytest
17
18 from composio.client.types import Tool, tool_list_response
19 from composio.core.models.base import allow_tracking
20 from composio.core.provider import AgenticProvider
21
22 try:
23 import composio_gemini as _composio_gemini # noqa: F401
24
25 HAS_COMPOSIO_GEMINI = True
26 except ImportError:
27 HAS_COMPOSIO_GEMINI = False
28
29 try:
30 from google.genai import types as genai_types
31
32 HAS_GENAI = True
33 except ImportError:
34 genai_types = None # type: ignore[assignment]
35 HAS_GENAI = False
36
37 pytestmark = [
38 pytest.mark.gemini,
39 pytest.mark.skipif(
40 not HAS_COMPOSIO_GEMINI, reason="composio_gemini package not installed"
41 ),
42 ]
43
44 requires_genai = pytest.mark.skipif(
45 not HAS_GENAI, reason="google-genai package not installed"
46 )
47
48
49 @pytest.fixture(autouse=True)
50 def disable_telemetry():
51 """Disable telemetry for all tests to prevent thread issues."""
52 token = allow_tracking.set(False)
53 yield
54 allow_tracking.reset(token)
55
56
57 def create_mock_tool(
58 slug: str,
59 toolkit_slug: str,
60 version: str = "12012025_00",
61 input_parameters: dict | None = None,
62 description: str = "Test tool for provider testing",
63 ) -> Tool:
64 """Create a mock tool for testing."""
65 return Tool(
66 name=f"Test {slug}",
67 slug=slug,
68 description=description,
69 input_parameters=input_parameters
70 or {"type": "object", "properties": {}, "required": []},
71 output_parameters={},
72 available_versions=[version],
73 version=version,
74 scopes=[],
75 toolkit=tool_list_response.ItemToolkit(
76 name=toolkit_slug.title(), slug=toolkit_slug, logo=""
77 ),
78 deprecated=tool_list_response.ItemDeprecated(
79 available_versions=[version],
80 displayName=f"Test {slug}",
81 version=version,
82 toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
83 is_deprecated=False,
84 ),
85 is_deprecated=False,
86 no_auth=False,
87 tags=[],
88 )
89
90
91 def create_mock_execute_tool():
92 """Create a mock execute_tool function matching AgenticProviderExecuteFn."""
93 mock_fn = Mock()
94 mock_fn.return_value = {
95 "data": {"result": "success"},
96 "error": None,
97 "successful": True,
98 }
99 return mock_fn
100
101
102 # ---------------------------------------------------------------------------
103 # Initialization
104 # ---------------------------------------------------------------------------
105
106
107 class TestGeminiProviderInitialization:
108 def test_initialization(self):
109 from composio_gemini import GeminiProvider
110
111 provider = GeminiProvider()
112 assert provider is not None
113 assert provider.name == "gemini"
114 assert isinstance(provider, AgenticProvider)
115
116 def test_has_no_wrapped_tools_on_init(self):
117 from composio_gemini import GeminiProvider
118
119 provider = GeminiProvider()
120 assert provider._wrapped_tools == {}
121
122
123 # ---------------------------------------------------------------------------
124 # wrap_tool – callable creation
125 # ---------------------------------------------------------------------------
126
127
128 class TestWrapTool:
129 def test_returns_callable(self):
130 from composio_gemini import GeminiProvider
131
132 provider = GeminiProvider()
133 tool = create_mock_tool("GITHUB_STAR_REPO", "github")
134 result = provider.wrap_tool(tool, create_mock_execute_tool())
135 assert callable(result)
136 assert inspect.isfunction(result)
137
138 def test_callable_has_correct_name(self):
139 from composio_gemini import GeminiProvider
140
141 provider = GeminiProvider()
142 tool = create_mock_tool("GITHUB_STAR_REPO", "github")
143 result = provider.wrap_tool(tool, create_mock_execute_tool())
144 assert result.__name__ == "GITHUB_STAR_REPO"
145
146 def test_callable_has_correct_doc(self):
147 from composio_gemini import GeminiProvider
148
149 provider = GeminiProvider()
150 tool = create_mock_tool(
151 "GITHUB_STAR_REPO", "github", description="Star a GitHub repository"
152 )
153 result = provider.wrap_tool(tool, create_mock_execute_tool())
154 assert result.__doc__ == "Star a GitHub repository"
155
156 def test_callable_has_typed_signature(self):
157 from composio_gemini import GeminiProvider
158
159 provider = GeminiProvider()
160 tool = create_mock_tool(
161 "GITHUB_CREATE_ISSUE",
162 "github",
163 input_parameters={
164 "type": "object",
165 "properties": {
166 "title": {"type": "string"},
167 "body": {"type": "string"},
168 },
169 "required": ["title"],
170 },
171 )
172 result = provider.wrap_tool(tool, create_mock_execute_tool())
173
174 sig = inspect.signature(result)
175 assert "title" in sig.parameters
176 assert "body" in sig.parameters
177 assert sig.parameters["title"].annotation is str
178 assert sig.parameters["body"].annotation is str
179
180 def test_callable_has_typed_free_form_object_argument(self):
181 from composio_gemini import GeminiProvider
182
183 provider = GeminiProvider()
184 tool = create_mock_tool(
185 "PROCESS_PAYLOAD",
186 "test",
187 input_parameters={
188 "type": "object",
189 "properties": {"payload": {"type": "object"}},
190 "required": ["payload"],
191 },
192 )
193
194 result = provider.wrap_tool(tool, create_mock_execute_tool())
195 parameter = inspect.signature(result).parameters["payload"]
196
197 # Plain `dict`: AFC calls `isinstance` with each value's annotation, and
198 # `Dict[str, Any]` makes that raise for every nested value.
199 assert parameter.annotation is dict
200 assert result.__annotations__["payload"] is dict
201
202 def test_callable_has_annotations(self):
203 from composio_gemini import GeminiProvider
204
205 provider = GeminiProvider()
206 tool = create_mock_tool(
207 "TEST_TOOL",
208 "test",
209 input_parameters={
210 "type": "object",
211 "properties": {"name": {"type": "string"}},
212 "required": ["name"],
213 },
214 )
215 result = provider.wrap_tool(tool, create_mock_execute_tool())
216
217 assert "name" in result.__annotations__
218 assert result.__annotations__["name"] is str
219 assert result.__annotations__["return"] is dict
220
221 def test_stores_executor(self):
222 from composio_gemini import GeminiProvider
223
224 provider = GeminiProvider()
225 tool = create_mock_tool("GITHUB_STAR_REPO", "github")
226 execute_tool = create_mock_execute_tool()
227 provider.wrap_tool(tool, execute_tool)
228
229 assert list(provider._wrapped_tools) == [execute_tool]
230 wrapped = provider._wrapped_tools[execute_tool]["GITHUB_STAR_REPO"]
231 assert wrapped.execute_tool is execute_tool
232
233 def test_callable_executes_correctly(self):
234 from composio_gemini import GeminiProvider
235
236 provider = GeminiProvider()
237 tool = create_mock_tool(
238 "MY_TOOL",
239 "test",
240 input_parameters={
241 "type": "object",
242 "properties": {"query": {"type": "string"}},
243 "required": ["query"],
244 },
245 )
246 execute_tool = create_mock_execute_tool()
247 func = provider.wrap_tool(tool, execute_tool)
248
249 result = func(query="hello")
250 execute_tool.assert_called_once_with("MY_TOOL", {"query": "hello"})
251 # _process_execution_result extracts data when successful
252 assert result == {"result": "success"}
253
254 def test_callable_processes_error_result(self):
255 from composio_gemini import GeminiProvider
256
257 provider = GeminiProvider()
258 tool = create_mock_tool(
259 "MY_TOOL",
260 "test",
261 input_parameters={
262 "type": "object",
263 "properties": {"q": {"type": "string"}},
264 "required": [],
265 },
266 )
267 execute_tool = Mock()
268 execute_tool.return_value = {
269 "data": {},
270 "error": "Auth failed",
271 "successful": False,
272 }
273 func = provider.wrap_tool(tool, execute_tool)
274 result = func(q="test")
275 assert result["error"] == "Auth failed"
276
277 def test_callable_converts_pydantic_args_to_dicts(self):
278 """AFC may pass Pydantic GeneratedModel instances as kwargs; they must be
279 converted to plain dicts before reaching execute_tool so the Composio
280 API can JSON-serialize them.
281 """
282 from composio_gemini import GeminiProvider
283 from pydantic import BaseModel
284
285 class FakeQuery(BaseModel):
286 use_case: str = ""
287 known_fields: str = ""
288
289 provider = GeminiProvider()
290 tool = create_mock_tool(
291 "COMPOSIO_SEARCH_TOOLS",
292 "composio",
293 input_parameters={
294 "type": "object",
295 "properties": {
296 "queries": {
297 "type": "array",
298 "items": {
299 "type": "object",
300 "title": "Query",
301 "properties": {
302 "use_case": {"type": "string"},
303 "known_fields": {"type": "string"},
304 },
305 },
306 },
307 },
308 "required": [],
309 },
310 )
311 execute_tool = create_mock_execute_tool()
312 func = provider.wrap_tool(tool, execute_tool)
313
314 # Simulate what the SDK does: pass Pydantic model instances
315 func(queries=[FakeQuery(use_case="summarize email")])
316
317 call_args = execute_tool.call_args[0][1]
318 # The queries value must be a plain list of dicts, not Pydantic models
319 assert isinstance(call_args["queries"], list)
320 assert isinstance(call_args["queries"][0], dict)
321 assert call_args["queries"][0]["use_case"] == "summarize email"
322
323 @pytest.mark.parametrize(
324 ("property_schema", "invalid", "valid"),
325 [
326 (
327 {"minLength": 2, "required": ["x"]},
328 {},
329 {"x": 1},
330 ),
331 (
332 {"type": "array", "contains": {"const": 1}},
333 [2],
334 [1, 2],
335 ),
336 (
337 {
338 "type": "number",
339 "if": {"minimum": 0},
340 "then": {"maximum": 10},
341 },
342 20,
343 5,
344 ),
345 ],
346 ids=["typeless-object", "contains", "conditional"],
347 )
348 def test_callable_validates_assertions_missing_from_function_declaration(
349 self,
350 property_schema,
351 invalid,
352 valid,
353 ):
354 """AFC type generation is lossy, so the callable validates the source schema."""
355 from composio_gemini import GeminiProvider
356 from pydantic import ValidationError
357
358 provider = GeminiProvider()
359 tool = create_mock_tool(
360 "VALIDATE_SOURCE_SCHEMA",
361 "test",
362 input_parameters={
363 "type": "object",
364 "properties": {"value": property_schema},
365 "required": ["value"],
366 },
367 )
368 execute_tool = create_mock_execute_tool()
369 func = provider.wrap_tool(tool, execute_tool)
370
371 with pytest.raises(ValidationError):
372 func(value=invalid)
373 execute_tool.assert_not_called()
374
375 func(value=valid)
376 assert execute_tool.call_args.args[1] == {"value": valid}
377
378 def test_array_param_has_parameterized_type(self):
379 """Array parameters must produce List[X], not bare List.
380
381 The google-genai SDK rejects bare List because it generates
382 {"type": "ARRAY"} without "items".
383 """
384 import typing
385
386 from composio_gemini import GeminiProvider
387
388 provider = GeminiProvider()
389 tool = create_mock_tool(
390 "TOOL_WITH_ARRAY",
391 "test",
392 input_parameters={
393 "type": "object",
394 "properties": {
395 "tags": {"type": "array", "items": {"type": "string"}},
396 "name": {"type": "string"},
397 },
398 "required": ["name"],
399 },
400 )
401 func = provider.wrap_tool(tool, create_mock_execute_tool())
402
403 sig = inspect.signature(func)
404 tags_annotation = sig.parameters["tags"].annotation
405 # Must be a parameterized generic like List[str], not bare list
406 assert typing.get_origin(tags_annotation) is list
407 assert typing.get_args(tags_annotation) != ()
408
409 def test_reserved_keyword_handling(self):
410 """Parameters named 'for' or 'async' are substituted and reinstated."""
411 from composio_gemini import GeminiProvider
412
413 provider = GeminiProvider()
414 tool = create_mock_tool(
415 "TOOL_WITH_RESERVED",
416 "test",
417 input_parameters={
418 "type": "object",
419 "properties": {
420 "for": {"type": "string"},
421 "name": {"type": "string"},
422 },
423 "required": [],
424 },
425 )
426 execute_tool = create_mock_execute_tool()
427 func = provider.wrap_tool(tool, execute_tool)
428
429 # The callable's signature should use the cleaned name
430 sig = inspect.signature(func)
431 assert "for_rs" in sig.parameters
432 assert "for" not in sig.parameters
433
434 # When called with the cleaned name, it reinstates the original
435 func(for_rs="test_value", name="hello")
436 call_args = execute_tool.call_args
437 assert call_args[0][1]["for"] == "test_value"
438 assert call_args[0][1]["name"] == "hello"
439
440 def test_wrap_same_tool_twice_does_not_corrupt_schema(self):
441 """Wrapping the same Tool object twice must not corrupt the schema."""
442 from composio_gemini import GeminiProvider
443
444 provider = GeminiProvider()
445 tool = create_mock_tool(
446 "TOOL_RESERVED",
447 "test",
448 input_parameters={
449 "type": "object",
450 "properties": {
451 "for": {"type": "string"},
452 "name": {"type": "string"},
453 },
454 "required": ["for"],
455 },
456 )
457 execute_tool = create_mock_execute_tool()
458
459 func1 = provider.wrap_tool(tool, execute_tool)
460 func2 = provider.wrap_tool(tool, execute_tool)
461
462 # Both callables must have the cleaned parameter
463 sig1 = inspect.signature(func1)
464 sig2 = inspect.signature(func2)
465 assert "for_rs" in sig1.parameters
466 assert "for_rs" in sig2.parameters
467
468 # Original tool schema must be unchanged
469 assert "for" in tool.input_parameters["properties"]
470 assert "for_rs" not in tool.input_parameters["properties"]
471
472 def test_reserved_keyword_stays_required(self):
473 """A reserved keyword listed in 'required' must remain required after renaming."""
474 from composio_gemini import GeminiProvider
475
476 provider = GeminiProvider()
477 tool = create_mock_tool(
478 "TOOL_REQUIRED_RESERVED",
479 "test",
480 input_parameters={
481 "type": "object",
482 "properties": {
483 "for": {"type": "string"},
484 "name": {"type": "string"},
485 },
486 "required": ["for", "name"],
487 },
488 )
489 func = provider.wrap_tool(tool, create_mock_execute_tool())
490
491 sig = inspect.signature(func)
492 # Both params should be required (no default value)
493 assert sig.parameters["for_rs"].default is inspect.Parameter.empty
494 assert sig.parameters["name"].default is inspect.Parameter.empty
495
496
497 # ---------------------------------------------------------------------------
498 # wrap_tools
499 # ---------------------------------------------------------------------------
500
501
502 class TestWrapTools:
503 def test_returns_list_of_callables(self):
504 from composio_gemini import GeminiProvider
505
506 provider = GeminiProvider()
507 tools = [
508 create_mock_tool("GITHUB_STAR_REPO", "github"),
509 create_mock_tool("GMAIL_SEND_EMAIL", "gmail"),
510 ]
511 result = provider.wrap_tools(tools, create_mock_execute_tool())
512
513 assert len(result) == 2
514 assert all(inspect.isfunction(f) for f in result)
515
516 def test_stores_all_executors(self):
517 from composio_gemini import GeminiProvider
518
519 provider = GeminiProvider()
520 tools = [
521 create_mock_tool("GITHUB_STAR_REPO", "github"),
522 create_mock_tool("GMAIL_SEND_EMAIL", "gmail"),
523 ]
524 execute_tool = create_mock_execute_tool()
525 provider.wrap_tools(tools, execute_tool)
526
527 assert set(provider._wrapped_tools[execute_tool]) == {
528 "GITHUB_STAR_REPO",
529 "GMAIL_SEND_EMAIL",
530 }
531
532 def test_empty_list(self):
533 from composio_gemini import GeminiProvider
534
535 provider = GeminiProvider()
536 result = provider.wrap_tools([], create_mock_execute_tool())
537 assert result == []
538
539
540 # ---------------------------------------------------------------------------
541 # AFC compatibility (requires google-genai)
542 # ---------------------------------------------------------------------------
543
544
545 AFC_CASES = [
546 pytest.param(
547 {"query": {"type": "string"}, "max_results": {"type": "integer"}},
548 ["query"],
549 {"query": "is:unread"},
550 {"query": {"type": "STRING"}, "max_results": {"type": "INTEGER"}},
551 id="omitted-optional",
552 ),
553 pytest.param(
554 {"order": {"type": "string", "enum": ["asc", "desc"]}},
555 ["order"],
556 {"order": "asc"},
557 {"order": {"type": "STRING"}},
558 id="enum",
559 ),
560 pytest.param(
561 {"order": {"enum": ["asc", "desc"]}},
562 ["order"],
563 {"order": "asc"},
564 {"order": {"type": "STRING"}},
565 id="typeless-enum",
566 ),
567 pytest.param(
568 {"slug": {"type": "string", "pattern": "^[a-z_]+$"}},
569 ["slug"],
570 {"slug": "a_b"},
571 {"slug": {"type": "STRING"}},
572 id="pattern",
573 ),
574 pytest.param(
575 {"n": {"type": "integer", "minimum": 1}},
576 ["n"],
577 {"n": 3},
578 {"n": {"type": "INTEGER"}},
579 id="minimum",
580 ),
581 pytest.param(
582 {"v": {"anyOf": [{"type": "integer"}, {"type": "string"}]}},
583 ["v"],
584 {"v": "x"},
585 {"v": {"any_of": [{"type": "INTEGER"}, {"type": "STRING"}], "type": "OBJECT"}},
586 id="any-of",
587 ),
588 pytest.param(
589 {"meta": {"type": "object"}},
590 ["meta"],
591 {"meta": {"x": {"y": 1}}},
592 {"meta": {"type": "OBJECT"}},
593 id="free-form-object",
594 ),
595 pytest.param(
596 {"meta": {"description": "Anything"}},
597 ["meta"],
598 {"meta": {"x": 1}},
599 {"meta": {}},
600 id="typeless-free-form",
601 ),
602 pytest.param(
603 {"c": {"const": "fixed"}},
604 ["c"],
605 {"c": "fixed"},
606 {"c": {"type": "STRING"}},
607 id="const",
608 ),
609 pytest.param(
610 {"version": {"const": 2}},
611 ["version"],
612 {"version": 2},
613 {"version": {"type": "INTEGER"}},
614 id="integer-const",
615 ),
616 pytest.param(
617 {
618 "body": {
619 "type": "object",
620 "properties": {"a": {"type": "string"}, "b": {"type": "integer"}},
621 "required": ["a"],
622 }
623 },
624 ["body"],
625 {"body": {"a": "x"}},
626 {
627 "body": {
628 "type": "OBJECT",
629 "properties": {"a": {"type": "STRING"}, "b": {"type": "INTEGER"}},
630 "required": ["a", "b"],
631 }
632 },
633 id="nested-omitted-optional",
634 ),
635 pytest.param(
636 {
637 "items": {
638 "type": "array",
639 "items": {
640 "type": "object",
641 "properties": {"k": {"type": "string"}, "v": {"type": "string"}},
642 "required": ["k"],
643 },
644 }
645 },
646 ["items"],
647 {"items": [{"k": "a"}, {"k": "b", "v": "c"}]},
648 {
649 "items": {
650 "type": "ARRAY",
651 "items": {
652 "type": "OBJECT",
653 "properties": {"k": {"type": "STRING"}, "v": {"type": "STRING"}},
654 "required": ["k", "v"],
655 },
656 }
657 },
658 id="array-of-objects",
659 ),
660 ]
661 """AFC round-trip cases: source properties and required names, the arguments
662 the model returns, and the ``properties`` Gemini is declared. The declarations
663 match the ones from before #4316, except that a typeless ``const``/``enum`` of
664 integers is declared as an integer and a typeless free-form property declares
665 no type, instead of both being declared as strings the conversion then
666 refused."""
667
668
669 def _wrap_afc_tool(properties, required):
670 from composio_gemini import GeminiProvider
671
672 tool = create_mock_tool(
673 "AFC_TOOL",
674 "test",
675 input_parameters={
676 "type": "object",
677 "properties": properties,
678 "required": required,
679 },
680 )
681 execute_tool = create_mock_execute_tool()
682 return GeminiProvider().wrap_tool(tool, execute_tool), execute_tool
683
684
685 @requires_genai
686 class TestAFCCompatibility:
687 """Verify callables work with google-genai's AFC pipeline."""
688
689 def _wrap_one_tool(self):
690 from composio_gemini import GeminiProvider
691
692 provider = GeminiProvider()
693 tool = create_mock_tool(
694 "GITHUB_STAR_REPO",
695 "github",
696 input_parameters={
697 "type": "object",
698 "properties": {
699 "owner": {"type": "string"},
700 "repo": {"type": "string"},
701 },
702 "required": ["owner", "repo"],
703 },
704 description="Star a GitHub repository",
705 )
706 func = provider.wrap_tool(tool, create_mock_execute_tool())
707 return func
708
709 def test_callable_is_function(self):
710 """inspect.isfunction must be True for t_tool() to call from_callable."""
711 func = self._wrap_one_tool()
712 assert inspect.isfunction(func)
713
714 def test_callable_not_afc_incompatible(self):
715 """Callables should not be flagged as AFC-incompatible."""
716 func = self._wrap_one_tool()
717 # The SDK checks: isinstance(tool, types.Tool) and tool.function_declarations
718 # A callable is not a types.Tool, so it should not be flagged.
719 assert not isinstance(func, genai_types.Tool)
720
721 def test_callable_enters_function_map(self):
722 """The callable should appear in the function map built by the SDK."""
723 func = self._wrap_one_tool()
724 # Simulate what get_function_map does: callable(tool) -> function_map[tool.__name__]
725 assert callable(func)
726 function_map = {}
727 if callable(func):
728 function_map[func.__name__] = func
729 assert "GITHUB_STAR_REPO" in function_map
730
731 @pytest.mark.parametrize(
732 ("properties", "required", "arguments", "declared"),
733 AFC_CASES,
734 )
735 def test_afc_executes_arguments_as_returned(
736 self, properties, required, arguments, declared
737 ):
738 """AFC converts each argument with its annotation before calling the tool."""
739 from google.genai import _extra_utils
740
741 func, execute_tool = _wrap_afc_tool(properties, required)
742
743 _extra_utils.invoke_function_from_dict_args(arguments, func)
744
745 execute_tool.assert_called_once_with("AFC_TOOL", arguments)
746
747 @pytest.mark.parametrize(
748 ("properties", "required", "arguments", "declared"),
749 AFC_CASES,
750 )
751 def test_afc_declares_the_argument_schema(
752 self, properties, required, arguments, declared
753 ):
754 """Gemini receives the same declaration as before the signature carried
755 validators, and a typeless ``enum``/``const`` keeps its values' type."""
756 from google import genai
757
758 func, _ = _wrap_afc_tool(properties, required)
759
760 declaration = genai_types.FunctionDeclaration.from_callable(
761 client=genai.Client(api_key="test")._api_client, callable=func
762 )
763
764 assert declaration.parameters is not None
765 parameters = declaration.parameters.model_dump(exclude_none=True, mode="json")
766 assert parameters["properties"] == declared
767 assert set(required) <= set(parameters["required"])
768
769 def test_afc_still_validates_the_source_schema(self):
770 from composio_gemini import GeminiProvider
771 from google.genai import _extra_utils, errors
772
773 provider = GeminiProvider()
774 tool = create_mock_tool(
775 "AFC_TOOL",
776 "test",
777 input_parameters={
778 "type": "object",
779 "properties": {"slug": {"type": "string", "pattern": "^[a-z_]+$"}},
780 "required": ["slug"],
781 },
782 )
783 execute_tool = create_mock_execute_tool()
784 func = provider.wrap_tool(tool, execute_tool)
785
786 with pytest.raises(errors.FunctionInvocationError):
787 _extra_utils.invoke_function_from_dict_args({"slug": "Not Valid"}, func)
788 execute_tool.assert_not_called()
789
790 def test_callables_in_generate_content_config(self):
791 """Wrapped callables can be passed to GenerateContentConfig without error."""
792 from composio_gemini import GeminiProvider
793
794 provider = GeminiProvider()
795 tools = [
796 create_mock_tool(
797 "GITHUB_STAR_REPO",
798 "github",
799 input_parameters={
800 "type": "object",
801 "properties": {
802 "owner": {"type": "string"},
803 "repo": {"type": "string"},
804 },
805 "required": ["owner", "repo"],
806 },
807 ),
808 ]
809 wrapped = provider.wrap_tools(tools, create_mock_execute_tool())
810
811 # This should not raise
812 config = genai_types.GenerateContentConfig(tools=wrapped)
813 assert config.tools is not None
814 assert len(config.tools) == 1
815
816
817 # ---------------------------------------------------------------------------
818 # handle_response (backward compat)
819 # ---------------------------------------------------------------------------
820
821
822 @requires_genai
823 class TestHandleResponse:
824 def _create_mock_response(self, function_calls: list[tuple[str, dict]]):
825 parts = []
826 for name, args in function_calls:
827 part = MagicMock()
828 part.function_call = MagicMock()
829 part.function_call.name = name
830 part.function_call.args = args
831 parts.append(part)
832
833 response = MagicMock()
834 response.candidates = [MagicMock()]
835 response.candidates[0].content = MagicMock()
836 response.candidates[0].content.parts = parts
837 return response
838
839 def test_executes_function_call(self):
840 from composio_gemini import GeminiProvider
841
842 provider = GeminiProvider()
843 tool = create_mock_tool("GITHUB_STAR_REPO", "github")
844 execute_tool = create_mock_execute_tool()
845 provider.wrap_tools([tool], execute_tool)
846
847 response = self._create_mock_response(
848 [("GITHUB_STAR_REPO", {"repo": "composio/composio"})]
849 )
850 function_responses, executed = provider.handle_response(response)
851
852 assert executed is True
853 assert len(function_responses) == 1
854 execute_tool.assert_called_once_with(
855 slug="GITHUB_STAR_REPO",
856 arguments={"repo": "composio/composio"},
857 )
858
859 def test_rejects_invalid_arguments_like_afc(self):
860 """Manual function calling validates the source schema like the AFC callable."""
861 from composio_gemini import GeminiProvider
862 from pydantic import ValidationError
863
864 provider = GeminiProvider()
865 tool = create_mock_tool(
866 "VALIDATE_SOURCE_SCHEMA",
867 "test",
868 input_parameters={
869 "type": "object",
870 "properties": {"value": {"type": "integer", "minimum": 1}},
871 "required": ["value"],
872 },
873 )
874 execute_tool = create_mock_execute_tool()
875 provider.wrap_tools([tool], execute_tool)
876
877 response = self._create_mock_response(
878 [("VALIDATE_SOURCE_SCHEMA", {"value": 0})]
879 )
880 with pytest.raises(ValidationError):
881 provider.handle_response(response)
882 execute_tool.assert_not_called()
883
884 response = self._create_mock_response(
885 [("VALIDATE_SOURCE_SCHEMA", {"value": 3})]
886 )
887 _responses, executed = provider.handle_response(response)
888 assert executed is True
889 execute_tool.assert_called_once_with(
890 slug="VALIDATE_SOURCE_SCHEMA", arguments={"value": 3}
891 )
892
893 def test_no_function_calls(self):
894 from composio_gemini import GeminiProvider
895
896 provider = GeminiProvider()
897 response = MagicMock()
898 response.candidates = [MagicMock()]
899 text_part = MagicMock()
900 text_part.function_call = None
901 response.candidates[0].content = MagicMock()
902 response.candidates[0].content.parts = [text_part]
903
904 function_responses, executed = provider.handle_response(response)
905 assert executed is False
906 assert function_responses == []
907
908 def test_unknown_function_name(self):
909 from composio_gemini import GeminiProvider
910
911 provider = GeminiProvider()
912 response = self._create_mock_response([("UNKNOWN_TOOL", {"param": "value"})])
913 function_responses, executed = provider.handle_response(response)
914 assert executed is False
915 assert function_responses == []
916
917 def test_no_candidates(self):
918 from composio_gemini import GeminiProvider
919
920 provider = GeminiProvider()
921 response = MagicMock()
922 response.candidates = []
923 function_responses, executed = provider.handle_response(response)
924 assert executed is False
925 assert function_responses == []
926
927
928 @requires_genai
929 class TestHandleResponseUser:
930 """A function call runs for the user its tools were fetched for (#5112).
931
932 These go through ``Composio.tools.get()`` so the execute function each
933 fetch binds is the one the SDK really builds.
934 """
935
936 GMAIL = "GMAIL_FETCH_EMAILS"
937 NOTION = "NOTION_CREATE_PAGE"
938 SCHEMA = {
939 "type": "object",
940 "properties": {"query": {"type": "string"}},
941 "required": ["query"],
942 }
943
944 def _tool(self, slug: str) -> Tool:
945 return create_mock_tool(
946 slug, slug.split("_")[0].lower(), input_parameters=self.SCHEMA
947 )
948
949 @pytest.fixture
950 def composio(self):
951 from composio import Composio
952 from composio_gemini import GeminiProvider
953
954 composio = Composio(
955 provider=GeminiProvider(), api_key="test-key", allow_tracking=False
956 )
957 tools = {slug: self._tool(slug) for slug in (self.GMAIL, self.NOTION)}
958
959 def execute(*, tool_slug, arguments, user_id, **_kwargs):
960 return MagicMock(
961 **{
962 "model_dump.return_value": {
963 "data": {"user_id": user_id},
964 "error": None,
965 "successful": True,
966 }
967 }
968 )
969
970 with (
971 patch.object(
972 composio.tools,
973 "get_raw_composio_tools",
974 side_effect=lambda **kwargs: [tools[s] for s in kwargs["tools"]],
975 ),
976 patch.object(
977 type(composio.tools._client),
978 "without_retries",
979 new_callable=PropertyMock,
980 ) as without_retries,
981 ):
982 without_retries.return_value.tools.execute.side_effect = execute
983 yield composio
984
985 @staticmethod
986 def _response(*slugs: str):
987 return genai_types.GenerateContentResponse(
988 candidates=[
989 genai_types.Candidate(
990 content=genai_types.Content(
991 role="model",
992 parts=[
993 genai_types.Part(
994 function_call=genai_types.FunctionCall(
995 name=slug, args={"query": "is:unread"}
996 )
997 )
998 for slug in slugs
999 ],
1000 )
1001 )
1002 ]
1003 )
1004
1005 def _executed_for(self, composio, *slugs: str, **kwargs) -> list[str]:
1006 parts, executed = composio.provider.handle_response(
1007 self._response(*slugs), **kwargs
1008 )
1009 assert executed is True
1010 return [part.function_response.response["user_id"] for part in parts]
1011
1012 def test_explicit_user_wins_over_last_fetch(self, composio):
1013 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1014 composio.tools.get(user_id="bob", tools=[self.GMAIL])
1015
1016 assert self._executed_for(composio, self.GMAIL, user_id="alice") == ["alice"]
1017 assert self._executed_for(composio, self.GMAIL, user_id="bob") == ["bob"]
1018
1019 @pytest.mark.parametrize(
1020 "bob_slug",
1021 [GMAIL, NOTION],
1022 ids=["same-slug", "disjoint-slugs"],
1023 )
1024 def test_rejects_call_without_user_once_two_users_fetched(self, composio, bob_slug):
1025 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1026 composio.tools.get(user_id="bob", tools=[bob_slug])
1027 execute = composio.tools._client.without_retries.tools.execute
1028
1029 with pytest.raises(ValueError, match="user_id="):
1030 composio.provider.handle_response(self._response(self.GMAIL))
1031 execute.assert_not_called()
1032
1033 def test_single_user_without_user_still_works(self, composio):
1034 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1035 composio.tools.get(user_id="alice", tools=[self.NOTION])
1036
1037 assert self._executed_for(composio, self.GMAIL, self.NOTION) == [
1038 "alice",
1039 "alice",
1040 ]
1041
1042 def test_rejects_user_with_no_fetched_tools(self, composio):
1043 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1044
1045 with pytest.raises(ValueError, match="user_id"):
1046 composio.provider.handle_response(
1047 self._response(self.GMAIL), user_id="carol"
1048 )
1049
1050 def test_fetch_during_execution_does_not_switch_user(self, composio):
1051 """Executors are picked before any call runs (review of #5120)."""
1052 composio.tools.get(user_id="alice", tools=[self.GMAIL, self.NOTION])
1053 execute = composio.tools._client.without_retries.tools.execute
1054 alice_execute = execute.side_effect
1055
1056 def fetch_for_bob_then_execute(**kwargs):
1057 execute.side_effect = alice_execute
1058 composio.tools.get(user_id="bob", tools=[self.NOTION])
1059 return alice_execute(**kwargs)
1060
1061 execute.side_effect = fetch_for_bob_then_execute
1062
1063 # Without a user: Bob's fetch mid-response would make a later lookup
1064 # ambiguous, but the response was resolved before it.
1065 assert self._executed_for(composio, self.GMAIL, self.NOTION) == [
1066 "alice",
1067 "alice",
1068 ]
1069 with pytest.raises(ValueError, match="user_id="):
1070 composio.provider.handle_response(self._response(self.GMAIL))
1071
1072 def test_call_is_validated_with_its_own_users_schema(self, composio):
1073 """Each user's schema modifiers check that user's calls (review of #5120)."""
1074 from composio import schema_modifier
1075
1076 @schema_modifier(tools=[self.GMAIL])
1077 def starred_only(tool: str, toolkit: str, schema: Tool) -> Tool:
1078 schema.input_parameters = {
1079 **self.SCHEMA,
1080 "properties": {"query": {"type": "string", "enum": ["is:starred"]}},
1081 }
1082 return schema
1083
1084 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1085 composio.tools.get(user_id="bob", tools=[self.GMAIL], modifiers=[starred_only])
1086
1087 assert self._executed_for(composio, self.GMAIL, user_id="alice") == ["alice"]
1088 with pytest.raises(ValueError, match="is:starred"):
1089 composio.provider.handle_response(self._response(self.GMAIL), user_id="bob")
1090
1091 def test_session_runs_through_its_own_executor(self, composio):
1092 """``session=`` keeps the routing ``session.tools()`` installed, which
1093 runs local custom tools in a batch in process."""
1094 from composio.core.provider import bind_execution_target
1095
1096 composio.tools.get(user_id="alice", tools=[self.GMAIL])
1097 session = Mock()
1098 routing_execute = bind_execution_target(
1099 Mock(return_value={"data": {"user_id": "session"}, "successful": True}),
1100 session,
1101 )
1102 composio.provider.wrap_tools([self._tool(self.GMAIL)], routing_execute)
1103
1104 assert self._executed_for(composio, self.GMAIL, session=session) == ["session"]
1105 routing_execute.assert_called_once_with(
1106 slug=self.GMAIL, arguments={"query": "is:unread"}
1107 )
1108 session.execute.assert_not_called()
1109
1110
1111 # ---------------------------------------------------------------------------
1112 # _process_execution_result
1113 # ---------------------------------------------------------------------------
1114
1115
1116 class TestProcessExecutionResult:
1117 def test_non_dict_result(self):
1118 from composio_gemini.provider import _process_execution_result
1119
1120 assert _process_execution_result("hello") == {"result": "hello"}
1121 assert _process_execution_result(42) == {"result": 42}
1122
1123 def test_successful_with_dict_data(self):
1124 from composio_gemini.provider import _process_execution_result
1125
1126 result = _process_execution_result(
1127 {"data": {"key": "value"}, "error": None, "successful": True}
1128 )
1129 assert result == {"key": "value"}
1130
1131 def test_successful_with_non_dict_data(self):
1132 from composio_gemini.provider import _process_execution_result
1133
1134 result = _process_execution_result(
1135 {"data": "plain text", "error": None, "successful": True}
1136 )
1137 assert result == {"result": "plain text"}
1138
1139 def test_failed_result(self):
1140 from composio_gemini.provider import _process_execution_result
1141
1142 result = _process_execution_result(
1143 {"data": {}, "error": "Something went wrong", "successful": False}
1144 )
1145 assert result["error"] == "Something went wrong"
1146
1147 def test_passthrough_dict(self):
1148 from composio_gemini.provider import _process_execution_result
1149
1150 result = _process_execution_result({"custom": "response"})
1151 assert result == {"custom": "response"}