Skip to content

Commit f3c7630

Browse files
author
hinux
committed
fix(openai-chat): deep-recursive null-type removal for all non-xAI providers
Expand the root-type fix to recursively remove `type:null` (JS null, JSON-Schema "null" string, and {type:"null"} inside anyOf/oneOf arrays) at every nesting level. Codex tools like `codex_app__automation_update` ship nullable fields such as: localEnvironmentConfigPath: { anyOf: [{ type: "string" }, { type: "null" }] } DeepSeek rejects all forms of `type:null` — at root and nested — while the original Kimi fix only enforced `type:"object"` at the root. Changes from v1: - Add `fixNullTypes()` — recursive walk that replaces standalone `type:null`/`type:"null"` with `type:"string"` and filters `{type:"null"}` entries out of anyOf/oneOf/allOf arrays. - Call `fixNullTypes` from `ensureRootObjectType` so every non-xAI provider gets both root and deep normalization.
1 parent a7cb02c commit f3c7630

1 file changed

Lines changed: 48 additions & 9 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,22 +408,60 @@ function emptyAssistantContent(provider: OcxProviderConfig): string | { type: "t
408408
}
409409

410410
/**
411-
* Several providers (Kimi, DeepSeek) require function.parameters.type to be
412-
* exactly "object" at the root and reject `type:null` or missing type. Codex
413-
* tools with oneOf/anyOf schemas may omit the root type, causing 400 errors.
411+
* Deep-recursive schema fix: replaces `type:null` (JS null, JSON-Schema
412+
* "null" string type, or missing type) at every level of the schema tree.
414413
*
415-
* Safe to apply unconditionally: JSON Schema for function parameters at the
416-
* root MUST be an object — this is a no-op when type is already "object",
417-
* and fixes every non-conforming case. Mirror of normalizeToolSchemas in
418-
* openai-responses.ts.
414+
* Several providers (Kimi, DeepSeek) reject JSON Schema `type:"null"` even
415+
* inside `anyOf`/`oneOf` composition arrays. Codex tools like
416+
* `codex_app__automation_update` ship nullable fields such as
417+
* `localEnvironmentConfigPath: {anyOf:[{type:"string"},{type:"null"}]}`,
418+
* causing 400 errors at the provider.
419+
*
420+
* This function recursively removes every `{type:"null"}` from arrays and
421+
* replaces standalone `type:null` with `type:"string"`, mirroring
422+
* `normalizeToolSchemas` in openai-responses.ts (but at all depths).
419423
*/
424+
function fixNullTypes(obj: Record<string, unknown>): Record<string, unknown> {
425+
const out: Record<string, unknown> = {};
426+
for (const [key, value] of Object.entries(obj)) {
427+
if (key === "type" && (value === null || value === undefined || value === "null")) {
428+
// DeepSeek rejects `type:null` (JS null and the JSON-Schema `"null"` type)
429+
// at any level. Replace with `"string"` — safe for all LLM tool-call schemas.
430+
out[key] = value === "null" ? "string" : "object";
431+
} else if (value !== null && typeof value === "object" && !Array.isArray(value)) {
432+
out[key] = fixNullTypes(value as Record<string, unknown>);
433+
} else if (Array.isArray(value)) {
434+
// Filter out `{type:"null"}` entries from anyOf/oneOf/allOf arrays — nullable
435+
// is fine for JSON Schema but not for DeepSeek's stricter validator.
436+
const filtered = value.filter(v => {
437+
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
438+
const t = (v as Record<string,unknown>).type;
439+
return t !== "null";
440+
}
441+
return true;
442+
});
443+
out[key] = filtered.map(v =>
444+
v !== null && typeof v === "object" && !Array.isArray(v)
445+
? fixNullTypes(v as Record<string, unknown>)
446+
: v
447+
);
448+
} else {
449+
out[key] = value;
450+
}
451+
}
452+
return out;
453+
}
454+
420455
function ensureRootObjectType(parameters: unknown): Record<string, unknown> {
421456
if (!parameters || typeof parameters !== "object" || Array.isArray(parameters)) {
422457
return { type: "object", properties: {} };
423458
}
424459
const obj = parameters as Record<string, unknown>;
425-
if (obj.type === "object") return obj;
426-
return { ...obj, type: "object" };
460+
if (obj.type === "object") {
461+
// Deep-fix: even if root type is correct, nested null types break DeepSeek
462+
return fixNullTypes(obj) as Record<string, unknown>;
463+
}
464+
return fixNullTypes({ ...obj, type: "object" }) as Record<string, unknown>;
427465
}
428466

429467
function expandXaiRootObjectSchemas(schema: unknown): Record<string, unknown>[] | undefined {
@@ -472,6 +510,7 @@ function toolsToChatFormat(parsed: OcxParsedRequest, provider: OcxProviderConfig
472510
const parameters = xaiTarget
473511
? normalizeXaiToolParameters(t.parameters)
474512
: ensureRootObjectType(t.parameters);
513+
475514
if (parameters === undefined) return [];
476515
return [{
477516
type: "function",

0 commit comments

Comments
 (0)