Skip to content

Commit 4eefa27

Browse files
committed
fix(openai-chat): root-only tool schema normalization, drop recursive null rewrite
The recursive rewrite in the PR head rewrote annotation values (default/const/ enum/examples), stripped nullable anyOf branches for every non-xAI provider, mishandled {type:null} encodings, and used a prototype-unsafe accumulator. Restore the documented contract: add type:"object" at the root only, leaving nested schema content untouched (parity with normalizeFunctionToolSchema).
1 parent 3806dac commit 4eefa27

2 files changed

Lines changed: 24 additions & 47 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -581,60 +581,22 @@ function emptyAssistantContent(provider: OcxProviderConfig): string | { type: "t
581581
}
582582

583583
/**
584-
* Deep-recursive schema fix: replaces `type:null` (JS null, JSON-Schema
585-
* "null" string type, or missing type) at every level of the schema tree.
584+
* Providers like Kimi and DeepSeek reject function parameter schemas whose root
585+
* `type` is missing or `null` — JSON Schema requires `"object"` at the root of
586+
* function parameters. Add `type: "object"` at the root while preserving
587+
* `oneOf`, `$defs`, and every other schema key.
586588
*
587-
* Several providers (Kimi, DeepSeek) reject JSON Schema `type:"null"` even
588-
* inside `anyOf`/`oneOf` composition arrays. Codex tools like
589-
* `codex_app__automation_update` ship nullable fields such as
590-
* `localEnvironmentConfigPath: {anyOf:[{type:"string"},{type:"null"}]}`,
591-
* causing 400 errors at the provider.
592-
*
593-
* This function recursively removes every `{type:"null"}` from arrays and
594-
* replaces standalone `type:null` with `type:"string"`, mirroring
595-
* `normalizeToolSchemas` in openai-responses.ts (but at all depths).
589+
* This mirrors `normalizeFunctionToolSchema` in openai-responses.ts, which
590+
* applies the same root-only normalization unconditionally on the responses
591+
* path. Nested schema content is intentionally left untouched.
596592
*/
597-
function fixNullTypes(obj: Record<string, unknown>): Record<string, unknown> {
598-
const out: Record<string, unknown> = {};
599-
for (const [key, value] of Object.entries(obj)) {
600-
if (key === "type" && (value === null || value === undefined || value === "null")) {
601-
// DeepSeek rejects `type:null` (JS null and the JSON-Schema `"null"` type)
602-
// at any level. Replace with `"string"` — safe for all LLM tool-call schemas.
603-
out[key] = value === "null" ? "string" : "object";
604-
} else if (value !== null && typeof value === "object" && !Array.isArray(value)) {
605-
out[key] = fixNullTypes(value as Record<string, unknown>);
606-
} else if (Array.isArray(value)) {
607-
// Filter out `{type:"null"}` entries from anyOf/oneOf/allOf arrays — nullable
608-
// is fine for JSON Schema but not for DeepSeek's stricter validator.
609-
const filtered = value.filter(v => {
610-
if (v !== null && typeof v === "object" && !Array.isArray(v)) {
611-
const t = (v as Record<string,unknown>).type;
612-
return t !== "null";
613-
}
614-
return true;
615-
});
616-
out[key] = filtered.map(v =>
617-
v !== null && typeof v === "object" && !Array.isArray(v)
618-
? fixNullTypes(v as Record<string, unknown>)
619-
: v
620-
);
621-
} else {
622-
out[key] = value;
623-
}
624-
}
625-
return out;
626-
}
627-
628593
function ensureRootObjectType(parameters: unknown): Record<string, unknown> {
629594
if (!parameters || typeof parameters !== "object" || Array.isArray(parameters)) {
630595
return { type: "object", properties: {} };
631596
}
632597
const obj = parameters as Record<string, unknown>;
633-
if (obj.type === "object") {
634-
// Deep-fix: even if root type is correct, nested null types break DeepSeek
635-
return fixNullTypes(obj) as Record<string, unknown>;
636-
}
637-
return fixNullTypes({ ...obj, type: "object" }) as Record<string, unknown>;
598+
if (obj.type === "object") return obj;
599+
return { ...obj, type: "object" };
638600
}
639601

640602
function expandXaiRootObjectSchemas(schema: unknown): Record<string, unknown>[] | undefined {

‎tests/xai-transport.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ describe("xAI auth-mode transport selection", () => {
123123
expect((JSON.parse(otherRequest.body) as { tools: Array<{ function: { parameters: unknown } }> }).tools[0].function.parameters).toEqual({ ...schema, type: "object" });
124124
});
125125

126+
test("non-xAI providers preserve nested nullable and annotation schema content", () => {
127+
const schema = {
128+
type: "object",
129+
properties: {
130+
path: { anyOf: [{ type: "string" }, { type: "null" }] },
131+
opts: { default: { type: "null" }, enum: [{ type: "null" }, "a"] },
132+
},
133+
};
134+
const request = createOpenAIChatAdapter({ ...provider("key"), baseUrl: "https://example.test/v1" }).buildRequest({
135+
...parsed(),
136+
context: { messages: [], tools: [{ name: "t", description: "T", parameters: schema }] },
137+
});
138+
expect((JSON.parse(request.body) as { tools: Array<{ function: { parameters: unknown } }> }).tools[0].function.parameters).toEqual(schema);
139+
});
140+
126141
test("omits an xAI tool whose root schema cannot be normalized safely", () => {
127142
const request = createOpenAIChatAdapter(provider("key")).buildRequest({
128143
...parsed(),

0 commit comments

Comments
 (0)