Skip to content

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

fix(ts-sdk): handle file upload list unions (#3374)

Shrey Singla93b67e8b2f1a
diff --git a/.changeset/file-uploadable-list-unions.md b/.changeset/file-uploadable-list-unions.md
new file mode 100644
index 000000000..9606ac293
--- /dev/null
+++ b/.changeset/file-uploadable-list-unions.md
@@ -0,0 +1,7 @@
+---
+'@composio/core': patch
+---
+
+Fix automatic file upload/download substitution for file schemas that accept either a single file or a list of files.
+
+The file modifier now selects composed-schema branches by runtime value shape, so `anyOf(file, array<file>)` uploads or downloads each file when the tool receives a list while preserving existing single-file behavior.
diff --git a/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts b/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
index f3809eb00..fd66a2576 100644
--- a/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
+++ b/ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
@@ -22,10 +22,62 @@ import {
import {
isPlainObject,
transformProperties,
- schemaHasFileUploadable,
- schemaHasFileDownloadable,
+ schemaHasFileProperty,
} from './FileToolModifier.utils.neutral';
+const getSchemaVariants = (schema: JSONSchemaProperty | undefined): JSONSchemaProperty[] => [
+ ...(schema?.anyOf ?? []),
+ ...(schema?.oneOf ?? []),
+ ...(schema?.allOf ?? []),
+];
+
+const jsonSchemaTypeMatchesValue = (schema: JSONSchemaProperty, value: unknown): boolean => {
+ const schemaType = schema.type;
+
+ if (Array.isArray(schemaType)) {
+ return schemaType.some(type => jsonSchemaTypeMatchesValue({ ...schema, type }, value));
+ }
+
+ if (schemaType === undefined) {
+ if (schema.properties) return isPlainObject(value);
+ if (schema.items) return Array.isArray(value);
+ return false;
+ }
+
+ switch (schemaType) {
+ case 'array':
+ return Array.isArray(value);
+ case 'object':
+ return isPlainObject(value);
+ case 'string':
+ return typeof value === 'string';
+ case 'integer':
+ return Number.isInteger(value);
+ case 'number':
+ return typeof value === 'number';
+ case 'boolean':
+ return typeof value === 'boolean';
+ case 'null':
+ return value === null;
+ default:
+ return false;
+ }
+};
+
+const findSchemaVariantWithFileProperty = (
+ schema: JSONSchemaProperty | undefined,
+ property: 'file_uploadable' | 'file_downloadable',
+ value: unknown
+): JSONSchemaProperty | undefined => {
+ const candidates = getSchemaVariants(schema).filter(variant =>
+ schemaHasFileProperty(variant, property)
+ );
+
+ return (
+ candidates.find(candidate => jsonSchemaTypeMatchesValue(candidate, value)) ?? candidates[0]
+ );
+};
+
/**
* Recursively walks a runtime value and its matching JSON-Schema node,
* uploading any string path whose schema node has `file_uploadable: true`.
@@ -128,27 +180,18 @@ const hydrateFiles = async (
}
// ──────────────────────────────────────────────────────────────────────────
- // 2. Handle anyOf/oneOf/allOf — pick the first variant that contains a
- // file_uploadable property and hydrate against it.
+ // 2. Handle anyOf/oneOf/allOf — pick the file-bearing variant whose
+ // JSON Schema shape matches the runtime value, then hydrate against it.
//
// We deliberately do NOT loop over every uploadable variant. With `oneOf`,
// exactly one variant should apply at runtime, and applying multiple would
// upload the same file once per variant — two presigned-URL round-trips and
- // two S3 PUTs for a two-variant `oneOf`. Matching the Python SDK
- // (`_find_uploadable_schema_variant`), we short-circuit on the first match.
+ // two S3 PUTs for a two-variant `oneOf`. If no runtime shape matches, fall
+ // back to the first file-bearing variant to preserve historical behavior.
// ──────────────────────────────────────────────────────────────────────────
- const schemaVariants = [
- ...(schema?.anyOf ?? []),
- ...(schema?.oneOf ?? []),
- ...(schema?.allOf ?? []),
- ];
-
- if (schemaVariants.length > 0) {
- const firstUploadableVariant = schemaVariants.find(schemaHasFileUploadable);
- if (firstUploadableVariant) {
- return hydrateFiles(value, firstUploadableVariant, ctx);
- }
- // If no uploadable variants found, fall through to check base properties
+ const uploadableVariant = findSchemaVariantWithFileProperty(schema, 'file_uploadable', value);
+ if (uploadableVariant) {
+ return hydrateFiles(value, uploadableVariant, ctx);
}
// ──────────────────────────────────────────────────────────────────────────
@@ -257,28 +300,20 @@ const hydrateDownloads = async (
// ──────────────────────────────────────────────────────────────────────────
// 3. Handle anyOf/oneOf/allOf - try each variant that may contain file_downloadable
// ──────────────────────────────────────────────────────────────────────────
- const schemaVariants = [
- ...(schema?.anyOf ?? []),
- ...(schema?.oneOf ?? []),
- ...(schema?.allOf ?? []),
- ];
+ const schemaVariants = getSchemaVariants(schema);
if (schemaVariants.length > 0) {
- // Find variants that have file_downloadable properties
- const downloadableVariants = schemaVariants.filter(schemaHasFileDownloadable);
-
- // Process with each downloadable variant
- let result = value;
- for (const variant of downloadableVariants) {
- result = await hydrateDownloads(result, variant, ctx);
+ const downloadableVariant = findSchemaVariantWithFileProperty(
+ schema,
+ 'file_downloadable',
+ value
+ );
+ if (downloadableVariant) {
+ return hydrateDownloads(value, downloadableVariant, ctx);
}
// If no downloadable variants found, still traverse the value for s3url objects
- if (downloadableVariants.length === 0) {
- return hydrateDownloads(value, undefined, ctx);
- }
-
- return result;
+ return hydrateDownloads(value, undefined, ctx);
}
// ──────────────────────────────────────────────────────────────────────────
diff --git a/ts/packages/core/test/tools/fileModifiers.test.ts b/ts/packages/core/test/tools/fileModifiers.test.ts
index 3c6d80dc7..63781a502 100644
--- a/ts/packages/core/test/tools/fileModifiers.test.ts
+++ b/ts/packages/core/test/tools/fileModifiers.test.ts
@@ -790,6 +790,178 @@ describe('FileToolModifier', () => {
expect(result.arguments?.fileInput).toEqual(mockFileData);
});
+ it('should upload each file when anyOf accepts a single file or an array of files', async () => {
+ const mockFileData1 = {
+ name: 'file1.txt',
+ mimetype: 'text/plain',
+ s3key: 'uploads/file1.txt',
+ };
+ const mockFileData2 = {
+ name: 'file2.txt',
+ mimetype: 'text/plain',
+ s3key: 'uploads/file2.txt',
+ };
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3)
+ .mockResolvedValueOnce(mockFileData1)
+ .mockResolvedValueOnce(mockFileData2);
+
+ const toolWithSingleOrArrayFile: Tool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A test tool',
+ tags: ['test'],
+ inputParameters: {
+ type: 'object',
+ properties: {
+ attachment: {
+ anyOf: [
+ { type: 'string', file_uploadable: true },
+ {
+ type: 'array',
+ items: { type: 'string', file_uploadable: true },
+ },
+ ],
+ },
+ },
+ },
+ version: '20251201_01',
+ availableVersions: ['20251201_01'],
+ };
+
+ const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ params: {
+ arguments: {
+ attachment: ['/path/to/file1.txt', '/path/to/file2.txt'],
+ },
+ userId: 'test-user',
+ },
+ });
+
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2);
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith(
+ 1,
+ '/path/to/file1.txt',
+ {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ }
+ );
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith(
+ 2,
+ '/path/to/file2.txt',
+ {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ }
+ );
+ expect(result.arguments?.attachment).toEqual([mockFileData1, mockFileData2]);
+ });
+
+ it('should keep single-file behavior when anyOf also accepts an array of files', async () => {
+ const mockFileData = {
+ name: 'file.txt',
+ mimetype: 'text/plain',
+ s3key: 'uploads/file.txt',
+ };
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData);
+
+ const toolWithSingleOrArrayFile: Tool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A test tool',
+ tags: ['test'],
+ inputParameters: {
+ type: 'object',
+ properties: {
+ attachment: {
+ anyOf: [
+ { type: 'string', file_uploadable: true },
+ {
+ type: 'array',
+ items: { type: 'string', file_uploadable: true },
+ },
+ ],
+ },
+ },
+ },
+ version: '20251201_01',
+ availableVersions: ['20251201_01'],
+ };
+
+ const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ params: {
+ arguments: {
+ attachment: '/path/to/file.txt',
+ },
+ userId: 'test-user',
+ },
+ });
+
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(1);
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+ expect(result.arguments?.attachment).toEqual(mockFileData);
+ });
+
+ it('should upload file array items whose item schema is an anyOf file branch', async () => {
+ const mockFileData1 = {
+ name: 'file1.txt',
+ mimetype: 'text/plain',
+ s3key: 'uploads/file1.txt',
+ };
+ const mockFileData2 = {
+ name: 'file2.txt',
+ mimetype: 'text/plain',
+ s3key: 'uploads/file2.txt',
+ };
+ vi.mocked(fileUtils.getFileDataAfterUploadingToS3)
+ .mockResolvedValueOnce(mockFileData1)
+ .mockResolvedValueOnce(mockFileData2);
+
+ const toolWithArrayItemAnyOf: Tool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A test tool',
+ tags: ['test'],
+ inputParameters: {
+ type: 'object',
+ properties: {
+ attachments: {
+ type: 'array',
+ items: {
+ anyOf: [{ type: 'string', file_uploadable: true }, { type: 'null' }],
+ },
+ },
+ },
+ },
+ version: '20251201_01',
+ availableVersions: ['20251201_01'],
+ };
+
+ const result = await fileToolModifier.fileUploadModifier(toolWithArrayItemAnyOf, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ params: {
+ arguments: {
+ attachments: ['/path/to/file1.txt', null, '/path/to/file2.txt'],
+ },
+ userId: 'test-user',
+ },
+ });
+
+ expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2);
+ expect(result.arguments?.attachments).toEqual([mockFileData1, null, mockFileData2]);
+ });
+
it('should not upload when value is null for anyOf with null variant', async () => {
const toolWithAnyOf: Tool = {
slug: 'test-tool',
@@ -1398,6 +1570,101 @@ describe('FileToolModifier', () => {
});
});
+ it('should download each file when anyOf accepts a single file or an array of files', async () => {
+ const mockDownloadResult1 = {
+ name: 'file1.txt',
+ mimeType: 'text/plain',
+ s3Url: 'downloads/file1.txt',
+ filePath: '/downloaded/file1.txt',
+ };
+ const mockDownloadResult2 = {
+ name: 'file2.txt',
+ mimeType: 'text/plain',
+ s3Url: 'downloads/file2.txt',
+ filePath: '/downloaded/file2.txt',
+ };
+ vi.mocked(fileUtils.downloadFileFromS3)
+ .mockResolvedValueOnce(mockDownloadResult1)
+ .mockResolvedValueOnce(mockDownloadResult2);
+
+ const toolWithSingleOrArrayFile: Tool = {
+ slug: 'test-tool',
+ name: 'Test Tool',
+ description: 'A test tool',
+ tags: ['test'],
+ outputParameters: {
+ type: 'object',
+ properties: {
+ attachment: {
+ anyOf: [
+ {
+ type: 'object',
+ file_downloadable: true,
+ properties: {
+ s3url: { type: 'string' },
+ mimetype: { type: 'string' },
+ },
+ },
+ {
+ type: 'array',
+ items: {
+ type: 'object',
+ file_downloadable: true,
+ properties: {
+ s3url: { type: 'string' },
+ mimetype: { type: 'string' },
+ },
+ },
+ },
+ ],
+ },
+ },
+ },
+ version: '20251201_01',
+ availableVersions: ['20251201_01'],
+ };
+
+ const modifiedResult = await fileToolModifier.fileDownloadModifier(
+ toolWithSingleOrArrayFile,
+ {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ result: {
+ data: {
+ attachment: [
+ {
+ s3url: 'https://s3.example.com/file1.txt',
+ mimetype: 'text/plain',
+ },
+ {
+ s3url: 'https://s3.example.com/file2.txt',
+ mimetype: 'text/plain',
+ },
+ ],
+ },
+ error: null,
+ successful: true,
+ },
+ }
+ );
+
+ expect(fileUtils.downloadFileFromS3).toHaveBeenCalledTimes(2);
+ expect(modifiedResult.data.attachment).toEqual([
+ {
+ uri: '/downloaded/file1.txt',
+ file_downloaded: true,
+ s3url: 'https://s3.example.com/file1.txt',
+ mimeType: 'text/plain',
+ },
+ {
+ uri: '/downloaded/file2.txt',
+ file_downloaded: true,
+ s3url: 'https://s3.example.com/file2.txt',
+ mimeType: 'text/plain',
+ },
+ ]);
+ });
+
it('should not download when value is null for anyOf with null variant', async () => {
const toolWithAnyOf: Tool = {
slug: 'test-tool',