Skip to content

Commit 396cf1c

Browse files
committed
fix(openai-responses): restore hosted image generation in every stripped container
Stripping walked all `additional_tools` containers; restoration targeted only the first stripped index. A request carrying two containers, each declaring an empty `image_gen` namespace and no hosted declaration anywhere, ended with the first container repaired and the second left with an empty tool list — no image capability at all. Track every stripped index and restore each one. Found by the automated review on #924 and driven red first: the second container came back as `[]` before the fix. Full suite 7575 pass, 0 fail.
1 parent 57f0ec5 commit 396cf1c

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,14 @@ function preferConfiguredHostedTools(
678678
}
679679

680680
let input = body.input;
681-
let strippedAdditionalToolsIndex = -1;
681+
const strippedAdditionalToolsIndices = new Set<number>();
682682
if (Array.isArray(body.input)) {
683683
let nestedChanged = false;
684684
const mappedInput = body.input.map((item, index) => {
685685
if (!isPlainObject(item) || item.type !== "additional_tools" || !Array.isArray(item.tools)) return item;
686686
const nestedTools = stripGroup(item.tools);
687687
if (nestedTools === item.tools) return item;
688-
if (strippedAdditionalToolsIndex === -1) strippedAdditionalToolsIndex = index;
688+
strippedAdditionalToolsIndices.add(index);
689689
nestedChanged = true;
690690
return { ...item, tools: nestedTools };
691691
});
@@ -704,11 +704,14 @@ function preferConfiguredHostedTools(
704704
|| (Array.isArray(input) && input.some(item => isPlainObject(item)
705705
&& item.type === "additional_tools"
706706
&& hasHostedImageGenTool(item.tools)));
707-
if ((strippedTopLevelImageGenTool || strippedAdditionalToolsIndex >= 0) && !hasHostedImageGenDeclaration) {
707+
if ((strippedTopLevelImageGenTool || strippedAdditionalToolsIndices.size > 0) && !hasHostedImageGenDeclaration) {
708708
if (strippedTopLevelImageGenTool && Array.isArray(tools)) {
709709
tools = [...tools, { type: HOSTED_IMAGE_GENERATION_TOOL }];
710-
} else if (strippedAdditionalToolsIndex >= 0 && Array.isArray(input)) {
711-
input = input.map((item, index) => index === strippedAdditionalToolsIndex
710+
} else if (strippedAdditionalToolsIndices.size > 0 && Array.isArray(input)) {
711+
// Restore in EVERY container we stripped, not just the first: a request carrying
712+
// two `additional_tools` groups would otherwise leave the later ones with no image
713+
// capability at all. Raised by the automated review on #924.
714+
input = input.map((item, index) => strippedAdditionalToolsIndices.has(index)
712715
&& isPlainObject(item)
713716
&& Array.isArray(item.tools)
714717
? { ...item, tools: [...item.tools, { type: HOSTED_IMAGE_GENERATION_TOOL }] }

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ describe("OpenAI Responses hosted-tool name conflicts", () => {
11221122
});
11231123

11241124
test("an inherited Object.prototype key is not read as a preference", () => {
1125-
// `provider.modelPreferHostedTools?.[modelId]` walks the prototype chain, so a
1125+
// `provider.modelPreferHostedTools?.[modelId]` walked the prototype chain, so a
11261126
// routed model literally named `constructor` or `toString` yielded a function
11271127
// and threw on `.includes` before the request was ever dispatched.
11281128
const adapter = createResponsesPassthroughAdapter({
@@ -1147,6 +1147,45 @@ describe("OpenAI Responses hosted-tool name conflicts", () => {
11471147
}
11481148
});
11491149

1150+
test("every stripped additional_tools container gets hosted image generation restored", () => {
1151+
// Stripping runs over all containers, but restoration originally targeted only the
1152+
// first stripped index, so a second container lost its image capability entirely.
1153+
const adapter = createResponsesPassthroughAdapter({
1154+
...keyedProvider,
1155+
modelPreferHostedTools: { "provider-image-model": ["image_generation"] },
1156+
});
1157+
const request = adapter.buildRequest({
1158+
modelId: "provider-image-model",
1159+
context: { messages: [] },
1160+
stream: true,
1161+
options: {},
1162+
_rawBody: {
1163+
model: "provider-image-model",
1164+
input: [
1165+
{
1166+
type: "additional_tools",
1167+
role: "developer",
1168+
tools: [{ type: "namespace", name: "image_gen", tools: [] }, { type: "web_search" }],
1169+
},
1170+
{
1171+
type: "additional_tools",
1172+
role: "developer",
1173+
tools: [{ type: "namespace", name: "image_gen", tools: [] }],
1174+
},
1175+
],
1176+
},
1177+
}, meta);
1178+
const body = JSON.parse(request.body) as {
1179+
input: Array<{ type: string; tools?: Array<{ type: string }> }>;
1180+
};
1181+
const containers = body.input.filter(item => item.type === "additional_tools");
1182+
1183+
expect(containers).toHaveLength(2);
1184+
for (const container of containers) {
1185+
expect(container.tools).toContainEqual({ type: "image_generation" });
1186+
}
1187+
});
1188+
11501189
test("configured model rewrites a custom image-gen selector", () => {
11511190
const adapter = createResponsesPassthroughAdapter({
11521191
...keyedProvider,

0 commit comments

Comments
 (0)