Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 11:00 UTC
fix(gemini): declare typeless const and enum arguments with their type
Mapping typing.Any to object declared a property without a type, such as {"enum": ["asc", "desc"]} or {"const": "fixed"}, with no type at all, where it was a string before #4316. Declare a typeless const or enum with the type its values share, and keep object only for free-form properties, which the conversion then accepts. Pin the declared properties for every AFC case so the declaration Gemini receives is checked alongside the round trip.
jkomyno committed
Sep 29, 2026 at 15:32 UTC
521fba51844dd32574117269f7c1ef062cf5bb23
2 files changed
+199
-91
python/providers/gemini/composio_gemini/provider.py
+31
-4
@@ -58,7 +58,25 @@ def _to_serializable(value: t.Any) -> t.Any:
58
return value
59
60
61
-def _afc_annotation(annotation: t.Any) -> t.Any:
61
+def _literal_type(schema: t.Any) -> type:
62
+ """The Python type shared by a typeless property's ``const``/``enum``
63
+ values, or ``object`` when there is none."""
64
+ if not isinstance(schema, dict):
65
+ return object
66
+ if "const" in schema:
67
+ values = [schema["const"]]
68
+ elif isinstance(schema.get("enum"), list) and schema["enum"]:
69
+ values = schema["enum"]
70
+ else:
71
+ return object
72
+ # ``bool`` first: it is a subclass of ``int``.
73
+ for literal_type in (bool, str, int, float):
74
+ if all(type(value) is literal_type for value in values):
75
+ return literal_type
76
+ return object
77
+
78
+
79
+def _afc_annotation(annotation: t.Any, schema: t.Any = None) -> t.Any:
80
"""Return an annotation google-genai AFC can convert arguments with.
81
82
AFC reads the callable's signature both to declare the function and to
@@ -67,11 +85,17 @@ def _afc_annotation(annotation: t.Any) -> t.Any:
85
parameterized ``Dict`` and ``typing.Any``, so any tool using them failed
86
before it ran. The plain annotations declare the same function; the source
87
schema is still enforced by ``args_schema``.
88
+
89
+ A property without a ``type`` is ``typing.Any``. A bare ``const`` or
90
+ ``enum`` is declared with the type of its values, so ``{"enum": ["asc",
91
+ "desc"]}`` stays a string as it was before the signature carried
92
+ validators. Anything else becomes ``object``, which declares no type and
93
+ lets the conversion accept any value.
94
"""
95
origin = t.get_origin(annotation)
96
args = t.get_args(annotation)
97
if origin is t.Annotated:
74
- return _afc_annotation(args[0])
98
+ return _afc_annotation(args[0], schema)
99
if origin is list and args:
100
return t.List[_afc_annotation(args[0])] # type: ignore[misc]
101
if origin is dict:
@@ -79,7 +103,7 @@ def _afc_annotation(annotation: t.Any) -> t.Any:
103
if origin in (t.Union, pytypes.UnionType):
104
return t.Union[tuple(_afc_annotation(arg) for arg in args)]
105
if annotation is t.Any:
82
- return object
106
+ return _literal_type(schema)
107
return annotation
108
109
@@ -165,8 +189,11 @@ class GeminiProvider(AgenticProvider[t.Callable, list[t.Callable]], name="gemini
189
# parameterized generics (e.g. List[str] instead of bare List).
190
# The google-genai SDK requires parameterized array types — bare List
191
# generates {"type": "ARRAY"} without "items", which the API rejects.
192
+ properties = aliases.schema.get("properties") or {}
193
sig_params = [
169
- param.replace(annotation=_afc_annotation(param.annotation))
194
+ param.replace(
195
+ annotation=_afc_annotation(param.annotation, properties.get(param.name))
196
+ )
197
for param in get_pydantic_signature_format_from_schema_params(
198
schema_params=aliases.schema,
199
skip_default=True,
python/tests/test_gemini_provider.py
+168
-87
@@ -540,6 +540,146 @@ class TestWrapTools:
540
# ---------------------------------------------------------------------------
541
542
543
+AFC_CASES = [
544
+ pytest.param(
545
+ {"query": {"type": "string"}, "max_results": {"type": "integer"}},
546
+ ["query"],
547
+ {"query": "is:unread"},
548
+ {"query": {"type": "STRING"}, "max_results": {"type": "INTEGER"}},
549
+ id="omitted-optional",
550
+ ),
551
+ pytest.param(
552
+ {"order": {"type": "string", "enum": ["asc", "desc"]}},
553
+ ["order"],
554
+ {"order": "asc"},
555
+ {"order": {"type": "STRING"}},
556
+ id="enum",
557
+ ),
558
+ pytest.param(
559
+ {"order": {"enum": ["asc", "desc"]}},
560
+ ["order"],
561
+ {"order": "asc"},
562
+ {"order": {"type": "STRING"}},
563
+ id="typeless-enum",
564
+ ),
565
+ pytest.param(
566
+ {"slug": {"type": "string", "pattern": "^[a-z_]+$"}},
567
+ ["slug"],
568
+ {"slug": "a_b"},
569
+ {"slug": {"type": "STRING"}},
570
+ id="pattern",
571
+ ),
572
+ pytest.param(
573
+ {"n": {"type": "integer", "minimum": 1}},
574
+ ["n"],
575
+ {"n": 3},
576
+ {"n": {"type": "INTEGER"}},
577
+ id="minimum",
578
+ ),
579
+ pytest.param(
580
+ {"v": {"anyOf": [{"type": "integer"}, {"type": "string"}]}},
581
+ ["v"],
582
+ {"v": "x"},
583
+ {"v": {"any_of": [{"type": "INTEGER"}, {"type": "STRING"}], "type": "OBJECT"}},
584
+ id="any-of",
585
+ ),
586
+ pytest.param(
587
+ {"meta": {"type": "object"}},
588
+ ["meta"],
589
+ {"meta": {"x": {"y": 1}}},
590
+ {"meta": {"type": "OBJECT"}},
591
+ id="free-form-object",
592
+ ),
593
+ pytest.param(
594
+ {"meta": {"description": "Anything"}},
595
+ ["meta"],
596
+ {"meta": {"x": 1}},
597
+ {"meta": {}},
598
+ id="typeless-free-form",
599
+ ),
600
+ pytest.param(
601
+ {"c": {"const": "fixed"}},
602
+ ["c"],
603
+ {"c": "fixed"},
604
+ {"c": {"type": "STRING"}},
605
+ id="const",
606
+ ),
607
+ pytest.param(
608
+ {"version": {"const": 2}},
609
+ ["version"],
610
+ {"version": 2},
611
+ {"version": {"type": "INTEGER"}},
612
+ id="integer-const",
613
+ ),
614
+ pytest.param(
615
+ {
616
+ "body": {
617
+ "type": "object",
618
+ "properties": {"a": {"type": "string"}, "b": {"type": "integer"}},
619
+ "required": ["a"],
620
+ }
621
+ },
622
+ ["body"],
623
+ {"body": {"a": "x"}},
624
+ {
625
+ "body": {
626
+ "type": "OBJECT",
627
+ "properties": {"a": {"type": "STRING"}, "b": {"type": "INTEGER"}},
628
+ "required": ["a", "b"],
629
+ }
630
+ },
631
+ id="nested-omitted-optional",
632
+ ),
633
+ pytest.param(
634
+ {
635
+ "items": {
636
+ "type": "array",
637
+ "items": {
638
+ "type": "object",
639
+ "properties": {"k": {"type": "string"}, "v": {"type": "string"}},
640
+ "required": ["k"],
641
+ },
642
+ }
643
+ },
644
+ ["items"],
645
+ {"items": [{"k": "a"}, {"k": "b", "v": "c"}]},
646
+ {
647
+ "items": {
648
+ "type": "ARRAY",
649
+ "items": {
650
+ "type": "OBJECT",
651
+ "properties": {"k": {"type": "STRING"}, "v": {"type": "STRING"}},
652
+ "required": ["k", "v"],
653
+ },
654
+ }
655
+ },
656
+ id="array-of-objects",
657
+ ),
658
+]
659
+"""AFC round-trip cases: source properties and required names, the arguments
660
+the model returns, and the ``properties`` Gemini is declared. The declarations
661
+match the ones from before #4316, except that a typeless ``const``/``enum`` of
662
+integers is declared as an integer and a typeless free-form property declares
663
+no type, instead of both being declared as strings the conversion then
664
+refused."""
665
+
666
+
667
+def _wrap_afc_tool(properties, required):
668
+ from composio_gemini import GeminiProvider
669
+
670
+ tool = create_mock_tool(
671
+ "AFC_TOOL",
672
+ "test",
673
+ input_parameters={
674
+ "type": "object",
675
+ "properties": properties,
676
+ "required": required,
677
+ },
678
+ )
679
+ execute_tool = create_mock_execute_tool()
680
+ return GeminiProvider().wrap_tool(tool, execute_tool), execute_tool
681
+
682
+
683
@requires_genai
684
class TestAFCCompatibility:
685
"""Verify callables work with google-genai's AFC pipeline."""
@@ -587,102 +727,43 @@ class TestAFCCompatibility:
727
assert "GITHUB_STAR_REPO" in function_map
728
729
@pytest.mark.parametrize(
590
- ("properties", "required", "arguments"),
591
- [
592
- (
593
- {"query": {"type": "string"}, "max_results": {"type": "integer"}},
594
- ["query"],
595
- {"query": "is:unread"},
596
- ),
597
- (
598
- {"order": {"type": "string", "enum": ["asc", "desc"]}},
599
- ["order"],
600
- {"order": "asc"},
601
- ),
602
- (
603
- {"slug": {"type": "string", "pattern": "^[a-z_]+$"}},
604
- ["slug"],
605
- {"slug": "a_b"},
606
- ),
607
- ({"n": {"type": "integer", "minimum": 1}}, ["n"], {"n": 3}),
608
- (
609
- {"v": {"anyOf": [{"type": "integer"}, {"type": "string"}]}},
610
- ["v"],
611
- {"v": "x"},
612
- ),
613
- ({"meta": {"type": "object"}}, ["meta"], {"meta": {"x": {"y": 1}}}),
614
- ({"c": {"const": "fixed"}}, ["c"], {"c": "fixed"}),
615
- (
616
- {
617
- "body": {
618
- "type": "object",
619
- "properties": {
620
- "a": {"type": "string"},
621
- "b": {"type": "integer"},
622
- },
623
- "required": ["a"],
624
- }
625
- },
626
- ["body"],
627
- {"body": {"a": "x"}},
628
- ),
629
- (
630
- {
631
- "items": {
632
- "type": "array",
633
- "items": {
634
- "type": "object",
635
- "properties": {
636
- "k": {"type": "string"},
637
- "v": {"type": "string"},
638
- },
639
- "required": ["k"],
640
- },
641
- }
642
- },
643
- ["items"],
644
- {"items": [{"k": "a"}, {"k": "b", "v": "c"}]},
645
- ),
646
- ],
647
- ids=[
648
- "omitted-optional",
649
- "enum",
650
- "pattern",
651
- "minimum",
652
- "any-of",
653
- "free-form-object",
654
- "const",
655
- "nested-omitted-optional",
656
- "array-of-objects",
657
- ],
730
+ ("properties", "required", "arguments", "declared"),
731
+ AFC_CASES,
732
)
659
- def test_afc_executes_arguments_as_returned(self, properties, required, arguments):
733
+ def test_afc_executes_arguments_as_returned(
734
+ self, properties, required, arguments, declared
735
+ ):
736
"""AFC converts each argument with its annotation before calling the tool."""
661
- from composio_gemini import GeminiProvider
662
- from google import genai
737
from google.genai import _extra_utils
738
665
- provider = GeminiProvider()
666
- tool = create_mock_tool(
667
- "AFC_TOOL",
668
- "test",
669
- input_parameters={
670
- "type": "object",
671
- "properties": properties,
672
- "required": required,
673
- },
674
- )
675
- execute_tool = create_mock_execute_tool()
676
- func = provider.wrap_tool(tool, execute_tool)
739
+ func, execute_tool = _wrap_afc_tool(properties, required)
740
678
- client = genai.Client(api_key="test")
679
- genai_types.FunctionDeclaration.from_callable(
680
- client=client._api_client, callable=func
681
- )
741
_extra_utils.invoke_function_from_dict_args(arguments, func)
742
743
execute_tool.assert_called_once_with("AFC_TOOL", arguments)
744
745
+ @pytest.mark.parametrize(
746
+ ("properties", "required", "arguments", "declared"),
747
+ AFC_CASES,
748
+ )
749
+ def test_afc_declares_the_argument_schema(
750
+ self, properties, required, arguments, declared
751
+ ):
752
+ """Gemini receives the same declaration as before the signature carried
753
+ validators, and a typeless ``enum``/``const`` keeps its values' type."""
754
+ from google import genai
755
+
756
+ func, _ = _wrap_afc_tool(properties, required)
757
+
758
+ declaration = genai_types.FunctionDeclaration.from_callable(
759
+ client=genai.Client(api_key="test")._api_client, callable=func
760
+ )
761
+
762
+ assert declaration.parameters is not None
763
+ parameters = declaration.parameters.model_dump(exclude_none=True, mode="json")
764
+ assert parameters["properties"] == declared
765
+ assert set(required) <= set(parameters["required"])
766
+
767
def test_afc_still_validates_the_source_schema(self):
768
from composio_gemini import GeminiProvider
769
from google.genai import _extra_utils, errors