Skip to content

Commit d532ac3

Browse files
tjenkinsonlgrammel
andauthored
Support additionalProperties in open api schema (#5626)
`z.record(z.string())` as a property in tool parameters currently gets converted to `undefined`, which is invalid and causes an error from vertex ai. This now gets converted to `{ type: 'object', additionalProperties: { type: 'string' } }`, which works fixes #5628 --------- Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
1 parent 3ac547c commit d532ac3

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

.changeset/cold-schools-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/google': patch
3+
---
4+
5+
Support tool schemas that allow additional properties (e.g `z.record(z.string())`)

packages/google/src/convert-json-schema-to-openapi-schema.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ it('should remove additionalProperties and $schema', () => {
2323
expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected);
2424
});
2525

26+
it('supports additionalProperties: true', () => {
27+
const input: JSONSchema7 = {
28+
$schema: 'http://json-schema.org/draft-07/schema#',
29+
type: 'object',
30+
properties: {
31+
name: { type: 'string' },
32+
age: { type: 'number' },
33+
},
34+
additionalProperties: true,
35+
};
36+
37+
const expected = {
38+
type: 'object',
39+
properties: {
40+
name: { type: 'string' },
41+
age: { type: 'number' },
42+
},
43+
additionalProperties: true,
44+
};
45+
46+
expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected);
47+
});
48+
49+
// z.record(z.string())
50+
it('supports `additionalProperties` as an object', () => {
51+
const input: JSONSchema7 = {
52+
$schema: 'http://json-schema.org/draft-07/schema#',
53+
type: 'object',
54+
properties: {
55+
name: { type: 'string' },
56+
age: { type: 'number' },
57+
},
58+
additionalProperties: { type: 'string' },
59+
};
60+
61+
const expected = {
62+
type: 'object',
63+
properties: {
64+
name: { type: 'string' },
65+
age: { type: 'number' },
66+
},
67+
additionalProperties: { type: 'string' },
68+
};
69+
70+
expect(convertJSONSchemaToOpenAPISchema(input)).toEqual(expected);
71+
});
72+
2673
it('should handle nested objects and arrays', () => {
2774
const input: JSONSchema7 = {
2875
type: 'object',

packages/google/src/convert-json-schema-to-openapi-schema.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function convertJSONSchemaToOpenAPISchema(
2828
const: constValue,
2929
minLength,
3030
enum: enumValues,
31+
additionalProperties,
3132
} = jsonSchema;
3233

3334
const result: Record<string, unknown> = {};
@@ -115,6 +116,13 @@ export function convertJSONSchemaToOpenAPISchema(
115116
result.minLength = minLength;
116117
}
117118

119+
if (additionalProperties === true) {
120+
result.additionalProperties = true;
121+
} else if (additionalProperties) {
122+
result.additionalProperties =
123+
convertJSONSchemaToOpenAPISchema(additionalProperties);
124+
}
125+
118126
return result;
119127
}
120128

@@ -124,6 +132,7 @@ function isEmptyObjectSchema(jsonSchema: JSONSchema7Definition): boolean {
124132
typeof jsonSchema === 'object' &&
125133
jsonSchema.type === 'object' &&
126134
(jsonSchema.properties == null ||
127-
Object.keys(jsonSchema.properties).length === 0)
135+
Object.keys(jsonSchema.properties).length === 0) &&
136+
!jsonSchema.additionalProperties
128137
);
129138
}

0 commit comments

Comments
 (0)