Skip to content

Commit 4386da7

Browse files
fix: normalize tool function schemas to always have root type: "object"
Some tool definitions (e.g. Codex `codex_app__automation_update`) arrive without a `parameters` field or with `parameters: null`. When serialized, that produces a JSON Schema whose root `type` is `null` (or missing). Strict validators — such as DeepSeek — reject such schemas with HTTP 400. Changes in two places to cover both forwarding paths: 1. `src/responses/parser.ts` (`buildTools`): Coerce every function tool's `parameters` to a valid object-schema root. Covers the adapter translation path (Anthropic, Gemini, Cursor, etc.). 2. `src/adapters/openai-responses.ts` (`createResponsesPassthroughAdapter`): Add `normalizeToolSchemas()` that fixes top-level `tools` and Responses Lite `additional_tools[].tools` before serialization. Required because the passthrough path (e.g. DeepSeek with `wire_api=responses`) forwards `_rawBody` verbatim and bypasses the parsed `context.tools`. Fixes the 400 error: "Invalid schema for function: schema must be a JSON Schema of 'type: \"object\"', got 'type: null'."
1 parent e449521 commit 4386da7

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,61 @@ function isPlainObject(v: unknown): v is Record<string, unknown> {
354354
return !!v && typeof v === "object" && !Array.isArray(v);
355355
}
356356

357+
/**
358+
* Ensure every function tool exposes a valid object JSON Schema at its
359+
* `parameters` root. Strict validators (e.g. DeepSeek) reject schemas whose
360+
* root `type` is `null`, missing, or non-`"object"` with HTTP 400.
361+
*
362+
* Codex tools such as `codex_app__automation_update` arrive without a
363+
* `parameters` field, which would otherwise serialize to a schema lacking the
364+
* root `type`. Walks both top-level `tools` and Responses Lite
365+
* `additional_tools[].tools`. Namespace grouping is flattened by earlier strip
366+
* steps, so any function tool seen here is already top-level.
367+
*/
368+
function normalizeToolSchemas(body: unknown): unknown {
369+
if (!isPlainObject(body)) return body;
370+
371+
const fixFunctionParams = (tool: unknown): unknown => {
372+
if (!isPlainObject(tool) || tool.type !== "function") return tool;
373+
const params = isPlainObject(tool.parameters) ? { ...tool.parameters } : {};
374+
let changed = false;
375+
if (params.type !== "object") { params.type = "object"; changed = true; }
376+
if (!isPlainObject(params.properties)) { params.properties = {}; changed = true; }
377+
return changed ? { ...tool, parameters: params } : tool;
378+
};
379+
380+
let changed = false;
381+
382+
// Top-level tools array.
383+
if (Array.isArray(body.tools)) {
384+
const tools = body.tools.map((t) => {
385+
const fixed = fixFunctionParams(t);
386+
if (fixed !== t) changed = true;
387+
return fixed;
388+
});
389+
if (changed) body = { ...body, tools };
390+
}
391+
392+
// Responses Lite `additional_tools` items embed their own tools arrays.
393+
if (Array.isArray(body.input)) {
394+
let inputChanged = false;
395+
const input = body.input.map((item) => {
396+
if (!isPlainObject(item) || item.type !== "additional_tools" || !Array.isArray(item.tools)) return item;
397+
let innerChanged = false;
398+
const innerTools = item.tools.map((t) => {
399+
const fixed = fixFunctionParams(t);
400+
if (fixed !== t) innerChanged = true;
401+
return fixed;
402+
});
403+
if (innerChanged) { inputChanged = true; return { ...item, tools: innerTools }; }
404+
return item;
405+
});
406+
if (inputChanged) { changed = true; body = { ...body, input }; }
407+
}
408+
409+
return body;
410+
}
411+
357412
const MAX_RESPONSES_CALL_ID_LENGTH = 64;
358413
const REPAIRED_CALL_ID_PREFIX = "call_ocx_";
359414
const REPAIRED_CALL_ID_DIGEST_LENGTH = MAX_RESPONSES_CALL_ID_LENGTH - REPAIRED_CALL_ID_PREFIX.length;
@@ -938,7 +993,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
938993
if (parsed._compactionRequest === true && !isCanonicalOpenAiForwardProvider(provider)) {
939994
outBody = buildRoutedCompactionBody(outBody);
940995
}
941-
const sanitizedBody = stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody)))))));
996+
const sanitizedBody = normalizeToolSchemas(stripSparkCompatibility(stripUnsupportedReasoningParams(stripItemIdsWhenUnstored(stripInvalidItemIds(stripUnsupportedHostedTools(sanitizeReasoningInputContent(scrubOcxCompactionItems(outBody))))))));
942997
return {
943998
url,
944999
method: "POST",

‎src/responses/parser.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,24 @@ function allowedToolName(tool: unknown): string | undefined {
137137
function buildTools(tools: unknown[] | undefined): OcxTool[] | undefined {
138138
if (!tools) return undefined;
139139
const out: OcxTool[] = [];
140+
// Some tool definitions (e.g. Codex `codex_app__automation_update`) arrive
141+
// without a `parameters` field or with `parameters: null`. When serialized,
142+
// that produces a JSON Schema whose root `type` is `null` (or missing).
143+
// Strict validators reject such schemas with HTTP 400
144+
// ("schema must be a JSON Schema of 'type: \"object\"', got 'type: null'").
145+
// Coerce every function tool's `parameters` to a valid object-schema root
146+
// so all downstream adapters pass through cleanly.
147+
const normalizeParameters = (raw: unknown): Record<string, unknown> => {
148+
const p = isObj(raw) ? { ...raw } : {};
149+
if (p.type !== "object") p.type = "object";
150+
if (!isObj(p.properties)) p.properties = {};
151+
return p;
152+
};
140153
const pushFn = (t: Record<string, unknown>, namespace?: string) => {
141154
const tool: OcxTool = {
142155
name: t.name as string,
143156
description: (t.description as string) ?? "",
144-
parameters: (t.parameters ?? {}) as Record<string, unknown>,
157+
parameters: normalizeParameters(t.parameters),
145158
};
146159
if (t.strict !== undefined) tool.strict = t.strict as boolean;
147160
if (namespace) tool.namespace = namespace;

0 commit comments

Comments
 (0)