diff --git a/.changeset/cold-schools-sip.md b/.changeset/cold-schools-sip.md new file mode 100644 index 000000000000..6b814e6e3611 --- /dev/null +++ b/.changeset/cold-schools-sip.md @@ -0,0 +1,5 @@ +--- +'@ai-sdk/google': patch +--- + +Support tool schemas that allow additional properties (e.g `z.record(z.string())`) diff --git a/packages/google/src/convert-json-schema-to-openapi-schema.test.ts b/packages/google/src/convert-json-schema-to-openapi-schema.test.ts index cf1b7fce992a..3a7e1e527495 100644 --- a/packages/google/src/convert-json-schema-to-openapi-schema.test.ts +++ b/packages/google/src/convert-json-schema-to-openapi-schema.test.ts @@ -23,6 +23,53 @@ it('should remove additionalProperties and $schema', () => { expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected); }); +it('supports additionalProperties: true', () => { + const input: JSONSchema7 = { + $schema: 'http://json-schema.org/draft-07/schema#', + type: 'object', + properties: { + name: { type: 'string' }, + age: { type: 'number' }, + }, + additionalProperties: true, + }; + + const expected = { + type: 'object', + properties: { + name: { type: 'string' }, + age: { type: 'number' }, + }, + additionalProperties: true, + }; + + expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected); +}); + +// z.record(z.string()) +it('supports `additionalProperties` as an object', () => { + const input: JSONSchema7 = { + $schema: 'http://json-schema.org/draft-07/schema#', + type: 'object', + properties: { + name: { type: 'string' }, + age: { type: 'number' }, + }, + additionalProperties: { type: 'string' }, + }; + + const expected = { + type: 'object', + properties: { + name: { type: 'string' }, + age: { type: 'number' }, + }, + additionalProperties: { type: 'string' }, + }; + + expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected); +}); + it('should handle nested objects and arrays', () => { const input: JSONSchema7 = { type: 'object', diff --git a/packages/google/src/convert-json-schema-to-openapi-schema.ts b/packages/google/src/convert-json-schema-to-openapi-schema.ts index dbd80d7cd7d4..70065ba8646d 100644 --- a/packages/google/src/convert-json-schema-to-openapi-schema.ts +++ b/packages/google/src/convert-json-schema-to-openapi-schema.ts @@ -28,6 +28,7 @@ export function convertJSONSchemaToOpenAPISchema( const: constValue, minLength, enum: enumValues, + additionalProperties, } = jsonSchema; const result: Record = {}; @@ -115,6 +116,13 @@ export function convertJSONSchemaToOpenAPISchema( result.minLength = minLength; } + if (additionalProperties === true) { + result.additionalProperties = true; + } else if (additionalProperties) { + result.additionalProperties = + convertJSONSchemaToOpenAPISchema(additionalProperties); + } + return result; } @@ -124,6 +132,7 @@ function isEmptyObjectSchema(jsonSchema: JSONSchema7Definition): boolean { typeof jsonSchema === 'object' && jsonSchema.type === 'object' && (jsonSchema.properties == null || - Object.keys(jsonSchema.properties).length === 0) + Object.keys(jsonSchema.properties).length === 0) && + !jsonSchema.additionalProperties ); }