Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 17 additions & 15 deletions src/tool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ describe('BaseTool', () => {
const adapters: Array<[string, () => Promise<Record<string, unknown>>]> = [
['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 () => {
Expand Down Expand Up @@ -196,17 +196,17 @@ 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);
expect(aiSdkJsonSchema(aiTool.inputSchema).additionalProperties).toBe(false);
});

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);
});
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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']);
});
Expand Down
14 changes: 6 additions & 8 deletions src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -822,7 +819,8 @@ export class Tools implements Iterable<BaseTool> {
}

/**
* 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[] {
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/openai-responses-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading