Skip to content

Commit 18e072c

Browse files
authored
Merge pull request #2103 from hyohyeon08/fix/xai-claude-tool-schema
fix(xai): preserve Claude tools with root $schema
2 parents 7a2d13a + 8ef4f4f commit 18e072c

2 files changed

Lines changed: 161 additions & 2 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,11 @@ function normalizeXaiToolParameters(parameters: unknown): Record<string, unknown
11561156
if (!isXaiObjectSchema(parameters)) return undefined;
11571157
const resolved = resolveXaiSchemaRefs(parameters, parameters);
11581158
if (!isXaiObjectSchema(resolved)) return undefined;
1159-
const variants = expandXaiRootObjectSchemas(resolved);
1159+
1160+
const normalizedRoot = { ...resolved };
1161+
delete normalizedRoot.$schema;
1162+
1163+
const variants = expandXaiRootObjectSchemas(normalizedRoot);
11601164
if (!variants) return undefined;
11611165
if (variants.length === 1) {
11621166
return xaiVariantIsConcreteObject(variants[0]) ? variants[0] : undefined;
@@ -1166,7 +1170,7 @@ function normalizeXaiToolParameters(parameters: unknown): Record<string, unknown
11661170
if (!additionalProperties.ok) return undefined;
11671171
if (!xaiPropertyMergeIsLossless(variants)) return undefined;
11681172

1169-
const metadata = Object.fromEntries(Object.entries(resolved).filter(([key]) => key !== "oneOf" && key !== "anyOf" && key !== "type"));
1173+
const metadata = Object.fromEntries(Object.entries(normalizedRoot).filter(([key]) => key !== "oneOf" && key !== "anyOf" && key !== "type"));
11701174
delete metadata.properties;
11711175
delete metadata.required;
11721176
delete metadata.additionalProperties;
@@ -1180,6 +1184,7 @@ function normalizeXaiToolParameters(parameters: unknown): Record<string, unknown
11801184
propertyValues.set(name, values);
11811185
}
11821186
}
1187+
11831188
const properties = Object.fromEntries(
11841189
[...propertyValues].map(([name, values]) => [name, mergeXaiPropertySchemas(values)]),
11851190
);

‎tests/xai-tool-schema.test.ts‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { describe, expect, test } from "bun:test";
2+
import {
3+
createOpenAIChatAdapter as createOpenAIChatAdapterProduction,
4+
} from "../src/adapters/openai-chat";
5+
import type {
6+
OcxParsedRequest,
7+
OcxTool,
8+
} from "../src/types";
9+
import { withTestTranslatorBudget } from "./helpers/translator-budget";
10+
11+
const createOpenAIChatAdapter = (
12+
...args: Parameters<typeof createOpenAIChatAdapterProduction>
13+
) =>
14+
withTestTranslatorBudget(
15+
createOpenAIChatAdapterProduction(...args),
16+
);
17+
18+
function parsedRequest(
19+
tool: OcxTool,
20+
): OcxParsedRequest {
21+
return {
22+
modelId: "grok-4.6",
23+
context: {
24+
messages: [
25+
{
26+
role: "user",
27+
content: "run the command",
28+
timestamp: 0,
29+
},
30+
],
31+
tools: [tool],
32+
},
33+
stream: true,
34+
options: {},
35+
};
36+
}
37+
38+
function xaiAdapter() {
39+
return createOpenAIChatAdapter({
40+
adapter: "openai-chat",
41+
baseUrl: "https://cli-chat-proxy.grok.com/v1",
42+
apiKey: "k",
43+
});
44+
}
45+
46+
describe("xAI Grok CLI tool schema normalization", () => {
47+
test("keeps Claude Code tools with a root $schema", async () => {
48+
const tool: OcxTool = {
49+
name: "Bash",
50+
description: "Execute a shell command",
51+
parameters: {
52+
$schema: "https://json-schema.org/draft/2020-12/schema",
53+
type: "object",
54+
properties: {
55+
command: {
56+
type: "string",
57+
},
58+
},
59+
required: ["command"],
60+
additionalProperties: false,
61+
},
62+
};
63+
64+
const request = await xaiAdapter().buildRequest(
65+
parsedRequest(tool),
66+
);
67+
68+
const body = JSON.parse(request.body) as {
69+
tools?: Array<{
70+
type: string;
71+
function: {
72+
name: string;
73+
parameters: Record<string, unknown>;
74+
};
75+
}>;
76+
};
77+
78+
expect(body.tools).toHaveLength(1);
79+
expect(body.tools?.[0]?.function.name).toBe("Bash");
80+
expect(body.tools?.[0]?.function.parameters).toEqual({
81+
type: "object",
82+
properties: {
83+
command: {
84+
type: "string",
85+
},
86+
},
87+
required: ["command"],
88+
additionalProperties: false,
89+
});
90+
expect(
91+
body.tools?.[0]?.function.parameters,
92+
).not.toHaveProperty("$schema");
93+
});
94+
95+
test("does not reintroduce $schema when flattening a root union", async () => {
96+
const tool: OcxTool = {
97+
name: "Bash",
98+
description: "Execute a shell command",
99+
parameters: {
100+
$schema: "https://json-schema.org/draft/2020-12/schema",
101+
oneOf: [
102+
{
103+
type: "object",
104+
properties: {
105+
command: {
106+
type: "string",
107+
},
108+
},
109+
required: ["command"],
110+
additionalProperties: false,
111+
},
112+
{
113+
type: "object",
114+
properties: {
115+
command: {
116+
type: "string",
117+
minLength: 1,
118+
},
119+
},
120+
required: ["command"],
121+
additionalProperties: false,
122+
},
123+
],
124+
},
125+
};
126+
127+
const request = await xaiAdapter().buildRequest(
128+
parsedRequest(tool),
129+
);
130+
131+
const body = JSON.parse(request.body) as {
132+
tools?: Array<{
133+
function: {
134+
parameters: Record<string, unknown>;
135+
};
136+
}>;
137+
};
138+
139+
expect(body.tools).toHaveLength(1);
140+
expect(body.tools?.[0]?.function.parameters).toEqual({
141+
type: "object",
142+
properties: {
143+
command: {
144+
anyOf: [
145+
{ type: "string" },
146+
{ type: "string", minLength: 1 },
147+
],
148+
},
149+
},
150+
required: ["command"],
151+
additionalProperties: false,
152+
});
153+
});
154+
});

0 commit comments

Comments
 (0)