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-agents): honor the strict option
jkomyno9692db5c0ab6
diff --git a/.changeset/openai-agents-honor-strict.md b/.changeset/openai-agents-honor-strict.mdnew file mode 100644index 000000000..100907342--- /dev/null+++ b/.changeset/openai-agents-honor-strict.md@@ -0,0 +1,5 @@+---+'@composio/openai-agents': patch+---++`OpenAIAgentsProvider({ strict: true })` now takes effect: tools are registered with `strict: true` and a schema normalized for OpenAI structured outputs (every property required, optional ones accept `null`), a `null` argument the tool's own schema does not accept is dropped before execution, and tools whose schema strict mode cannot express are registered without strict mode with a warning. The option was previously ignored.diff --git a/ts/packages/providers/openai-agents/README.md b/ts/packages/providers/openai-agents/README.mdindex 6ec1e20d8..ec341cc87 100644--- a/ts/packages/providers/openai-agents/README.md+++ b/ts/packages/providers/openai-agents/README.md@@ -43,7 +43,7 @@ For multi-turn conversations, store `session.sessionId` and reuse it with `compo## Strict mode-Pass `strict: true` to enable strict JSON schema validation for tool parameters (see the [Agents SDK options reference](https://openai.github.io/openai-agents-js/guides/tools/#options-reference)):+Pass `strict: true` to register tools with strict JSON schema validation (see the [Agents SDK options reference](https://openai.github.io/openai-agents-js/guides/tools/#options-reference)). Each tool's input schema is normalized for OpenAI structured outputs: every object lists all of its properties in `required` and is closed, and optional properties stay available but accept `null`; a `null` the model sends for an optional parameter is dropped before the tool runs. Tools whose schema cannot be expressed in strict mode (objects that accept arbitrary keys, `allOf`, `prefixItems`) are registered without strict mode and log a warning:```typescriptconst provider = new OpenAIAgentsProvider({ strict: true });diff --git a/ts/packages/providers/openai-agents/src/index.ts b/ts/packages/providers/openai-agents/src/index.tsindex 3d4d38cd9..68fb5aa7f 100644--- a/ts/packages/providers/openai-agents/src/index.ts+++ b/ts/packages/providers/openai-agents/src/index.ts@@ -18,11 +18,28 @@ import {McpServerGetResponse,normalizeToolArguments,deduplicateJsonSchemaRequiredArrays,+ logger,+ omitNullToolArguments,+ toStrictJsonSchema,} from '@composio/core';import type { Tool as OpenAIAgentTool } from '@openai/agents';import { tool as createOpenAIAgentTool } from '@openai/agents';type OpenAIAgentsToolCollection = Array<OpenAIAgentTool>;++/** The strict JSON Schema parameter shape the Agents SDK accepts. */+type StrictToolParameters = Extract<+ Parameters<typeof createOpenAIAgentTool>[0]['parameters'],+ { additionalProperties: false }+>;++/** Parameters registered for a tool without input parameters under strict mode. */+const EMPTY_OBJECT_SCHEMA = {+ type: 'object',+ properties: {},+ required: [],+ additionalProperties: false,+} as const;export class OpenAIAgentsProvider extends BaseAgenticProvider<OpenAIAgentsToolCollection,OpenAIAgentTool,@@ -125,6 +142,40 @@ export class OpenAIAgentsProvider extends BaseAgenticProvider<wrapTool(composioTool: ComposioTool, executeTool: ExecuteToolFn): OpenAIAgentTool {const inputParameters = deduplicateJsonSchemaRequiredArrays(composioTool.inputParameters);+ // Strict mode applies OpenAI's structured-output contract at every+ // depth: all properties required, closed objects, optional properties+ // widened to accept null instead of being dropped. Tools whose schema+ // strict mode cannot express are registered without strict mode.+ const execute = async (params: unknown, strictSource?: Record<string, unknown>) => {+ // Models occasionally emit tool input as a JSON string rather than an object (issue #2406).+ const normalized = normalizeToolArguments(params, composioTool.slug);+ // Under strict mode optional parameters are emitted as required-nullable,+ // so a null the tool's own schema does not accept means "omitted".+ return await executeTool(+ composioTool.slug,+ strictSource ? omitNullToolArguments(normalized, strictSource) : normalized+ );+ };++ if (this.strict) {+ const strict = toStrictJsonSchema(composioTool.inputParameters ?? EMPTY_OBJECT_SCHEMA);+ if (strict.unsupported.length === 0) {+ return createOpenAIAgentTool({+ name: composioTool.slug,+ description: composioTool.description ?? '',+ parameters: strict.schema as StrictToolParameters,+ strict: true,+ execute: params => execute(params, strict.source),+ });+ }+ const reasons = strict.unsupported+ .map(entry => `${entry.path || '<root>'}: ${entry.keyword} (${entry.detail})`)+ .join('; ');+ logger.warn(+ `OpenAIAgentsProvider: tool "${composioTool.slug}" is registered without strict mode because its schema cannot be expressed as strict structured outputs: ${reasons}`+ );+ }+return createOpenAIAgentTool({name: composioTool.slug,description: composioTool.description ?? '',@@ -135,13 +186,7 @@ export class OpenAIAgentsProvider extends BaseAgenticProvider<additionalProperties: true,},strict: false,- execute: async params => {- // Models occasionally emit tool input as a JSON string rather than an object (issue #2406).- return await executeTool(- composioTool.slug,- normalizeToolArguments(params, composioTool.slug)- );- },+ execute: params => execute(params),});}diff --git a/ts/packages/providers/openai-agents/test/openai-agents.test.ts b/ts/packages/providers/openai-agents/test/openai-agents.test.tsindex 49d8a5bfc..3c52f6982 100644--- a/ts/packages/providers/openai-agents/test/openai-agents.test.ts+++ b/ts/packages/providers/openai-agents/test/openai-agents.test.ts@@ -1,6 +1,6 @@import { describe, it, expect, vi, beforeEach } from 'vitest';import { OpenAIAgentsProvider } from '../src';-import { Tool } from '@composio/core';+import { Tool, ExecuteToolFn } from '@composio/core';import { tool as createOpenAIAgentTool } from '@openai/agents';// Define an interface for our mocked OpenAI Agent tool@@ -157,6 +157,96 @@ describe('OpenAIAgentsProvider', () => {});});+ describe('strict mode', () => {+ it('registers a strict schema with optional parameters kept as required-nullable', () => {+ const strictProvider = new OpenAIAgentsProvider({ strict: true });+ strictProvider._setExecuteToolFn(mockExecuteToolFn);+ const wrapped = strictProvider.wrapTool(+ {+ ...mockTool,+ inputParameters: {+ type: 'object',+ properties: {+ input: { type: 'string' },+ cfg: {+ type: 'object',+ properties: { url: { type: 'string' }, note: { type: 'string' } },+ required: ['url'],+ },+ },+ required: ['input'],+ },+ },+ mockExecuteToolFn as ExecuteToolFn+ ) as unknown as MockedOpenAIAgentTool;++ expect(wrapped.strict).toBe(true);+ expect(wrapped.parameters).toEqual({+ type: 'object',+ properties: {+ input: { type: 'string' },+ cfg: {+ type: ['object', 'null'],+ properties: { url: { type: 'string' }, note: { type: ['string', 'null'] } },+ required: ['url', 'note'],+ additionalProperties: false,+ },+ },+ required: ['input', 'cfg'],+ additionalProperties: false,+ });+ });++ it('registers tools strict mode cannot express without strict', () => {+ const strictProvider = new OpenAIAgentsProvider({ strict: true });+ const wrapped = strictProvider.wrapTool(+ {+ ...mockTool,+ inputParameters: {+ type: 'object',+ properties: { headers: { type: 'object', additionalProperties: { type: 'string' } } },+ required: ['headers'],+ },+ },+ mockExecuteToolFn as ExecuteToolFn+ ) as unknown as MockedOpenAIAgentTool;++ expect(wrapped.strict).toBe(false);+ expect(wrapped.parameters).toEqual({+ type: 'object',+ properties: { headers: { type: 'object', additionalProperties: { type: 'string' } } },+ required: ['headers'],+ additionalProperties: true,+ });+ });++ it('omits null arguments the tool schema rejects before executing under strict mode', async () => {+ const strictProvider = new OpenAIAgentsProvider({ strict: true });+ const wrapped = strictProvider.wrapTool(+ {+ ...mockTool,+ inputParameters: {+ type: 'object',+ properties: {+ input: { type: 'string' },+ label: { type: 'string' },+ clearable: { type: ['string', 'null'] },+ },+ required: ['input'],+ },+ },+ mockExecuteToolFn as ExecuteToolFn+ ) as unknown as MockedOpenAIAgentTool;++ await wrapped.execute({ input: 'x', label: null, clearable: null });++ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {+ input: 'x',+ clearable: null,+ });+ });+ });+describe('wrapTools', () => {it('should wrap multiple tools', () => {const anotherTool: Tool = {