@devries-cameron20 / composio Mirror · read-only / commits / eaacfec55
Fork 0

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 22, 17:00 UTC

fix(py): KeyError: 'type' when SUPABASE_BETA_RUN_SQL_QUERY is used with Agents (#2264)

Co-authored-by: jkomyno <12381818+jkomyno@users.noreply.github.com>

Alberto Schiabel committed Dec 13, 2025 at 08:28 UTC eaacfec5527fdb90391a79f085e10e9976e08245
2 files changed +326 -2
python/composio/core/models/_files.py
+8 -2
@@ -232,7 +232,10 @@ class FileHelper(WithLogger):
232 ).model_dump()
233 continue
234
235 - if isinstance(request[_param], dict) and params[_param]["type"] == "object":
235 + if (
236 + isinstance(request[_param], dict)
237 + and params[_param].get("type") == "object"
238 + ):
239 request[_param] = self._substitute_file_uploads_recursively(
240 schema=params[_param],
241 request=request[_param],
@@ -305,7 +308,10 @@ class FileHelper(WithLogger):
308 continue
309 params[_param] = obj
310
308 - if isinstance(request[_param], dict) and params[_param]["type"] == "object":
311 + if (
312 + isinstance(request[_param], dict)
313 + and params[_param].get("type") == "object"
314 + ):
315 request[_param] = self._substitute_file_downloads_recursively(
316 schema=params[_param],
317 request=request[_param],
python/tests/test_files.py new
+318
@@ -0,0 +1,318 @@
1 +"""Tests for file helper functionality in composio/core/models/_files.py.
2 +
3 +These tests ensure that the FileHelper class correctly handles JSON schemas
4 +that use anyOf, oneOf, allOf, or $ref instead of direct 'type' properties.
5 +"""
6 +
7 +from unittest.mock import Mock
8 +
9 +import pytest
10 +
11 +from composio.client.types import Tool, tool_list_response
12 +from composio.core.models._files import FileHelper
13 +from composio.core.models.base import allow_tracking
14 +
15 +
16 +@pytest.fixture(autouse=True)
17 +def disable_telemetry():
18 + """Disable telemetry for all tests to prevent thread issues."""
19 + token = allow_tracking.set(False)
20 + yield
21 + allow_tracking.reset(token)
22 +
23 +
24 +@pytest.fixture
25 +def mock_client():
26 + """Create a mock HTTP client."""
27 + return Mock()
28 +
29 +
30 +@pytest.fixture
31 +def file_helper(mock_client):
32 + """Create a FileHelper instance with a mock client."""
33 + return FileHelper(client=mock_client)
34 +
35 +
36 +@pytest.fixture
37 +def mock_tool():
38 + """Create a mock tool for testing."""
39 + return Tool(
40 + name="Test Tool",
41 + slug="TEST_TOOL",
42 + description="Test tool",
43 + input_parameters={
44 + "properties": {
45 + "query": {"type": "string"},
46 + },
47 + "type": "object",
48 + },
49 + output_parameters={
50 + "properties": {
51 + "data": {"type": "object", "properties": {}},
52 + },
53 + "type": "object",
54 + },
55 + available_versions=["v1.0.0"],
56 + version="v1.0.0",
57 + scopes=[],
58 + toolkit=tool_list_response.ItemToolkit(
59 + name="Test Toolkit", slug="test_toolkit", logo=""
60 + ),
61 + deprecated=tool_list_response.ItemDeprecated(
62 + available_versions=["v1.0.0"],
63 + displayName="Test Tool",
64 + version="v1.0.0",
65 + toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
66 + is_deprecated=False,
67 + ),
68 + is_deprecated=False,
69 + no_auth=False,
70 + tags=[],
71 + )
72 +
73 +
74 +class TestFileHelperSchemaHandling:
75 + """Test cases for handling schemas without direct 'type' property.
76 +
77 + Regression tests for PLEN-766: KeyError: 'type' when schemas use
78 + anyOf, oneOf, allOf, or $ref instead of direct type properties.
79 + """
80 +
81 + def test_substitute_file_uploads_with_oneof_schema(self, file_helper, mock_tool):
82 + """Test that oneOf schemas don't cause KeyError in upload path."""
83 + schema_with_oneof = {
84 + "properties": {
85 + "input": {
86 + "oneOf": [
87 + {"type": "string"},
88 + {"type": "object", "properties": {"file": {"type": "string"}}},
89 + ]
90 + }
91 + }
92 + }
93 + request = {"input": {"file": "test.txt"}}
94 +
95 + # Should not raise KeyError
96 + result = file_helper._substitute_file_uploads_recursively(
97 + tool=mock_tool,
98 + schema=schema_with_oneof,
99 + request=request.copy(),
100 + )
101 + assert result == {"input": {"file": "test.txt"}}
102 +
103 + def test_substitute_file_uploads_with_anyof_schema(self, file_helper, mock_tool):
104 + """Test that anyOf schemas don't cause KeyError in upload path."""
105 + schema_with_anyof = {
106 + "properties": {
107 + "data": {
108 + "anyOf": [
109 + {"type": "string"},
110 + {"type": "object", "properties": {"value": {"type": "string"}}},
111 + ]
112 + }
113 + }
114 + }
115 + request = {"data": {"value": "test"}}
116 +
117 + # Should not raise KeyError
118 + result = file_helper._substitute_file_uploads_recursively(
119 + tool=mock_tool,
120 + schema=schema_with_anyof,
121 + request=request.copy(),
122 + )
123 + assert result == {"data": {"value": "test"}}
124 +
125 + def test_substitute_file_uploads_with_allof_schema(self, file_helper, mock_tool):
126 + """Test that allOf schemas don't cause KeyError in upload path."""
127 + schema_with_allof = {
128 + "properties": {
129 + "config": {
130 + "allOf": [
131 + {"properties": {"name": {"type": "string"}}},
132 + {"properties": {"value": {"type": "string"}}},
133 + ]
134 + }
135 + }
136 + }
137 + request = {"config": {"name": "test", "value": "123"}}
138 +
139 + # Should not raise KeyError
140 + result = file_helper._substitute_file_uploads_recursively(
141 + tool=mock_tool,
142 + schema=schema_with_allof,
143 + request=request.copy(),
144 + )
145 + assert result == {"config": {"name": "test", "value": "123"}}
146 +
147 + def test_substitute_file_uploads_with_ref_schema(self, file_helper, mock_tool):
148 + """Test that $ref schemas don't cause KeyError in upload path."""
149 + schema_with_ref = {
150 + "properties": {"reference": {"$ref": "#/definitions/SomeType"}}
151 + }
152 + request = {"reference": {"nested": "value"}}
153 +
154 + # Should not raise KeyError
155 + result = file_helper._substitute_file_uploads_recursively(
156 + tool=mock_tool,
157 + schema=schema_with_ref,
158 + request=request.copy(),
159 + )
160 + assert result == {"reference": {"nested": "value"}}
161 +
162 + def test_substitute_file_downloads_with_oneof_schema(self, file_helper, mock_tool):
163 + """Test that oneOf schemas don't cause KeyError in download path."""
164 + schema_with_oneof = {
165 + "properties": {
166 + "result": {
167 + "oneOf": [
168 + {"type": "string"},
169 + {"type": "object", "properties": {"data": {"type": "string"}}},
170 + ]
171 + }
172 + }
173 + }
174 + response = {"result": {"data": "some value"}}
175 +
176 + # Should not raise KeyError
177 + result = file_helper._substitute_file_downloads_recursively(
178 + tool=mock_tool,
179 + schema=schema_with_oneof,
180 + request=response.copy(),
181 + )
182 + assert result == {"result": {"data": "some value"}}
183 +
184 + def test_substitute_file_downloads_with_anyof_schema(self, file_helper, mock_tool):
185 + """Test that anyOf schemas don't cause KeyError in download path."""
186 + schema_with_anyof = {
187 + "properties": {
188 + "output": {
189 + "anyOf": [
190 + {"type": "string"},
191 + {
192 + "type": "object",
193 + "properties": {"nested": {"type": "string"}},
194 + },
195 + ]
196 + }
197 + }
198 + }
199 + response = {"output": {"nested": "value"}}
200 +
201 + # Should not raise KeyError
202 + result = file_helper._substitute_file_downloads_recursively(
203 + tool=mock_tool,
204 + schema=schema_with_anyof,
205 + request=response.copy(),
206 + )
207 + assert result == {"output": {"nested": "value"}}
208 +
209 + def test_substitute_file_downloads_with_allof_schema(self, file_helper, mock_tool):
210 + """Test that allOf schemas don't cause KeyError in download path."""
211 + schema_with_allof = {
212 + "properties": {
213 + "response": {
214 + "allOf": [
215 + {"properties": {"status": {"type": "string"}}},
216 + {"properties": {"message": {"type": "string"}}},
217 + ]
218 + }
219 + }
220 + }
221 + response = {"response": {"status": "ok", "message": "success"}}
222 +
223 + # Should not raise KeyError
224 + result = file_helper._substitute_file_downloads_recursively(
225 + tool=mock_tool,
226 + schema=schema_with_allof,
227 + request=response.copy(),
228 + )
229 + assert result == {"response": {"status": "ok", "message": "success"}}
230 +
231 + def test_substitute_file_downloads_with_ref_schema(self, file_helper, mock_tool):
232 + """Test that $ref schemas don't cause KeyError in download path."""
233 + schema_with_ref = {
234 + "properties": {"data": {"$ref": "#/definitions/ResponseType"}}
235 + }
236 + response = {"data": {"field": "value"}}
237 +
238 + # Should not raise KeyError
239 + result = file_helper._substitute_file_downloads_recursively(
240 + tool=mock_tool,
241 + schema=schema_with_ref,
242 + request=response.copy(),
243 + )
244 + assert result == {"data": {"field": "value"}}
245 +
246 + def test_substitute_file_uploads_with_normal_type_still_works(
247 + self, file_helper, mock_tool
248 + ):
249 + """Test that normal schemas with 'type' property still work correctly."""
250 + schema_with_type = {
251 + "properties": {
252 + "nested": {
253 + "type": "object",
254 + "properties": {
255 + "value": {"type": "string"},
256 + },
257 + }
258 + }
259 + }
260 + request = {"nested": {"value": "test"}}
261 +
262 + # Should process normally and recurse into nested object
263 + result = file_helper._substitute_file_uploads_recursively(
264 + tool=mock_tool,
265 + schema=schema_with_type,
266 + request=request.copy(),
267 + )
268 + assert result == {"nested": {"value": "test"}}
269 +
270 + def test_substitute_file_downloads_with_normal_type_still_works(
271 + self, file_helper, mock_tool
272 + ):
273 + """Test that normal schemas with 'type' property still work correctly."""
274 + schema_with_type = {
275 + "properties": {
276 + "data": {
277 + "type": "object",
278 + "properties": {
279 + "result": {"type": "string"},
280 + },
281 + }
282 + }
283 + }
284 + response = {"data": {"result": "success"}}
285 +
286 + # Should process normally and recurse into nested object
287 + result = file_helper._substitute_file_downloads_recursively(
288 + tool=mock_tool,
289 + schema=schema_with_type,
290 + request=response.copy(),
291 + )
292 + assert result == {"data": {"result": "success"}}
293 +
294 + def test_substitute_with_empty_properties(self, file_helper, mock_tool):
295 + """Test handling of schemas with empty properties."""
296 + schema_empty = {"properties": {}}
297 + request = {"unknown": {"nested": "value"}}
298 +
299 + # Should not raise any errors
300 + result = file_helper._substitute_file_uploads_recursively(
301 + tool=mock_tool,
302 + schema=schema_empty,
303 + request=request.copy(),
304 + )
305 + assert result == {"unknown": {"nested": "value"}}
306 +
307 + def test_substitute_with_no_properties_key(self, file_helper, mock_tool):
308 + """Test handling of schemas without properties key."""
309 + schema_no_props = {"type": "object"}
310 + request = {"data": {"value": "test"}}
311 +
312 + # Should return request unchanged
313 + result = file_helper._substitute_file_uploads_recursively(
314 + tool=mock_tool,
315 + schema=schema_no_props,
316 + request=request.copy(),
317 + )
318 + assert result == {"data": {"value": "test"}}