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 21, 14:45 UTC
fix(openai): send tools strict mode cannot express without strict
OpenAIResponsesProvider emits the strict schema and strict: true only when the rewrite is lossless; otherwise the tool keeps its original schema with strict: false and a warning names the tool and path. Tools without parameters get a canonical empty closed object. Null arguments the tool schema rejects are dropped before execution. Co-authored-by: AseemPrasad <aseemprasad0520@gmail.com> Claude-Session: https://claude.ai/code/session_01TDrxCHn2hg51HmxVstSUgs
jkomyno committed
Aug 26, 2026 at 17:19 UTC
6a7ddbad0619663e294d67ce2104352c47bdcd8e
2 files changed
+148
-36
ts/packages/providers/openai/src/OpenAIResponsesProvider.ts
+66
-22
@@ -20,12 +20,20 @@ import {
20
McpUrlResponse,
21
normalizeToolArguments,
22
deduplicateJsonSchemaRequiredArrays,
23
- dereferenceJsonSchema,
23
+ omitNullToolArguments,
24
toStrictJsonSchema,
25
logger,
26
} from '@composio/core';
27
28
export type OpenAiTool = OpenAI.Responses.FunctionTool;
29
+
30
+/** Parameters emitted for a tool without input parameters under strict mode. */
31
+const EMPTY_OBJECT_SCHEMA = {
32
+ type: 'object',
33
+ properties: {},
34
+ required: [],
35
+ additionalProperties: false,
36
+} as const;
37
export type OpenAiMcpTool = OpenAI.Responses.Tool.Mcp;
38
export type OpenAiToolCollection = Array<OpenAiTool>;
39
export type OpenAIResponsesProviderOptions = {
@@ -46,6 +54,12 @@ export class OpenAIResponsesProvider extends BaseNonAgenticProvider<
54
> {
55
readonly name = 'openai';
56
private strict: boolean | null;
57
+ /**
58
+ * Dereferenced parameter schemas of the tools wrapped under strict mode,
59
+ * keyed by slug, so tool-call arguments can be reconciled against the
60
+ * schema the model actually saw.
61
+ */
62
+ private readonly strictInputSchemas = new Map<string, Record<string, unknown>>();
63
64
/**
65
* Creates a new instance of the OpenAIProvider.
@@ -124,32 +138,56 @@ export class OpenAIResponsesProvider extends BaseNonAgenticProvider<
138
*/
139
override wrapTool(tool: Tool): OpenAiTool {
140
const inputParams = tool.inputParameters;
127
-
128
- let parameters: Record<string, unknown> = (inputParams ?? {}) as Record<string, unknown>;
129
- if (this.strict && inputParams?.type === 'object') {
130
- // Structured outputs reject schemas whose nested objects keep optional
131
- // properties or their own additionalProperties, so the strict contract
132
- // must be applied at every depth. Inline $ref/$defs first (lenient mode
133
- // keeps upstream schemas with dangling refs usable), then normalize.
134
- const dereferenced = dereferenceJsonSchema(parameters, {
135
- onUnresolved: 'sentinel',
136
- onReplace: ref =>
137
- logger.debug(
138
- `OpenAIResponsesProvider: unresolved $ref "${ref}" in tool "${tool.slug}" replaced with a permissive schema`
139
- ),
140
- });
141
- parameters = toStrictJsonSchema<Record<string, unknown>>(dereferenced).schema;
141
+ if (!this.strict) {
142
+ return {
143
+ name: tool.slug,
144
+ description: tool.description,
145
+ // Canonicalize required arrays at the vendor-schema emission boundary.
146
+ parameters: deduplicateJsonSchemaRequiredArrays(
147
+ (inputParams ?? {}) as Record<string, unknown>
148
+ ),
149
+ strict: this.strict,
150
+ type: 'function',
151
+ };
152
}
153
144
- // Canonicalize required arrays at the vendor-schema emission boundary,
145
- // after any provider-specific schema transformations.
146
- const wrappedParameters = deduplicateJsonSchemaRequiredArrays(parameters);
154
+ // Structured outputs enforce their contract at every depth: all
155
+ // properties required, closed objects, no annotation keywords. The strict
156
+ // rewrite keeps every parameter (optional ones become nullable) and
157
+ // reports constructs it cannot express; such a tool is sent without
158
+ // strict mode rather than with a narrower schema.
159
+ const source = (inputParams ?? EMPTY_OBJECT_SCHEMA) as Record<string, unknown>;
160
+ const strict = toStrictJsonSchema(source);
161
+ if (strict.unsupported.length > 0) {
162
+ const reasons = strict.unsupported
163
+ .map(entry => `${entry.path || '<root>'}: ${entry.keyword} (${entry.detail})`)
164
+ .join('; ');
165
+ logger.warn(
166
+ `OpenAIResponsesProvider: tool "${tool.slug}" is sent without strict mode because its schema cannot be expressed as strict structured outputs: ${reasons}`
167
+ );
168
+ this.strictInputSchemas.delete(tool.slug);
169
+ return {
170
+ name: tool.slug,
171
+ description: tool.description,
172
+ parameters: deduplicateJsonSchemaRequiredArrays(source),
173
+ strict: false,
174
+ type: 'function',
175
+ };
176
+ }
177
+ if (strict.totalChanges > 0) {
178
+ logger.debug(
179
+ `OpenAIResponsesProvider: strict mode rewrote ${strict.totalChanges} node(s) of tool "${tool.slug}": ${strict.changes
180
+ .map(change => `${change.path}: ${change.reason}`)
181
+ .join('; ')}`
182
+ );
183
+ }
184
+ this.strictInputSchemas.set(tool.slug, strict.source);
185
186
return {
187
name: tool.slug,
188
description: tool.description,
151
- parameters: wrappedParameters,
152
- strict: this.strict,
189
+ parameters: strict.schema,
190
+ strict: true,
191
type: 'function',
192
};
193
}
@@ -246,7 +284,13 @@ export class OpenAIResponsesProvider extends BaseNonAgenticProvider<
284
): Promise<string> {
285
// OpenAI always serializes tool arguments as a JSON string; normalize tolerates
286
// empty / object-shaped payloads too (issue #2406).
249
- const arguments_ = normalizeToolArguments(tool.arguments, tool.name);
287
+ const normalizedArguments = normalizeToolArguments(tool.arguments, tool.name);
288
+ // Under strict mode optional parameters are emitted as required-nullable,
289
+ // so a `null` the tool's own schema does not accept means "omitted".
290
+ const strictSchema = this.strictInputSchemas.get(tool.name);
291
+ const arguments_ = strictSchema
292
+ ? omitNullToolArguments(normalizedArguments, strictSchema)
293
+ : normalizedArguments;
294
const result = await this.executeToolForTarget(
295
executionTarget,
296
tool.name,
ts/packages/providers/openai/test/opeanai-responses.test.ts
+82
-14
@@ -130,7 +130,7 @@ describe('OpenAIResponsesProvider', () => {
130
expect(wrapped.parameters.required).toEqual(['input']);
131
});
132
133
- it('normalizes nested objects, nullable types and anyOf under strict mode', () => {
133
+ it('keeps optional parameters as required-nullable under strict mode', () => {
134
const strictProvider = new OpenAIResponsesProvider({ strict: true });
135
const wrapped = strictProvider.wrapTool({
136
...mockTool,
@@ -156,39 +156,42 @@ describe('OpenAIResponsesProvider', () => {
156
{ type: 'null' },
157
],
158
},
159
+ label: { type: 'string', default: 'x' },
160
},
161
required: ['cfg', 'id', 'payload'],
162
},
163
}) as MockedOpenAITool;
164
165
+ expect(wrapped.strict).toBe(true);
166
expect(wrapped.parameters).toEqual({
167
type: 'object',
168
properties: {
169
cfg: {
170
type: 'object',
169
- properties: { url: { type: 'string' } },
170
- required: ['url'],
171
+ properties: { url: { type: 'string' }, note: { type: ['string', 'null'] } },
172
+ required: ['url', 'note'],
173
additionalProperties: false,
174
},
173
- id: { anyOf: [{ type: 'string' }, { type: 'null' }] },
175
+ id: { type: ['string', 'null'] },
176
payload: {
177
anyOf: [
178
{
179
type: 'object',
178
- properties: { inner: { type: 'string' } },
179
- required: ['inner'],
180
+ properties: { inner: { type: 'string' }, extra: { type: ['string', 'null'] } },
181
+ required: ['inner', 'extra'],
182
additionalProperties: false,
183
},
184
{ type: 'null' },
185
],
186
},
187
+ label: { type: ['string', 'null'] },
188
},
186
- required: ['cfg', 'id', 'payload'],
189
+ required: ['cfg', 'id', 'payload', 'label'],
190
additionalProperties: false,
191
});
192
});
193
191
- it('dereferences $defs before normalizing under strict mode', () => {
194
+ it('keeps nullable nested objects nullable and inlines $defs under strict mode', () => {
195
const strictProvider = new OpenAIResponsesProvider({ strict: true });
196
const wrapped = strictProvider.wrapTool({
197
...mockTool,
@@ -200,7 +203,7 @@ describe('OpenAIResponsesProvider', () => {
203
required: ['cfg'],
204
$defs: {
205
Config: {
203
- type: 'object',
206
+ type: ['object', 'null'],
207
properties: { url: { type: 'string' }, opt: { type: 'string' } },
208
required: ['url'],
209
},
@@ -209,13 +212,14 @@ describe('OpenAIResponsesProvider', () => {
212
}) as MockedOpenAITool;
213
214
expect(JSON.stringify(wrapped.parameters)).not.toContain('$ref');
215
+ expect(wrapped.strict).toBe(true);
216
expect(wrapped.parameters).toEqual({
217
type: 'object',
218
properties: {
219
cfg: {
216
- type: 'object',
217
- properties: { url: { type: 'string' } },
218
- required: ['url'],
220
+ type: ['object', 'null'],
221
+ properties: { url: { type: 'string' }, opt: { type: ['string', 'null'] } },
222
+ required: ['url', 'opt'],
223
additionalProperties: false,
224
},
225
},
@@ -224,14 +228,36 @@ describe('OpenAIResponsesProvider', () => {
228
});
229
});
230
227
- it('leaves non-object schemas untouched in strict mode apart from required dedup', () => {
231
+ it('sends tools strict mode cannot express without strict, keeping their schema', () => {
232
+ const strictProvider = new OpenAIResponsesProvider({ strict: true });
233
+ const inputParameters = {
234
+ type: 'object',
235
+ properties: {
236
+ headers: { type: 'object', additionalProperties: { type: 'string' } },
237
+ name: { type: 'string' },
238
+ },
239
+ required: ['headers', 'name', 'name'],
240
+ };
241
+ const wrapped = strictProvider.wrapTool({ ...mockTool, inputParameters }) as MockedOpenAITool;
242
+
243
+ expect(wrapped.strict).toBe(false);
244
+ expect(wrapped.parameters).toEqual({ ...inputParameters, required: ['headers', 'name'] });
245
+ });
246
+
247
+ it('emits an empty closed object for tools without parameters in strict mode', () => {
248
const strictProvider = new OpenAIResponsesProvider({ strict: true });
249
const wrapped = strictProvider.wrapTool({
250
...mockTool,
251
inputParameters: undefined,
252
}) as MockedOpenAITool;
253
234
- expect(wrapped.parameters).toEqual({});
254
+ expect(wrapped.strict).toBe(true);
255
+ expect(wrapped.parameters).toEqual({
256
+ type: 'object',
257
+ properties: {},
258
+ required: [],
259
+ additionalProperties: false,
260
+ });
261
});
262
});
263
@@ -301,6 +327,48 @@ describe('OpenAIResponsesProvider', () => {
327
);
328
});
329
330
+ it('omits nulls the tool schema does not accept before executing under strict mode', async () => {
331
+ const strictProvider = new OpenAIResponsesProvider({ strict: true });
332
+ strictProvider._setExecuteToolFn(mockExecuteToolFn);
333
+ strictProvider.wrapTool({
334
+ ...mockTool,
335
+ slug: 'test-tool',
336
+ inputParameters: {
337
+ type: 'object',
338
+ properties: {
339
+ cfg: {
340
+ type: 'object',
341
+ properties: { url: { type: 'string' }, note: { type: 'string' } },
342
+ required: ['url'],
343
+ },
344
+ label: { type: 'string' },
345
+ clearable: { type: ['string', 'null'] },
346
+ },
347
+ required: ['cfg'],
348
+ },
349
+ });
350
+ const toolCall = {
351
+ id: 'call-123',
352
+ type: 'function',
353
+ name: 'test-tool',
354
+ arguments: JSON.stringify({ cfg: { url: 'u', note: null }, label: null, clearable: null }),
355
+ call_id: 'call-123',
356
+ } as unknown as OpenAI.Responses.ResponseFunctionToolCall;
357
+
358
+ await strictProvider.executeToolCall('test-user', toolCall);
359
+
360
+ expect(mockExecuteToolFn).toHaveBeenCalledWith(
361
+ 'test-tool',
362
+ {
363
+ arguments: { cfg: { url: 'u' }, clearable: null },
364
+ userId: 'test-user',
365
+ connectedAccountId: undefined,
366
+ customAuthParams: undefined,
367
+ },
368
+ undefined
369
+ );
370
+ });
371
+
372
it('should coerce empty-string arguments to an empty object (issue #2406)', async () => {
373
const toolCall = {
374
id: 'call-123',