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(json-schema-to-zod): keep guarded tool schemas as objects
The whole-schema guard from #4316 wrapped the parsed schema in z.any().pipe(), so providers emitted tool parameters as {allOf: [{}, {type: object}]} and OpenAI and Anthropic rejected them. Guard nodes by subclassing their own Zod class instead, and give the guard's interpreter Unicode-mode spellings of patterns with identity escapes such as \_ that the u flag refuses.
jkomyno committed
Sep 29, 2026 at 14:29 UTC
077ceb3ccf8c67559b8813fff93f56c3a08698c1
3 files changed
+177
-32
.changeset/json-schema-to-zod-object-root.md
new
+6
@@ -0,0 +1,6 @@
1
+---
2
+'@composio/json-schema-to-zod': patch
3
+'@composio/core': patch
4
+---
5
+
6
+Fix tool schemas rejected by OpenAI and Anthropic when a parameter uses `anyOf`, `oneOf`, `allOf`, `$ref`, or similar keywords. The converted schema is a `ZodObject` again, so the LangChain, Vercel, LlamaIndex, and Claude Agent SDK providers send tool parameters with a top-level `type: "object"`. Patterns with escapes such as `\_` or `\:` no longer fail every call to the tool.
ts/packages/json-schema-to-zod/src/whole-schema-validation.ts
+136
-32
@@ -161,12 +161,65 @@ export const guardSchemaAt = (
161
{ ...refs.root, $ref: `#/${refs.path.map(part => encodePointer(String(part))).join('/')}` }
162
: node;
163
164
+const compilesAsUnicode = (pattern: string): boolean => {
165
+ try {
166
+ new RegExp(pattern, 'u');
167
+ return true;
168
+ } catch {
169
+ return false;
170
+ }
171
+};
172
+
173
+const REGEX_SYNTAX_CHARACTERS = new Set('^$\\.*+?()[]{}|/');
174
+
175
+/**
176
+ * The interpreter compiles every pattern with the `u` flag, while
177
+ * `compilePattern` deliberately does not. Tool schemas routinely use identity
178
+ * escapes such as `\_` or `\:` that are literal characters without the flag
179
+ * and a SyntaxError with it, so those escapes are dropped. Returns `undefined`
180
+ * when the pattern still has no Unicode-mode spelling.
181
+ */
182
+const toUnicodePattern = (pattern: string): string | undefined => {
183
+ if (compilesAsUnicode(pattern)) {
184
+ return pattern;
185
+ }
186
+
187
+ let rewritten = '';
188
+ let inClass = false;
189
+ for (let index = 0; index < pattern.length; index++) {
190
+ const char = pattern[index];
191
+ if (char === '\\' && index + 1 < pattern.length) {
192
+ const escaped = pattern[++index];
193
+ const keepEscape =
194
+ /[A-Za-z0-9]/.test(escaped) ||
195
+ REGEX_SYNTAX_CHARACTERS.has(escaped) ||
196
+ (inClass && escaped === '-');
197
+ rewritten += keepEscape ? `\\${escaped}` : escaped;
198
+ continue;
199
+ }
200
+ if (char === '[') {
201
+ inClass = true;
202
+ } else if (char === ']') {
203
+ inClass = false;
204
+ }
205
+ rewritten += char;
206
+ }
207
+
208
+ return compilesAsUnicode(rewritten) ? rewritten : undefined;
209
+};
210
+
211
/**
165
- * OpenAPI 3.0 / Draft 4 spell exclusive bounds as a boolean flag next to
166
- * `minimum`/`maximum`. The Draft 7 interpreter ignores the flag, so the guard
167
- * receives the numeric spelling the native number parser already honors.
212
+ * Rewrites the guard's private copy of a schema into what the Draft 7
213
+ * interpreter understands the way the native parsers do:
214
+ *
215
+ * - OpenAPI 3.0 / Draft 4 spell exclusive bounds as a boolean flag next to
216
+ * `minimum`/`maximum`. The interpreter ignores the flag, so it receives the
217
+ * numeric spelling the native number parser already honors.
218
+ * - Patterns get their Unicode-mode spelling (see `toUnicodePattern`). A
219
+ * `pattern` without one is left to the native string parser, which compiles
220
+ * it without the flag, instead of failing every value.
221
*/
169
-const normalizeExclusiveBounds = (value: unknown, seen: WeakSet<object>): void => {
222
+const prepareInterpreterSchema = (value: unknown, seen: WeakSet<object>): void => {
223
if (!isObject(value) || seen.has(value)) {
224
return;
225
}
@@ -185,54 +238,105 @@ const normalizeExclusiveBounds = (value: unknown, seen: WeakSet<object>): void =
238
delete value.exclusiveMaximum;
239
}
240
241
+ if (typeof value.pattern === 'string') {
242
+ const pattern = toUnicodePattern(value.pattern);
243
+ if (pattern === undefined) {
244
+ delete value.pattern;
245
+ } else {
246
+ value.pattern = pattern;
247
+ }
248
+ }
249
+ if (isObject(value.patternProperties)) {
250
+ value.patternProperties = Object.fromEntries(
251
+ Object.entries(value.patternProperties).map(([pattern, schema]) => [
252
+ toUnicodePattern(pattern) ?? pattern,
253
+ schema,
254
+ ])
255
+ );
256
+ }
257
+
258
for (const [key, child] of Object.entries(value)) {
259
if (SCHEMA_MAP_KEYWORDS.has(key) && isObject(child)) {
190
- Object.values(child).forEach(nested => normalizeExclusiveBounds(nested, seen));
260
+ Object.values(child).forEach(nested => prepareInterpreterSchema(nested, seen));
261
} else if (SCHEMA_ARRAY_KEYWORDS.has(key) && Array.isArray(child)) {
192
- child.forEach(nested => normalizeExclusiveBounds(nested, seen));
262
+ child.forEach(nested => prepareInterpreterSchema(nested, seen));
263
} else if (SCHEMA_VALUE_KEYWORDS.has(key)) {
264
(Array.isArray(child) ? child : [child]).forEach(nested =>
195
- normalizeExclusiveBounds(nested, seen)
265
+ prepareInterpreterSchema(nested, seen)
266
);
267
}
268
}
269
};
270
271
+type WholeSchemaGuard = (value: unknown) => string | undefined;
272
+type GuardedDef = z.ZodTypeDef & { wholeSchemaGuard?: WholeSchemaGuard };
273
+type ZodSchemaClass = {
274
+ new (def: z.ZodTypeDef): z.ZodTypeAny;
275
+ prototype: z.ZodTypeAny;
276
+};
277
+
278
+const guardedSchemaClasses = new WeakMap<ZodSchemaClass, ZodSchemaClass>();
279
+
280
+/**
281
+ * A subclass of the parsed schema's own class that checks the source value
282
+ * against the guard before parsing it. Keeping the class, rather than wrapping
283
+ * it in a pipeline, keeps the schema's kind: a guarded object is still a
284
+ * `ZodObject` with a `shape`, which is what zod-to-json-schema and the MCP SDK
285
+ * need to emit a root `type: "object"` for LLM tool parameters. The guard lives
286
+ * on `_def`, so copies such as `.describe()` keep it.
287
+ */
288
+const guardedSchemaClass = (Base: ZodSchemaClass): ZodSchemaClass => {
289
+ const cached = guardedSchemaClasses.get(Base);
290
+ if (cached) {
291
+ return cached;
292
+ }
293
+
294
+ const parseAsBase = Base.prototype._parse;
295
+ class Guarded extends Base {
296
+ override _parse(input: z.ParseInput): z.ParseReturnType<unknown> {
297
+ const failure = (this._def as GuardedDef).wholeSchemaGuard?.(input.data);
298
+ if (failure === undefined) {
299
+ return parseAsBase.call(this, input);
300
+ }
301
+ z.addIssueToContext(this._getOrReturnCtx(input), {
302
+ code: z.ZodIssueCode.custom,
303
+ message: failure,
304
+ });
305
+ return z.DIRTY(input.data);
306
+ }
307
+ }
308
+ guardedSchemaClasses.set(Base, Guarded);
309
+ // Guarding an already guarded schema reuses its class and composes guards.
310
+ guardedSchemaClasses.set(Guarded, Guarded);
311
+ return Guarded;
312
+};
313
+
314
export const withWholeSchemaValidation = (
315
jsonSchema: JsonSchema,
316
parsedSchema: z.ZodTypeAny
317
): z.ZodTypeAny => {
318
const interpreterSchema = structuredClone(jsonSchema);
206
- normalizeExclusiveBounds(interpreterSchema, new WeakSet());
319
+ prepareInterpreterSchema(interpreterSchema, new WeakSet());
320
const validator = new Validator(interpreterSchema as InterpreterSchema, '7', false);
321
322
// Validate the source value before defaults and other Zod transforms run.
323
// Otherwise a missing required field can be synthesized and incorrectly
324
// appear valid to the JSON Schema interpreter.
212
- const guardedSchema = z
213
- .any()
214
- .superRefine((value, ctx) => {
215
- let result: ReturnType<Validator['validate']>;
216
- try {
217
- result = validator.validate(value);
218
- } catch (cause) {
219
- ctx.addIssue({
220
- code: z.ZodIssueCode.custom,
221
- message: `JSON Schema validation failed: ${String(cause)}`,
222
- });
223
- return;
224
- }
325
+ const innerGuard = (parsedSchema._def as GuardedDef).wholeSchemaGuard;
326
+ const wholeSchemaGuard: WholeSchemaGuard = value => {
327
+ let result: ReturnType<Validator['validate']>;
328
+ try {
329
+ result = validator.validate(value);
330
+ } catch (cause) {
331
+ return `JSON Schema validation failed: ${String(cause)}`;
332
+ }
333
226
- if (!result.valid) {
227
- ctx.addIssue({
228
- code: z.ZodIssueCode.custom,
229
- message: result.errors[0]?.error ?? 'Input does not satisfy the complete JSON Schema.',
230
- });
231
- }
232
- })
233
- .pipe(parsedSchema);
334
+ if (!result.valid) {
335
+ return result.errors[0]?.error ?? 'Input does not satisfy the complete JSON Schema.';
336
+ }
337
+ return innerGuard?.(value);
338
+ };
339
235
- return parsedSchema.description
236
- ? guardedSchema.describe(parsedSchema.description)
237
- : guardedSchema;
340
+ const Guarded = guardedSchemaClass(parsedSchema.constructor as ZodSchemaClass);
341
+ return new Guarded({ ...parsedSchema._def, wholeSchemaGuard } as GuardedDef);
342
};
ts/packages/json-schema-to-zod/test/semantic-regressions.test.ts
+35
@@ -323,6 +323,41 @@ describe('whole-schema semantic regressions', () => {
323
expect(jsonSchemaToZod(schema).safeParse(value).success).toBe(false);
324
});
325
326
+ it('keeps a guarded object root a ZodObject with its shape', () => {
327
+ const schema: JsonSchema = {
328
+ type: 'object',
329
+ properties: {
330
+ q: { type: 'string' },
331
+ limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] },
332
+ },
333
+ required: ['q'],
334
+ };
335
+ const parsed = jsonSchemaToZod(schema);
336
+
337
+ expect(parsed).toBeInstanceOf(z.ZodObject);
338
+ expect(Object.keys((parsed as z.AnyZodObject).shape)).toEqual(['q', 'limit']);
339
+ expect(parsed.safeParse({ q: 'cats', limit: null }).success).toBe(true);
340
+ expect(parsed.safeParse({ q: 'cats', limit: 'ten' }).success).toBe(false);
341
+ expect(parsed.describe('Search').safeParse({ q: 'cats', limit: 'ten' }).success).toBe(false);
342
+ });
343
+
344
+ it('accepts patterns with identity escapes that Unicode mode refuses', () => {
345
+ const schema: JsonSchema = {
346
+ type: 'object',
347
+ properties: {
348
+ slug: { type: 'string', pattern: '^[a-z\\_]+$' },
349
+ ref: { $ref: '#/$defs/handle' },
350
+ limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] },
351
+ },
352
+ $defs: { handle: { type: 'string', pattern: '^\\@[a-z\\-]+$' } },
353
+ };
354
+ const parsed = jsonSchemaToZod(schema);
355
+
356
+ expect(parsed.safeParse({ slug: 'a_b', ref: '@a-b' }).success).toBe(true);
357
+ expect(parsed.safeParse({ slug: 'A' }).success).toBe(false);
358
+ expect(parsed.safeParse({ ref: 'a-b' }).success).toBe(false);
359
+ });
360
+
361
it('compares decimal multiples by their JSON number spelling', () => {
362
const schema: JsonSchema = { type: 'number', multipleOf: 0.1 };
363
expect(jsonSchemaToZod(schema).safeParse(0.3).success).toBe(true);