Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
Fix openai responses schema parsing issues (#1890)
Musthaq Ahamad5027e1887652
diff --git a/.changeset/wise-loops-deny.md b/.changeset/wise-loops-deny.mdnew file mode 100644index 000000000..77bd5f664--- /dev/null+++ b/.changeset/wise-loops-deny.md@@ -0,0 +1,6 @@+---+'@composio/core': patch+'@composio/json-schema-to-zod': patch+---++Fix openai responses schema parsingdiff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 617a0ca9f..1be7c63b6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -489,8 +489,11 @@ importers:specifier: ^16.4.1version: 16.5.0openai:- specifier: ^4.94.0- version: 4.104.0([email protected])([email protected])+ specifier: ^5.19.1+ version: 5.19.1([email protected])([email protected])+ zod-to-json-schema:+ specifier: ^3.24.5+ version: 3.24.5([email protected])devDependencies:'@types/bun':specifier: ^1.2.9@@ -707,6 +710,9 @@ importers:vitest:specifier: ^3.1.4version: 3.2.4(@types/[email protected])(@vitest/[email protected])([email protected])([email protected])([email protected])+ zod-to-json-schema:+ specifier: ^3.24.5+ version: 3.24.5([email protected])ts/packages/providers/anthropic:dependencies:@@ -766,7 +772,7 @@ importers:dependencies:'@langchain/core':specifier: ^0.3.63devDependencies:'@composio/core':specifier: workspace:*@@ -5809,8 +5815,8 @@ packages:zod:optional: true- resolution: {integrity: sha512-5i19097mGotHA1eFsM6Tjd/tJ8uo9sa5Ysv4Q6bKJ2vtN6rc0MzMrUefXnLXYAJcmMQrC1Efhj0AvfIkXrQamw==}+ resolution: {integrity: sha512-zSqnUF7oR9ksmpusKkpUgkNrj8Sl57U+OyzO8jzc7LUjTMg4DRfR3uCm+EIMA6iw06sRPNp4t7ojp3sCpEUZRQ==}hasBin: truepeerDependencies:ws: ^8.18.0@@ -8652,14 +8658,14 @@ snapshots:transitivePeerDependencies:- openaidependencies:'@cfworker/json-schema': 4.1.1ansi-styles: 5.2.0camelcase: 6.3.0decamelize: 1.2.0js-tiktoken: 1.0.20mustache: 4.2.0p-queue: 6.6.2p-retry: 4.6.2@@ -8734,7 +8740,7 @@ snapshots:dependencies:js-tiktoken: 1.0.20- openai: 5.5.1([email protected])([email protected])+ openai: 5.16.0([email protected])([email protected])zod: 3.25.67transitivePeerDependencies:- ws@@ -12726,7 +12732,7 @@ snapshots:optionalDependencies:openai: 4.104.0([email protected])([email protected])dependencies:'@types/uuid': 10.0.0chalk: 4.1.2@@ -12736,7 +12742,7 @@ snapshots:semver: 7.7.2uuid: 10.0.0optionalDependencies:- openai: 5.16.0([email protected])([email protected])+ openai: 5.19.1([email protected])([email protected])dependencies:@@ -13107,7 +13113,7 @@ snapshots:ws: 8.18.2zod: 3.25.67optionalDependencies:ws: 8.18.2zod: 3.25.67diff --git a/ts/examples/tools/package.json b/ts/examples/tools/package.jsonindex d46be6316..fbf8244ed 100644--- a/ts/examples/tools/package.json+++ b/ts/examples/tools/package.json@@ -20,11 +20,11 @@"dependencies": {"@composio/core": "workspace:*","dotenv": "^16.4.1",- "openai": "^4.94.0"+ "openai": "^5.19.1",+ "zod-to-json-schema": "^3.24.5"},"devDependencies": {"@types/bun": "^1.2.9","typescript": "^5.8.3"}}-diff --git a/ts/examples/tools/src/ai.ts b/ts/examples/tools/src/ai.tsindex 373e857ec..726683e51 100644--- a/ts/examples/tools/src/ai.ts+++ b/ts/examples/tools/src/ai.ts@@ -51,10 +51,9 @@ console.log('✅ Chat completion created successfully...');* If the assistant has tool calls, execute them and log the result*/console.log('🔄 Calling tool...');-if (response.choices[0].message.tool_calls) {+if (response.choices[0].message.tool_calls && response.choices[0].message.tool_calls[0].type === 'function') {console.log(`✅ Calling tool ${response.choices[0].message.tool_calls[0].function.name}`);const result = await composio.provider.handleToolCalls('default', response);- const data = JSON.parse(result[0]);- console.log(data);+ console.log(result);}-console.log('✅ Tool called successfully...');\ No newline at end of file+console.log('✅ Tool called successfully...');diff --git a/ts/examples/tools/src/tool-conversion.ts b/ts/examples/tools/src/tool-conversion.tsnew file mode 100644index 000000000..6786536bd--- /dev/null+++ b/ts/examples/tools/src/tool-conversion.ts@@ -0,0 +1,15 @@+import { Composio, jsonSchemaToZodSchema } from '@composio/core';+import zodToJsonSchema from 'zod-to-json-schema';++const composio = new Composio({+ apiKey: process.env.COMPOSIO_API_KEY,+});++const tool = await composio.tools.getRawComposioToolBySlug('GMAIL_CREATE_EMAIL_DRAFT');+const inputParams = tool.inputParameters;+console.log(`---------- Original Input Parameters ----------`);+console.log(JSON.stringify(inputParams, null, 2));++const zodSchema = jsonSchemaToZodSchema(inputParams ?? {});+console.log(`---------- Zod Schema Parsed ----------`);+console.log(JSON.stringify(zodToJsonSchema(zodSchema, { target: "jsonSchema7"}), null, 2));\ No newline at end of filediff --git a/ts/packages/json-schema-to-zod/package.json b/ts/packages/json-schema-to-zod/package.jsonindex bf4f311e3..a11346e62 100644--- a/ts/packages/json-schema-to-zod/package.json+++ b/ts/packages/json-schema-to-zod/package.json@@ -33,7 +33,8 @@"@types/node": "^24.0.1","tsup": "^8.4.0","typescript": "^5.8.3",- "vitest": "^3.1.4"+ "vitest": "^3.1.4",+ "zod-to-json-schema": "^3.24.5"},"dependencies": {"@types/json-schema": "^7.0.15",diff --git a/ts/packages/json-schema-to-zod/src/parsers/parse-nullable.ts b/ts/packages/json-schema-to-zod/src/parsers/parse-nullable.tsindex cfc575e9c..13b65f187 100644--- a/ts/packages/json-schema-to-zod/src/parsers/parse-nullable.ts+++ b/ts/packages/json-schema-to-zod/src/parsers/parse-nullable.ts@@ -6,5 +6,15 @@ import { omit } from '../utils/omit';* For compatibility with open api 3.0 nullable*/export const parseNullable = (jsonSchema: JsonSchemaObject & { nullable: true }, refs: Refs) => {- return parseSchema(omit(jsonSchema, 'nullable'), refs, true).nullable();+ // If the schema has a null default, we need to handle it specially+ // Remove the default from the base schema to prevent it from being applied to non-null types+ const hasNullDefault = jsonSchema.default === null;+ const baseSchema = hasNullDefault+ ? omit(omit(jsonSchema, 'nullable'), 'default')+ : omit(jsonSchema, 'nullable');++ const zodSchema = parseSchema(baseSchema, refs, true).nullable();++ // Apply the null default at the nullable level if it exists+ return hasNullDefault ? zodSchema.default(null) : zodSchema;};diff --git a/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts b/ts/packages/json-schema-to-zod/src/parsers/parse-object.tsindex 8fabf819c..a7d8a53c8 100644--- a/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts+++ b/ts/packages/json-schema-to-zod/src/parsers/parse-object.ts@@ -4,7 +4,7 @@ import { parseAllOf } from './parse-all-of';import { parseAnyOf } from './parse-any-of';import { parseOneOf } from './parse-one-of';import { parseSchema } from './parse-schema';-import type { JsonSchemaObject, Refs } from '../types';+import type { JsonSchemaObject, Refs, JsonSchema } from '../types';import { its } from '../utils/its';function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object' }, refs: Refs) {@@ -39,8 +39,33 @@ function parseObjectProperties(objectSchema: JsonSchemaObject & { type: 'object''default' in propJsonSchema) {// If default is null, make the field nullable with the null default+ // But don't make it nullable if it's already an anyOf/oneOf that includes nullif (propJsonSchema.default === null) {- properties[key] = propZodSchema.nullable().optional().default(null);+ const hasAnyOfWithNull =+ propJsonSchema.anyOf &&+ Array.isArray(propJsonSchema.anyOf) &&+ propJsonSchema.anyOf.some(+ (schema: JsonSchema) =>+ typeof schema === 'object' && schema !== null && schema.type === 'null'+ );+ const hasOneOfWithNull =+ propJsonSchema.oneOf &&+ Array.isArray(propJsonSchema.oneOf) &&+ propJsonSchema.oneOf.some(+ (schema: JsonSchema) =>+ typeof schema === 'object' && schema !== null && schema.type === 'null'+ );+ const isNullable =+ 'nullable' in propJsonSchema &&+ (propJsonSchema as JsonSchemaObject & { nullable?: boolean }).nullable === true;++ if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) {+ // The schema already handles null through anyOf/oneOf/nullable, just make it optional with default+ properties[key] = propZodSchema.optional().default(null);+ } else {+ // Make the field nullable with the null default+ properties[key] = propZodSchema.nullable().optional().default(null);+ }} else {properties[key] = propZodSchema.optional().default(propJsonSchema.default);}diff --git a/ts/packages/json-schema-to-zod/src/parsers/parse-schema.ts b/ts/packages/json-schema-to-zod/src/parsers/parse-schema.tsindex 09bfac618..8396409cd 100644--- a/ts/packages/json-schema-to-zod/src/parsers/parse-schema.ts+++ b/ts/packages/json-schema-to-zod/src/parsers/parse-schema.ts@@ -53,8 +53,23 @@ const addDescribes = (jsonSchema: JsonSchemaObject, zodSchema: z.ZodTypeAny): z.return zodSchema;};-const addDefaults = (jsonSchema: JsonSchemaObject, zodSchema: z.ZodTypeAny): z.ZodTypeAny => {+const addDefaults = (+ jsonSchema: JsonSchemaObject,+ zodSchema: z.ZodTypeAny,+ refs?: Refs+): z.ZodTypeAny => {if (jsonSchema.default !== undefined) {+ // Don't apply null defaults to non-nullable types within anyOf/oneOf contexts+ if (jsonSchema.default === null) {+ // Check if we're in an anyOf/oneOf context by examining the path+ const inAnyOfOrOneOf = refs?.path.some(segment => segment === 'anyOf' || segment === 'oneOf');++ // If we're in anyOf/oneOf and this is a non-nullable type, skip the default+ if (inAnyOfOrOneOf && jsonSchema.type && jsonSchema.type !== 'null' && !jsonSchema.nullable) {+ return zodSchema;+ }+ }+zodSchema = zodSchema.default(jsonSchema.default);}@@ -144,7 +159,7 @@ export const parseSchema = (}if (!refs.withoutDefaults) {- parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);+ parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);}parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);diff --git a/ts/packages/json-schema-to-zod/test/comprehensive-edge-case-tests.ts b/ts/packages/json-schema-to-zod/test/comprehensive-edge-case-tests.tsnew file mode 100644index 000000000..ea3f6f44b--- /dev/null+++ b/ts/packages/json-schema-to-zod/test/comprehensive-edge-case-tests.ts@@ -0,0 +1,258 @@+import { jsonSchemaToZod } from '../src/json-schema-to-zod';+import zodToJsonSchema from 'zod-to-json-schema';+import type { JsonSchema } from '../src/types';++console.log('=== Testing Complex Schema Edge Cases ===\n');++// Test 1: Original anyOf schema (not from nullable conversion)+console.log('Test 1: Original anyOf with different types and defaults');+const originalAnyOfSchema: JsonSchema = {+ type: 'object',+ properties: {+ flexible_field: {+ anyOf: [+ {+ type: 'string',+ minLength: 5,+ default: 'hello', // This should be preserved+ },+ {+ type: 'number',+ minimum: 0,+ default: 42, // This should be preserved+ },+ {+ type: 'boolean',+ default: true, // This should be preserved+ },+ ],+ description: 'A field that can be string, number, or boolean',+ default: 'hello',+ },+ status: {+ anyOf: [+ {+ type: 'string',+ enum: ['active', 'inactive'],+ default: 'active',+ },+ {+ type: 'number',+ enum: [1, 0],+ default: 1,+ },+ ],+ default: 'active',+ },+ },+ required: [],+};++console.log('Original schema:');+console.log(JSON.stringify(originalAnyOfSchema, null, 2));++const zodSchema1 = jsonSchemaToZod(originalAnyOfSchema);+const backToJson1 = zodToJsonSchema(zodSchema1, { target: 'jsonSchema7' });++console.log('\nConverted back to JSON:');+console.log(JSON.stringify(backToJson1, null, 2));++console.log('\nTesting parsing:');+try {+ console.log('Empty object:', zodSchema1.parse({}));+ console.log('With string flexible_field:', zodSchema1.parse({ flexible_field: 'world' }));+ console.log('With number flexible_field:', zodSchema1.parse({ flexible_field: 123 }));+ console.log('With boolean flexible_field:', zodSchema1.parse({ flexible_field: false }));+} catch (error) {+ console.error('Parse error:', error);+}++console.log('\n' + '='.repeat(80) + '\n');++// Test 2: Array with different types (union types)+console.log('Test 2: Array with union of different types');+const arrayUnionSchema: JsonSchema = {+ type: 'object',+ properties: {+ mixed_items: {+ type: 'array',+ items: {+ anyOf: [+ {+ type: 'string',+ pattern: '^[a-zA-Z]+$',+ default: 'text',+ },+ {+ type: 'number',+ multipleOf: 2,+ default: 10,+ },+ {+ type: 'object',+ properties: {+ id: { type: 'number' },+ name: { type: 'string' },+ },+ required: ['id'],+ default: { id: 1, name: 'default' },+ },+ ],+ },+ default: ['text', 10, { id: 1, name: 'default' }],+ description: 'Array that can contain strings, numbers, or objects',+ },+ tags: {+ type: 'array',+ items: {+ oneOf: [+ {+ type: 'string',+ enum: ['important', 'urgent', 'normal'],+ default: 'normal',+ },+ {+ type: 'number',+ minimum: 1,+ maximum: 5,+ default: 3,+ },+ ],+ },+ default: ['normal', 3],+ description: 'Tags can be priority strings or numeric ratings',+ },+ },+ required: [],+};++console.log('Original schema:');+console.log(JSON.stringify(arrayUnionSchema, null, 2));++const zodSchema2 = jsonSchemaToZod(arrayUnionSchema);+const backToJson2 = zodToJsonSchema(zodSchema2, { target: 'jsonSchema7' });++console.log('\nConverted back to JSON:');+console.log(JSON.stringify(backToJson2, null, 2));++console.log('\nTesting parsing:');+try {+ console.log('Empty object:', zodSchema2.parse({}));+ console.log(+ 'With mixed array:',+ zodSchema2.parse({+ mixed_items: ['hello', 42, { id: 5, name: 'test' }],+ tags: ['urgent', 4, 'normal'],+ })+ );+} catch (error) {+ console.error('Parse error:', error);+}++console.log('\n' + '='.repeat(80) + '\n');++// Test 3: Complex nested schema with multiple anyOf and nullable fields+console.log('Test 3: Complex nested schema with anyOf and nullable');+const complexNestedSchema: JsonSchema = {+ type: 'object',+ properties: {+ user: {+ anyOf: [+ {+ type: 'object',+ properties: {+ id: { type: 'string', default: 'user-123' },+ name: { type: 'string' },+ email: {+ type: 'string',+ format: 'email',+ nullable: true,+ default: null,+ } as any,+ },+ required: ['name'],+ default: { id: 'user-123', name: 'Anonymous', email: null },+ },+ {+ type: 'string',+ pattern: '^user-\\d+$',+ default: 'user-000',+ },+ ],+ description: 'User can be an object or a user ID string',+ default: { id: 'user-123', name: 'Anonymous', email: null },+ },+ metadata: {+ type: 'object',+ properties: {+ settings: {+ anyOf: [+ {+ type: 'object',+ properties: {+ theme: {+ type: 'string',+ enum: ['light', 'dark'],+ default: 'light',+ },+ notifications: {+ type: 'boolean',+ default: true,+ },+ },+ default: { theme: 'light', notifications: true },+ },+ {+ type: 'string',+ enum: ['minimal', 'full'],+ default: 'full',+ },+ ],+ default: { theme: 'light', notifications: true },+ },+ version: {+ type: 'string',+ nullable: true,+ default: null,+ } as any,+ },+ default: {+ settings: { theme: 'light', notifications: true },+ version: null,+ },+ },+ },+ required: [],+};++console.log('Original schema:');+console.log(JSON.stringify(complexNestedSchema, null, 2));++const zodSchema3 = jsonSchemaToZod(complexNestedSchema);+const backToJson3 = zodToJsonSchema(zodSchema3, { target: 'jsonSchema7' });++console.log('\nConverted back to JSON:');+console.log(JSON.stringify(backToJson3, null, 2));++console.log('\nTesting parsing:');+try {+ console.log('Empty object:', zodSchema3.parse({}));+ console.log(+ 'With user object:',+ zodSchema3.parse({+ user: { name: 'John Doe', email: '[email protected]' },+ metadata: {+ settings: 'minimal',+ version: '1.0.0',+ },+ })+ );+ console.log(+ 'With user string:',+ zodSchema3.parse({+ user: 'user-456',+ })+ );+} catch (error) {+ console.error('Parse error:', error);+}