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, 20:45 UTC
Fix openai responses schema parsing issues (#1890)
Musthaq Ahamad committed
Sep 4, 2025 at 13:27 UTC
5027e1887652f6f5802993351f52e4e72ec646df
10 files changed
+358
-23
.changeset/wise-loops-deny.md
new
+6
@@ -0,0 +1,6 @@
1
+---
2
+'@composio/core': patch
3
+'@composio/json-schema-to-zod': patch
4
+---
5
+
6
+Fix openai responses schema parsing
pnpm-lock.yaml
+17
-11
@@ -489,8 +489,11 @@ importers:
489
specifier: ^16.4.1
490
version: 16.5.0
491
openai:
492
- specifier: ^4.94.0
493
- version: 4.104.0(ws@8.18.2)(zod@3.25.67)
492
+ specifier: ^5.19.1
493
+ version: 5.19.1(ws@8.18.2)(zod@3.25.67)
494
+ zod-to-json-schema:
495
+ specifier: ^3.24.5
496
+ version: 3.24.5(zod@3.25.67)
497
devDependencies:
498
'@types/bun':
499
specifier: ^1.2.9
@@ -707,6 +710,9 @@ importers:
710
vitest:
711
specifier: ^3.1.4
712
version: 3.2.4(@types/node@24.0.3)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
713
+ zod-to-json-schema:
714
+ specifier: ^3.24.5
715
+ version: 3.24.5(zod@3.25.67)
716
717
ts/packages/providers/anthropic:
718
dependencies:
@@ -766,7 +772,7 @@ importers:
772
dependencies:
773
'@langchain/core':
774
specifier: ^0.3.63
769
- version: 0.3.63(openai@5.16.0(ws@8.18.2)(zod@3.25.67))
775
+ version: 0.3.63(openai@5.19.1(ws@8.18.2)(zod@3.25.67))
776
devDependencies:
777
'@composio/core':
778
specifier: workspace:*
@@ -5809,8 +5815,8 @@ packages:
5815
zod:
5816
optional: true
5817
5812
- openai@5.5.1:
5813
- resolution: {integrity: sha512-5i19097mGotHA1eFsM6Tjd/tJ8uo9sa5Ysv4Q6bKJ2vtN6rc0MzMrUefXnLXYAJcmMQrC1Efhj0AvfIkXrQamw==}
5818
+ openai@5.19.1:
5819
+ resolution: {integrity: sha512-zSqnUF7oR9ksmpusKkpUgkNrj8Sl57U+OyzO8jzc7LUjTMg4DRfR3uCm+EIMA6iw06sRPNp4t7ojp3sCpEUZRQ==}
5820
hasBin: true
5821
peerDependencies:
5822
ws: ^8.18.0
@@ -8652,14 +8658,14 @@ snapshots:
8658
transitivePeerDependencies:
8659
- openai
8660
8655
- '@langchain/core@0.3.63(openai@5.16.0(ws@8.18.2)(zod@3.25.67))':
8661
+ '@langchain/core@0.3.63(openai@5.19.1(ws@8.18.2)(zod@3.25.67))':
8662
dependencies:
8663
'@cfworker/json-schema': 4.1.1
8664
ansi-styles: 5.2.0
8665
camelcase: 6.3.0
8666
decamelize: 1.2.0
8667
js-tiktoken: 1.0.20
8662
- langsmith: 0.3.33(openai@5.16.0(ws@8.18.2)(zod@3.25.67))
8668
+ langsmith: 0.3.33(openai@5.19.1(ws@8.18.2)(zod@3.25.67))
8669
mustache: 4.2.0
8670
p-queue: 6.6.2
8671
p-retry: 4.6.2
@@ -8734,7 +8740,7 @@ snapshots:
8740
dependencies:
8741
'@langchain/core': 0.3.63(openai@4.104.0(ws@8.18.2)(zod@3.25.67))
8742
js-tiktoken: 1.0.20
8737
- openai: 5.5.1(ws@8.18.2)(zod@3.25.67)
8743
+ openai: 5.16.0(ws@8.18.2)(zod@3.25.67)
8744
zod: 3.25.67
8745
transitivePeerDependencies:
8746
- ws
@@ -12726,7 +12732,7 @@ snapshots:
12732
optionalDependencies:
12733
openai: 4.104.0(ws@8.18.2)(zod@3.25.67)
12734
12729
- langsmith@0.3.33(openai@5.16.0(ws@8.18.2)(zod@3.25.67)):
12735
+ langsmith@0.3.33(openai@5.19.1(ws@8.18.2)(zod@3.25.67)):
12736
dependencies:
12737
'@types/uuid': 10.0.0
12738
chalk: 4.1.2
@@ -12736,7 +12742,7 @@ snapshots:
12742
semver: 7.7.2
12743
uuid: 10.0.0
12744
optionalDependencies:
12739
- openai: 5.16.0(ws@8.18.2)(zod@3.25.67)
12745
+ openai: 5.19.1(ws@8.18.2)(zod@3.25.67)
12746
12747
levn@0.4.1:
12748
dependencies:
@@ -13107,7 +13113,7 @@ snapshots:
13113
ws: 8.18.2
13114
zod: 3.25.67
13115
13110
- openai@5.5.1(ws@8.18.2)(zod@3.25.67):
13116
+ openai@5.19.1(ws@8.18.2)(zod@3.25.67):
13117
optionalDependencies:
13118
ws: 8.18.2
13119
zod: 3.25.67
ts/examples/tools/package.json
+2
-2
@@ -20,11 +20,11 @@
20
"dependencies": {
21
"@composio/core": "workspace:*",
22
"dotenv": "^16.4.1",
23
- "openai": "^4.94.0"
23
+ "openai": "^5.19.1",
24
+ "zod-to-json-schema": "^3.24.5"
25
},
26
"devDependencies": {
27
"@types/bun": "^1.2.9",
28
"typescript": "^5.8.3"
29
}
30
}
30
-
ts/examples/tools/src/ai.ts
+3
-4
@@ -51,10 +51,9 @@ console.log('✅ Chat completion created successfully...');
51
* If the assistant has tool calls, execute them and log the result
52
*/
53
console.log('🔄 Calling tool...');
54
-if (response.choices[0].message.tool_calls) {
54
+if (response.choices[0].message.tool_calls && response.choices[0].message.tool_calls[0].type === 'function') {
55
console.log(`✅ Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);
56
const result = await composio.provider.handleToolCalls('default', response);
57
- const data = JSON.parse(result[0]);
58
- console.log(data);
57
+ console.log(result);
58
}
60
-console.log('✅ Tool called successfully...');
\ No newline at end of file
59
+console.log('✅ Tool called successfully...');
ts/examples/tools/src/tool-conversion.ts
new
+15
@@ -0,0 +1,15 @@
1
+import { Composio, jsonSchemaToZodSchema } from '@composio/core';
2
+import zodToJsonSchema from 'zod-to-json-schema';
3
+
4
+const composio = new Composio({
5
+ apiKey: process.env.COMPOSIO_API_KEY,
6
+});
7
+
8
+const tool = await composio.tools.getRawComposioToolBySlug('GMAIL_CREATE_EMAIL_DRAFT');
9
+const inputParams = tool.inputParameters;
10
+console.log(`---------- Original Input Parameters ----------`);
11
+console.log(JSON.stringify(inputParams, null, 2));
12
+
13
+const zodSchema = jsonSchemaToZodSchema(inputParams ?? {});
14
+console.log(`---------- Zod Schema Parsed ----------`);
15
+console.log(JSON.stringify(zodToJsonSchema(zodSchema, { target: "jsonSchema7"}), null, 2));
\ No newline at end of file
ts/packages/json-schema-to-zod/package.json
+2
-1
@@ -33,7 +33,8 @@
33
"@types/node": "^24.0.1",
34
"tsup": "^8.4.0",
35
"typescript": "^5.8.3",
36
- "vitest": "^3.1.4"
36
+ "vitest": "^3.1.4",
37
+ "zod-to-json-schema": "^3.24.5"
38
},
39
"dependencies": {
40
"@types/json-schema": "^7.0.15",
ts/packages/json-schema-to-zod/src/parsers/parse-nullable.ts
+11
-1
@@ -6,5 +6,15 @@ import { omit } from '../utils/omit';
6
* For compatibility with open api 3.0 nullable
7
*/
8
export const parseNullable = (jsonSchema: JsonSchemaObject & { nullable: true }, refs: Refs) => {
9
- return parseSchema(omit(jsonSchema, 'nullable'), refs, true).nullable();
9
+ // If the schema has a null default, we need to handle it specially
10
+ // Remove the default from the base schema to prevent it from being applied to non-null types
11
+ const hasNullDefault = jsonSchema.default === null;
12
+ const baseSchema = hasNullDefault
13
+ ? omit(omit(jsonSchema, 'nullable'), 'default')
14
+ : omit(jsonSchema, 'nullable');
15
+
16
+ const zodSchema = parseSchema(baseSchema, refs, true).nullable();
17
+
18
+ // Apply the null default at the nullable level if it exists
19
+ return hasNullDefault ? zodSchema.default(null) : zodSchema;
20
};
ts/packages/json-schema-to-zod/src/parsers/parse-object.ts
+27
-2
@@ -4,7 +4,7 @@ import { parseAllOf } from './parse-all-of';
4
import { parseAnyOf } from './parse-any-of';
5
import { parseOneOf } from './parse-one-of';
6
import { parseSchema } from './parse-schema';
7
-import type { JsonSchemaObject, Refs } from '../types';
7
+import type { JsonSchemaObject, Refs, JsonSchema } from '../types';
8
import { its } from '../utils/its';
9
10
function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object' }, refs: Refs) {
@@ -39,8 +39,33 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object'
39
'default' in propJsonSchema
40
) {
41
// If default is null, make the field nullable with the null default
42
+ // But don't make it nullable if it's already an anyOf/oneOf that includes null
43
if (propJsonSchema.default === null) {
43
- properties[key] = propZodSchema.nullable().optional().default(null);
44
+ const hasAnyOfWithNull =
45
+ propJsonSchema.anyOf &&
46
+ Array.isArray(propJsonSchema.anyOf) &&
47
+ propJsonSchema.anyOf.some(
48
+ (schema: JsonSchema) =>
49
+ typeof schema === 'object' && schema !== null && schema.type === 'null'
50
+ );
51
+ const hasOneOfWithNull =
52
+ propJsonSchema.oneOf &&
53
+ Array.isArray(propJsonSchema.oneOf) &&
54
+ propJsonSchema.oneOf.some(
55
+ (schema: JsonSchema) =>
56
+ typeof schema === 'object' && schema !== null && schema.type === 'null'
57
+ );
58
+ const isNullable =
59
+ 'nullable' in propJsonSchema &&
60
+ (propJsonSchema as JsonSchemaObject & { nullable?: boolean }).nullable === true;
61
+
62
+ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) {
63
+ // The schema already handles null through anyOf/oneOf/nullable, just make it optional with default
64
+ properties[key] = propZodSchema.optional().default(null);
65
+ } else {
66
+ // Make the field nullable with the null default
67
+ properties[key] = propZodSchema.nullable().optional().default(null);
68
+ }
69
} else {
70
properties[key] = propZodSchema.optional().default(propJsonSchema.default);
71
}
ts/packages/json-schema-to-zod/src/parsers/parse-schema.ts
+17
-2
@@ -53,8 +53,23 @@ const addDescribes = (jsonSchema: JsonSchemaObject, zodSchema: z.ZodTypeAny): z.
53
return zodSchema;
54
};
55
56
-const addDefaults = (jsonSchema: JsonSchemaObject, zodSchema: z.ZodTypeAny): z.ZodTypeAny => {
56
+const addDefaults = (
57
+ jsonSchema: JsonSchemaObject,
58
+ zodSchema: z.ZodTypeAny,
59
+ refs?: Refs
60
+): z.ZodTypeAny => {
61
if (jsonSchema.default !== undefined) {
62
+ // Don't apply null defaults to non-nullable types within anyOf/oneOf contexts
63
+ if (jsonSchema.default === null) {
64
+ // Check if we're in an anyOf/oneOf context by examining the path
65
+ const inAnyOfOrOneOf = refs?.path.some(segment => segment === 'anyOf' || segment === 'oneOf');
66
+
67
+ // If we're in anyOf/oneOf and this is a non-nullable type, skip the default
68
+ if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== 'null' && !jsonSchema.nullable) {
69
+ return zodSchema;
70
+ }
71
+ }
72
+
73
zodSchema = zodSchema.default(jsonSchema.default);
74
}
75
@@ -144,7 +159,7 @@ export const parseSchema = (
159
}
160
161
if (!refs.withoutDefaults) {
147
- parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
162
+ parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
163
}
164
165
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
ts/packages/json-schema-to-zod/test/comprehensive-edge-case-tests.ts
new
+258
@@ -0,0 +1,258 @@
1
+import { jsonSchemaToZod } from '../src/json-schema-to-zod';
2
+import zodToJsonSchema from 'zod-to-json-schema';
3
+import type { JsonSchema } from '../src/types';
4
+
5
+console.log('=== Testing Complex Schema Edge Cases ===\n');
6
+
7
+// Test 1: Original anyOf schema (not from nullable conversion)
8
+console.log('Test 1: Original anyOf with different types and defaults');
9
+const originalAnyOfSchema: JsonSchema = {
10
+ type: 'object',
11
+ properties: {
12
+ flexible_field: {
13
+ anyOf: [
14
+ {
15
+ type: 'string',
16
+ minLength: 5,
17
+ default: 'hello', // This should be preserved
18
+ },
19
+ {
20
+ type: 'number',
21
+ minimum: 0,
22
+ default: 42, // This should be preserved
23
+ },
24
+ {
25
+ type: 'boolean',
26
+ default: true, // This should be preserved
27
+ },
28
+ ],
29
+ description: 'A field that can be string, number, or boolean',
30
+ default: 'hello',
31
+ },
32
+ status: {
33
+ anyOf: [
34
+ {
35
+ type: 'string',
36
+ enum: ['active', 'inactive'],
37
+ default: 'active',
38
+ },
39
+ {
40
+ type: 'number',
41
+ enum: [1, 0],
42
+ default: 1,
43
+ },
44
+ ],
45
+ default: 'active',
46
+ },
47
+ },
48
+ required: [],
49
+};
50
+
51
+console.log('Original schema:');
52
+console.log(JSON.stringify(originalAnyOfSchema, null, 2));
53
+
54
+const zodSchema1 = jsonSchemaToZod(originalAnyOfSchema);
55
+const backToJson1 = zodToJsonSchema(zodSchema1, { target: 'jsonSchema7' });
56
+
57
+console.log('\nConverted back to JSON:');
58
+console.log(JSON.stringify(backToJson1, null, 2));
59
+
60
+console.log('\nTesting parsing:');
61
+try {
62
+ console.log('Empty object:', zodSchema1.parse({}));
63
+ console.log('With string flexible_field:', zodSchema1.parse({ flexible_field: 'world' }));
64
+ console.log('With number flexible_field:', zodSchema1.parse({ flexible_field: 123 }));
65
+ console.log('With boolean flexible_field:', zodSchema1.parse({ flexible_field: false }));
66
+} catch (error) {
67
+ console.error('Parse error:', error);
68
+}
69
+
70
+console.log('\n' + '='.repeat(80) + '\n');
71
+
72
+// Test 2: Array with different types (union types)
73
+console.log('Test 2: Array with union of different types');
74
+const arrayUnionSchema: JsonSchema = {
75
+ type: 'object',
76
+ properties: {
77
+ mixed_items: {
78
+ type: 'array',
79
+ items: {
80
+ anyOf: [
81
+ {
82
+ type: 'string',
83
+ pattern: '^[a-zA-Z]+$',
84
+ default: 'text',
85
+ },
86
+ {
87
+ type: 'number',
88
+ multipleOf: 2,
89
+ default: 10,
90
+ },
91
+ {
92
+ type: 'object',
93
+ properties: {
94
+ id: { type: 'number' },
95
+ name: { type: 'string' },
96
+ },
97
+ required: ['id'],
98
+ default: { id: 1, name: 'default' },
99
+ },
100
+ ],
101
+ },
102
+ default: ['text', 10, { id: 1, name: 'default' }],
103
+ description: 'Array that can contain strings, numbers, or objects',
104
+ },
105
+ tags: {
106
+ type: 'array',
107
+ items: {
108
+ oneOf: [
109
+ {
110
+ type: 'string',
111
+ enum: ['important', 'urgent', 'normal'],
112
+ default: 'normal',
113
+ },
114
+ {
115
+ type: 'number',
116
+ minimum: 1,
117
+ maximum: 5,
118
+ default: 3,
119
+ },
120
+ ],
121
+ },
122
+ default: ['normal', 3],
123
+ description: 'Tags can be priority strings or numeric ratings',
124
+ },
125
+ },
126
+ required: [],
127
+};
128
+
129
+console.log('Original schema:');
130
+console.log(JSON.stringify(arrayUnionSchema, null, 2));
131
+
132
+const zodSchema2 = jsonSchemaToZod(arrayUnionSchema);
133
+const backToJson2 = zodToJsonSchema(zodSchema2, { target: 'jsonSchema7' });
134
+
135
+console.log('\nConverted back to JSON:');
136
+console.log(JSON.stringify(backToJson2, null, 2));
137
+
138
+console.log('\nTesting parsing:');
139
+try {
140
+ console.log('Empty object:', zodSchema2.parse({}));
141
+ console.log(
142
+ 'With mixed array:',
143
+ zodSchema2.parse({
144
+ mixed_items: ['hello', 42, { id: 5, name: 'test' }],
145
+ tags: ['urgent', 4, 'normal'],
146
+ })
147
+ );
148
+} catch (error) {
149
+ console.error('Parse error:', error);
150
+}
151
+
152
+console.log('\n' + '='.repeat(80) + '\n');
153
+
154
+// Test 3: Complex nested schema with multiple anyOf and nullable fields
155
+console.log('Test 3: Complex nested schema with anyOf and nullable');
156
+const complexNestedSchema: JsonSchema = {
157
+ type: 'object',
158
+ properties: {
159
+ user: {
160
+ anyOf: [
161
+ {
162
+ type: 'object',
163
+ properties: {
164
+ id: { type: 'string', default: 'user-123' },
165
+ name: { type: 'string' },
166
+ email: {
167
+ type: 'string',
168
+ format: 'email',
169
+ nullable: true,
170
+ default: null,
171
+ } as any,
172
+ },
173
+ required: ['name'],
174
+ default: { id: 'user-123', name: 'Anonymous', email: null },
175
+ },
176
+ {
177
+ type: 'string',
178
+ pattern: '^user-\\d+$',
179
+ default: 'user-000',
180
+ },
181
+ ],
182
+ description: 'User can be an object or a user ID string',
183
+ default: { id: 'user-123', name: 'Anonymous', email: null },
184
+ },
185
+ metadata: {
186
+ type: 'object',
187
+ properties: {
188
+ settings: {
189
+ anyOf: [
190
+ {
191
+ type: 'object',
192
+ properties: {
193
+ theme: {
194
+ type: 'string',
195
+ enum: ['light', 'dark'],
196
+ default: 'light',
197
+ },
198
+ notifications: {
199
+ type: 'boolean',
200
+ default: true,
201
+ },
202
+ },
203
+ default: { theme: 'light', notifications: true },
204
+ },
205
+ {
206
+ type: 'string',
207
+ enum: ['minimal', 'full'],
208
+ default: 'full',
209
+ },
210
+ ],
211
+ default: { theme: 'light', notifications: true },
212
+ },
213
+ version: {
214
+ type: 'string',
215
+ nullable: true,
216
+ default: null,
217
+ } as any,
218
+ },
219
+ default: {
220
+ settings: { theme: 'light', notifications: true },
221
+ version: null,
222
+ },
223
+ },
224
+ },
225
+ required: [],
226
+};
227
+
228
+console.log('Original schema:');
229
+console.log(JSON.stringify(complexNestedSchema, null, 2));
230
+
231
+const zodSchema3 = jsonSchemaToZod(complexNestedSchema);
232
+const backToJson3 = zodToJsonSchema(zodSchema3, { target: 'jsonSchema7' });
233
+
234
+console.log('\nConverted back to JSON:');
235
+console.log(JSON.stringify(backToJson3, null, 2));
236
+
237
+console.log('\nTesting parsing:');
238
+try {
239
+ console.log('Empty object:', zodSchema3.parse({}));
240
+ console.log(
241
+ 'With user object:',
242
+ zodSchema3.parse({
243
+ user: { name: 'John Doe', email: 'john@example.com' },
244
+ metadata: {
245
+ settings: 'minimal',
246
+ version: '1.0.0',
247
+ },
248
+ })
249
+ );
250
+ console.log(
251
+ 'With user string:',
252
+ zodSchema3.parse({
253
+ user: 'user-456',
254
+ })
255
+ );
256
+} catch (error) {
257
+ console.error('Parse error:', error);
258
+}