diff --git a/README.md b/README.md index 30ab93e8..b4c99ca2 100644 --- a/README.md +++ b/README.md @@ -399,12 +399,12 @@ The catalog is cached per account scope and mode, so changing a filter never ref `tool.toJsonSchema()` is the schema the server served, verbatim — `$schema`, `$defs`, `$ref`, `title`, `additionalProperties`, top-level `oneOf`/`anyOf`/`allOf` and every nested constraint — with the served `required` list. It is a fresh copy each call. The framework adapters start from it and change only what the provider would otherwise reject: -| Adapter | Adjustment | -| ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `toJsonSchema()` | None: lossless. | -| `toOpenAI()`, `toAnthropic()`, `toClaudeAgentSdkTool()` | A top-level `oneOf`/`anyOf`/`allOf` is folded into the root: every branch's properties become root properties, and an `allOf` branch's `required` is merged in. OpenAI also rejects a top-level `enum`/`not`, so those are dropped. | -| `toOpenAIResponses()` | As above. With `strict` (the default) the root also gets `additionalProperties: false`. OpenAI's strict mode additionally requires every property to be `required` and every nested object closed, which the SDK does not rewrite, so pass `{ strict: false }` for schemas with optional fields. | -| `toAISDK()` | As `toOpenAI()`, plus `additionalProperties: false` on the root. | +| Adapter | Adjustment | +| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `toJsonSchema()` | None: lossless. | +| `toOpenAI()`, `toAnthropic()`, `toClaudeAgentSdkTool()` | A top-level `oneOf`/`anyOf`/`allOf` is folded into the root: every branch's properties become root properties, and an `allOf` branch's `required` is merged in. OpenAI also rejects a top-level `enum`/`not`, so those are dropped. | +| `toOpenAIResponses()` | As above, not strict by default. `{ strict: true }` also gives the root `additionalProperties: false`, but OpenAI's strict mode additionally requires every property to be `required` and every nested object closed, which the SDK does not rewrite, so use it only for schemas that already meet that. | +| `toAISDK()` | As `toOpenAI()`, plus `additionalProperties: false` on the root. | ### Headers diff --git a/src/tool.test.ts b/src/tool.test.ts index 4b03243e..9a6a46de 100644 --- a/src/tool.test.ts +++ b/src/tool.test.ts @@ -139,11 +139,11 @@ describe('BaseTool', () => { const adapters: Array<[string, () => Promise>]> = [ ['toOpenAI', async () => present(tool.toOpenAI().function.parameters)], ['toAnthropic', async () => tool.toAnthropic().input_schema], - ['toOpenAIResponses (strict)', async () => present(tool.toOpenAIResponses().parameters)], [ - 'toOpenAIResponses (non-strict)', - async () => present(tool.toOpenAIResponses({ strict: false }).parameters), + 'toOpenAIResponses (strict)', + async () => present(tool.toOpenAIResponses({ strict: true }).parameters), ], + ['toOpenAIResponses (non-strict)', async () => present(tool.toOpenAIResponses().parameters)], [ 'toAISDK', async () => { @@ -196,9 +196,9 @@ describe('BaseTool', () => { expect(open.toJsonSchema().additionalProperties).toBe(true); expect(open.toOpenAI().function.parameters?.additionalProperties).toBe(true); expect(open.toAnthropic().input_schema.additionalProperties).toBe(true); - expect(open.toOpenAIResponses({ strict: false }).parameters?.additionalProperties).toBe(true); + expect(open.toOpenAIResponses().parameters?.additionalProperties).toBe(true); // Strict Responses and the AI SDK close the root: documented, and pinned here. - expect(open.toOpenAIResponses().parameters?.additionalProperties).toBe(false); + expect(open.toOpenAIResponses({ strict: true }).parameters?.additionalProperties).toBe(false); const aiTools = await open.toAISDK({ executable: false }); const aiTool = aiTools.open; assert.isDefined(aiTool); @@ -206,7 +206,7 @@ describe('BaseTool', () => { }); it('closes the root in strict mode without rewriting anything nested', () => { - const parameters = present(tool.toOpenAIResponses().parameters); + const parameters = present(tool.toOpenAIResponses({ strict: true }).parameters); expect(parameters.additionalProperties).toBe(false); expect(parameters).toHaveProperty(['properties', 'address'], richSchema.properties.address); }); @@ -231,13 +231,14 @@ describe('BaseTool', () => { }); }); - it('converts to the OpenAI Responses shape, strict by default', () => { - const strict = simpleTool().toOpenAIResponses(); - expect(strict).toMatchObject({ type: 'function', name: 'test_tool', strict: true }); - - const lax = simpleTool().toOpenAIResponses({ strict: false }); - expect(lax.strict).toBe(false); + it('converts to the OpenAI Responses shape, not strict by default', () => { + const lax = simpleTool().toOpenAIResponses(); + expect(lax).toMatchObject({ type: 'function', name: 'test_tool', strict: false }); expect(lax.parameters).not.toHaveProperty('additionalProperties'); + + const strict = simpleTool().toOpenAIResponses({ strict: true }); + expect(strict.strict).toBe(true); + expect(strict.parameters?.additionalProperties).toBe(false); }); it('builds an AI SDK tool whose schema the ai package accepts', async () => { @@ -737,9 +738,10 @@ describe('Tools', () => { ]); expect(tools.toOpenAI().map((tool) => tool.function.name)).toEqual(['a', 'b']); expect(tools.toAnthropic().map((tool) => tool.name)).toEqual(['a', 'b']); - expect(tools.toOpenAIResponses({ strict: false }).map((tool) => tool.strict)).toEqual([ - false, - false, + expect(tools.toOpenAIResponses().map((tool) => tool.strict)).toEqual([false, false]); + expect(tools.toOpenAIResponses({ strict: true }).map((tool) => tool.strict)).toEqual([ + true, + true, ]); expect(Object.keys(await tools.toAISDK())).toEqual(['a', 'b']); }); diff --git a/src/tool.ts b/src/tool.ts index 868c2916..9861b513 100644 --- a/src/tool.ts +++ b/src/tool.ts @@ -179,18 +179,15 @@ export class BaseTool { /** * Convert the tool to OpenAI Responses API format. * - * With `strict` (the default) the root schema is closed with `additionalProperties: false`, - * replacing whatever the server served there. The SDK does not rewrite anything nested: - * OpenAI's strict mode additionally requires every property to be listed in `required` and - * every nested object to be closed, so a served schema with optional fields is rejected by - * OpenAI in strict mode. Pass `{ strict: false }` for those — the served schema then goes - * through unchanged apart from the top-level composition fold. + * Not strict by default. `{ strict: true }` closes only the root with `additionalProperties: + * false`: OpenAI's strict mode also needs every property `required` and every nested object + * closed, which the SDK does not rewrite, so it rejects strict tools with optional fields. * * @see https://platform.openai.com/docs/api-reference/responses * @see https://platform.openai.com/docs/guides/structured-outputs#supported-schemas */ toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool { - const { strict = true } = options; + const { strict = false } = options; return { type: 'function', name: this.name, @@ -822,7 +819,8 @@ export class Tools implements Iterable { } /** - * Convert all tools to OpenAI Responses API format + * Convert all tools to OpenAI Responses API format, not strict by default: see + * {@link BaseTool.toOpenAIResponses} for what `{ strict: true }` does. * @see https://platform.openai.com/docs/api-reference/responses */ toOpenAIResponses(options: { strict?: boolean } = {}): OpenAIResponsesFunctionTool[] { diff --git a/tests/examples/openai-responses-integration.test.ts b/tests/examples/openai-responses-integration.test.ts index bc2845c4..97c1db24 100644 --- a/tests/examples/openai-responses-integration.test.ts +++ b/tests/examples/openai-responses-integration.test.ts @@ -33,6 +33,8 @@ describe('openai-responses-integration example e2e', () => { // Verify tools are in OpenAI Responses format expect(Array.isArray(openAIResponsesTools)).toBe(true); expect(openAIResponsesTools.length).toBeGreaterThan(0); + // Strict mode makes OpenAI reject served schemas with optional fields + expect(openAIResponsesTools.map((tool) => tool.strict)).not.toContain(true); // Initialize OpenAI client const openai = new OpenAI();