Skip to content

Commit 6498691

Browse files
author
Weiyan
committed
fix(request): pin strict: false on function tools missing Responses strictness
OpenCode's AI SDK adapter omits `strict` when serializing function tools for the Responses API, and the backend treats an omitted flag as strict mode: every schema property becomes effectively required. Models then fabricate values for optional parameters — notably `sessionID` on OpenCode's subagent tool — and fresh subagent creation fails with "Subagent session not found" / "not a child of the current session". Fill in `strict: false` on function tools that don't set it, at the single choke point in transformRequestForCodex so both the native and legacy transform modes are covered. Explicitly set values are preserved; hosted (non-function) tools are untouched. This matches Codex CLI, which hardcodes strict: false on every function tool (codex-rs/tools/src/responses_api.rs), and OpenCode's own fix for its native providers (anomalyco/opencode#33392). Fixes #293
1 parent 9dd0012 commit 6498691

4 files changed

Lines changed: 231 additions & 1 deletion

File tree

‎lib/request/fetch-helpers.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
shapeBodyForModel,
4040
usesResponsesLite,
4141
} from "./helpers/responses-lite.js";
42+
import { ensureFunctionToolStrictFalse } from "./helpers/tool-utils.js";
4243
import { resolveClientIdentity } from "./helpers/client-identity.js";
4344
import { convertSseToJson, ensureContentType, readBoundedResponseText } from "./response-handler.js";
4445
import type { OAuthAuthDetails, UserConfig, RequestBody } from "../types.js";
@@ -1004,6 +1005,17 @@ export async function transformRequestForCodex(
10041005
const originalModel = body.model;
10051006
const requestTransformMode = options?.requestTransformMode ?? "legacy";
10061007

1008+
// OpenCode's AI SDK adapter omits `strict` on function tools, and the
1009+
// Responses backend treats an omitted flag as strict mode — making
1010+
// optional parameters effectively required, so models fabricate values
1011+
// (e.g. a sessionID for OpenCode's subagent tool) and fresh subagent
1012+
// creation fails. Codex CLI pins strict: false on every function tool
1013+
// for the same reason (codex-rs/tools/src/responses_api.rs). Applies to
1014+
// both transform modes; explicitly set values are preserved.
1015+
if (Array.isArray(body.tools)) {
1016+
body.tools = ensureFunctionToolStrictFalse(body.tools);
1017+
}
1018+
10071019
if (requestTransformMode === "native") {
10081020
logRequest(LOG_STAGES.BEFORE_TRANSFORM, {
10091021
url,

‎lib/request/helpers/tool-utils.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,46 @@ export function cleanupToolDefinitions(tools: unknown): unknown {
4545
});
4646
}
4747

48+
/**
49+
* Ensures every function tool carries an explicit `strict: false`.
50+
*
51+
* Why: OpenCode's AI SDK adapter omits `strict` when serializing function
52+
* tools for the Responses API, and the backend treats an omitted flag as
53+
* strict mode — making every schema property effectively required. Models
54+
* then fabricate values for optional parameters (notably `sessionID` on
55+
* OpenCode's `subagent` tool), which breaks fresh subagent creation with
56+
* "Session <parent-session-id> is not a child of the current session".
57+
* Codex CLI hardcodes `strict: false` on every function tool for the same
58+
* reason (codex-rs/tools/src/responses_api.rs).
59+
*
60+
* Handles both wire shapes:
61+
* - Responses API: { type: "function", name, parameters, strict }
62+
* - Chat Completions: { type: "function", function: { name, parameters, strict } }
63+
*
64+
* Explicitly set `strict` values are preserved.
65+
*
66+
* @param tools - Array of tool definitions
67+
* @returns The same array, with `strict: false` filled in where missing
68+
*/
69+
export function ensureFunctionToolStrictFalse(tools: unknown): unknown {
70+
if (!Array.isArray(tools)) return tools;
71+
72+
for (const tool of tools) {
73+
if (!tool || typeof tool !== "object") continue;
74+
const entry = tool as Record<string, unknown>;
75+
if (entry.type !== "function") continue;
76+
77+
const functionDef = entry.function as Record<string, unknown> | undefined;
78+
if (functionDef && typeof functionDef === "object") {
79+
if (functionDef.strict === undefined) functionDef.strict = false;
80+
} else if (entry.strict === undefined) {
81+
entry.strict = false;
82+
}
83+
}
84+
85+
return tools;
86+
}
87+
4888
/**
4989
* Recursively cleans up a JSON schema object
5090
*/

‎test/fetch-helpers.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,120 @@ describe('Fetch Helpers Module', () => {
18631863
expect(serialized.include).toContain('reasoning.encrypted_content');
18641864
});
18651865

1866+
it('pins strict: false on function tools missing the flag in native mode', async () => {
1867+
const { transformRequestForCodex } = await import('../lib/request/fetch-helpers.js');
1868+
// OpenCode's AI SDK adapter omits `strict`; the Responses backend treats
1869+
// that as strict mode, so optional params (e.g. subagent sessionID)
1870+
// become effectively required and models fabricate values.
1871+
const requestBody = {
1872+
model: 'gpt-5.5',
1873+
input: [{ type: 'message', role: 'user', content: 'Hello' }],
1874+
tools: [
1875+
{
1876+
type: 'function',
1877+
name: 'subagent',
1878+
description: 'Spawns an agent in a child session.',
1879+
parameters: {
1880+
type: 'object',
1881+
properties: {
1882+
agent: { type: 'string' },
1883+
sessionID: { type: 'string', pattern: '^ses' },
1884+
},
1885+
required: ['agent'],
1886+
},
1887+
},
1888+
{ type: 'function', name: 'explicit', parameters: {}, strict: true },
1889+
{ type: 'web_search_preview' },
1890+
],
1891+
};
1892+
1893+
const result = await transformRequestForCodex(
1894+
{ body: JSON.stringify(requestBody) },
1895+
'https://example.com',
1896+
{ global: {}, models: {} },
1897+
true,
1898+
undefined,
1899+
{ requestTransformMode: 'native' } as any,
1900+
);
1901+
1902+
const tools = result?.body.tools as any[];
1903+
expect(tools[0].strict).toBe(false);
1904+
// Optionality must survive — the backend must not see sessionID as required.
1905+
expect(tools[0].parameters.required).toEqual(['agent']);
1906+
// Explicit values are preserved.
1907+
expect(tools[1].strict).toBe(true);
1908+
// Hosted tools are untouched.
1909+
expect(tools[2]).not.toHaveProperty('strict');
1910+
1911+
const serialized = JSON.parse(result!.updatedInit.body as string);
1912+
expect(serialized.tools[0].strict).toBe(false);
1913+
});
1914+
1915+
it('pins strict: false inside additional_tools on the lite wire shape', async () => {
1916+
const { transformRequestForCodex } = await import('../lib/request/fetch-helpers.js');
1917+
const requestBody = {
1918+
model: 'gpt-5.6-sol',
1919+
input: [{ type: 'message', role: 'user', content: 'Hello' }],
1920+
tools: [
1921+
{
1922+
type: 'function',
1923+
name: 'subagent',
1924+
parameters: {
1925+
type: 'object',
1926+
properties: { agent: { type: 'string' } },
1927+
required: ['agent'],
1928+
},
1929+
},
1930+
],
1931+
};
1932+
1933+
const result = await transformRequestForCodex(
1934+
{ body: JSON.stringify(requestBody) },
1935+
'https://example.com',
1936+
{ global: {}, models: {} },
1937+
true,
1938+
undefined,
1939+
{ requestTransformMode: 'native' } as any,
1940+
);
1941+
1942+
const serialized = JSON.parse(result!.updatedInit.body as string);
1943+
const additionalTools = serialized.input.find(
1944+
(item: any) => item?.type === 'additional_tools',
1945+
);
1946+
const subagent = additionalTools?.tools?.find((t: any) => t.name === 'subagent');
1947+
expect(subagent?.strict).toBe(false);
1948+
});
1949+
1950+
it('pins strict: false on function tools in legacy mode', async () => {
1951+
const { transformRequestForCodex } = await import('../lib/request/fetch-helpers.js');
1952+
const requestBody = {
1953+
model: 'gpt-5.5',
1954+
input: [{ type: 'message', role: 'user', content: 'Hello' }],
1955+
tools: [
1956+
{
1957+
type: 'function',
1958+
name: 'subagent',
1959+
parameters: {
1960+
type: 'object',
1961+
properties: { agent: { type: 'string' } },
1962+
required: ['agent'],
1963+
},
1964+
},
1965+
],
1966+
};
1967+
1968+
const result = await transformRequestForCodex(
1969+
{ body: JSON.stringify(requestBody) },
1970+
'https://example.com',
1971+
{ global: {}, models: {} },
1972+
true,
1973+
);
1974+
1975+
const serialized = JSON.parse(result!.updatedInit.body as string);
1976+
const subagent = serialized.tools.find((t: any) => t.name === 'subagent');
1977+
expect(subagent?.strict).toBe(false);
1978+
expect(subagent?.parameters.required).toEqual(['agent']);
1979+
});
18661980
it('produces a complete Codex request when instructions come from the bundled fallback', async () => {
18671981
const { transformRequestForCodex } = await import('../lib/request/fetch-helpers.js');
18681982
const { BUNDLED_CODEX_INSTRUCTIONS } = await import(

‎test/tool-utils.test.ts‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { cleanupToolDefinitions } from "../lib/request/helpers/tool-utils.js";
2+
import { cleanupToolDefinitions, ensureFunctionToolStrictFalse } from "../lib/request/helpers/tool-utils.js";
33

44
describe("cleanupToolDefinitions", () => {
55
it("returns non-array input unchanged", () => {
@@ -581,3 +581,67 @@ describe("cleanupToolDefinitions", () => {
581581
expect(props.valid).toEqual({ type: "string" });
582582
});
583583
});
584+
585+
describe("ensureFunctionToolStrictFalse", () => {
586+
it("returns non-array input unchanged", () => {
587+
expect(ensureFunctionToolStrictFalse(null)).toBeNull();
588+
expect(ensureFunctionToolStrictFalse("string")).toBe("string");
589+
expect(ensureFunctionToolStrictFalse({})).toEqual({});
590+
});
591+
592+
it("sets strict: false on Responses-shaped function tools missing the flag", () => {
593+
const tools = [{
594+
type: "function",
595+
name: "subagent",
596+
description: "Spawns an agent in a child session.",
597+
parameters: {
598+
type: "object",
599+
properties: {
600+
agent: { type: "string" },
601+
sessionID: { type: "string", pattern: "^ses" },
602+
},
603+
required: ["agent"],
604+
},
605+
}];
606+
607+
const result = ensureFunctionToolStrictFalse(tools) as typeof tools;
608+
expect(result[0].strict).toBe(false);
609+
// Schema content must be untouched — optionality is preserved.
610+
expect(result[0].parameters.required).toEqual(["agent"]);
611+
});
612+
613+
it("sets strict: false on Chat-Completions-shaped function tools missing the flag", () => {
614+
const tools = [{
615+
type: "function",
616+
function: {
617+
name: "test",
618+
parameters: { type: "object", properties: {} },
619+
},
620+
}];
621+
622+
const result = ensureFunctionToolStrictFalse(tools) as typeof tools;
623+
expect(result[0].function.strict).toBe(false);
624+
});
625+
626+
it("preserves explicitly set strict values", () => {
627+
const tools = [
628+
{ type: "function", name: "a", parameters: {}, strict: true },
629+
{ type: "function", function: { name: "b", strict: true } },
630+
];
631+
632+
const result = ensureFunctionToolStrictFalse(tools) as typeof tools;
633+
expect(result[0].strict).toBe(true);
634+
expect(result[1].function.strict).toBe(true);
635+
});
636+
637+
it("leaves non-function tools untouched", () => {
638+
const tools = [
639+
{ type: "web_search_preview", search_context_size: "medium" },
640+
{ type: "file_search", vector_store_ids: ["vs_123"] },
641+
];
642+
643+
const result = ensureFunctionToolStrictFalse(tools) as typeof tools;
644+
expect(result[0]).not.toHaveProperty("strict");
645+
expect(result[1]).not.toHaveProperty("strict");
646+
});
647+
});

0 commit comments

Comments
 (0)