Skip to content

Commit db284da

Browse files
committed
feat(chat): forward response_format to routed openai-chat models
1 parent 0c81552 commit db284da

4 files changed

Lines changed: 68 additions & 15 deletions

File tree

‎docs/github-copilot-app.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ existing providers, routing, OAuth, and sidecars apply.
5050
The compatibility surface supports `model`, `messages`, `stream`, function tools
5151
and tool choice, token limits, temperature/top-p/stop, reasoning effort, parallel
5252
tool calls, prompt cache keys, metadata, and `response_format` on native Responses
53-
routes. Routed `openai-chat` models reject `response_format` with HTTP 400 because
54-
their structured-output support is not verified. Other Chat Completions fields,
55-
including penalties, `n`, and logprobs, are not currently supported.
53+
routes and routed `openai-chat` models (`json_object` and `json_schema` are
54+
forwarded as-is; a backend without structured-output support returns its own
55+
error). Other Chat Completions fields, including penalties, `n`, and logprobs,
56+
are not currently supported.
5657

5758
## Troubleshooting
5859

‎src/server/chat-completions.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ async function handleChatCompletionsWithBudget(
131131
} else if (internalBody.store === undefined) {
132132
internalBody.store = false;
133133
}
134-
if (route.provider.adapter === "openai-chat" && internalBody.text !== undefined) {
135-
if (logIds) addFinalRequestLog(logIds.requestId, logIds.start, logCtx, 400, { closeReason: "non_stream" });
136-
return chatCompletionsErrorResponse(400, "response_format is not supported for routed openai-chat models");
137-
}
138134
if (route.provider.adapter === "cursor" || route.provider.adapter === "kiro") {
139135
const raw = chatBody as Rec;
140136
const parts: string[] = [];

‎tests/chat-completions-endpoint.test.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ test("responsesSseToChatCompletionsSse delivers the first frame before a macrota
467467
await reader.cancel();
468468
});
469469

470-
test("POST /v1/chat/completions rejects response_format for routed openai-chat", async () => {
471-
const upstream = mockChatUpstream();
470+
test("POST /v1/chat/completions forwards response_format to routed openai-chat", async () => {
471+
const { server: upstream, captured } = mockChatUpstreamCapturing();
472472
saveConfig(mockConfig(`${upstream.url.toString().replace(/\/$/, "")}/v1`));
473473
const server = startServer(0);
474474
try {
@@ -477,15 +477,47 @@ test("POST /v1/chat/completions rejects response_format for routed openai-chat",
477477
headers: { "content-type": "application/json" },
478478
body: JSON.stringify({
479479
model: "mock/test-model",
480-
stream: false,
480+
stream: true,
481481
messages: [{ role: "user", content: "hi" }],
482-
response_format: { type: "json_object" },
482+
response_format: { type: "json_schema", json_schema: { name: "answer", schema: { type: "object" }, strict: true } },
483483
}),
484484
});
485-
expect(response.status).toBe(400);
486-
const json = await response.json() as { error: { message: string; type: string } };
487-
expect(json.error.message).toContain("response_format");
488-
expect(json.error.type).toBe("invalid_request_error");
485+
expect(response.status).toBe(200);
486+
await response.text();
487+
// Round trip: chat nested -> internal flat text.format -> re-nested on the wire, byte-identical.
488+
expect(captured.length).toBe(1);
489+
expect(captured[0]!.response_format).toEqual({
490+
type: "json_schema",
491+
json_schema: { name: "answer", schema: { type: "object" }, strict: true },
492+
});
493+
} finally {
494+
await server.stop(true);
495+
upstream.stop(true);
496+
}
497+
});
498+
499+
test("POST /v1/responses carries text.format onto the routed chat wire", async () => {
500+
const { server: upstream, captured } = mockChatUpstreamCapturing();
501+
saveConfig(mockConfig(`${upstream.url.toString().replace(/\/$/, "")}/v1`));
502+
const server = startServer(0);
503+
try {
504+
const response = await fetch(new URL("/v1/responses", server.url), {
505+
method: "POST",
506+
headers: { "content-type": "application/json" },
507+
body: JSON.stringify({
508+
model: "mock/test-model",
509+
stream: true,
510+
input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] }],
511+
text: { format: { type: "json_schema", name: "answer", schema: { type: "object" }, strict: true } },
512+
}),
513+
});
514+
expect(response.status).toBe(200);
515+
await response.text();
516+
expect(captured.length).toBe(1);
517+
expect(captured[0]!.response_format).toEqual({
518+
type: "json_schema",
519+
json_schema: { name: "answer", schema: { type: "object" }, strict: true },
520+
});
489521
} finally {
490522
await server.stop(true);
491523
upstream.stop(true);

‎tests/responses-compaction-routing.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,30 @@ describe("routed compaction for key-mode openai-responses (#422)", () => {
418418
expect(compactionItems.length).toBe(1);
419419
});
420420

421+
test("routed chat compaction drops the structured-output format", async () => {
422+
const bodies: Array<Record<string, unknown>> = [];
423+
globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {
424+
bodies.push(JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>);
425+
return jsonResponse({
426+
choices: [{ index: 0, message: { role: "assistant", content: "handoff summary" }, finish_reason: "stop" }],
427+
usage: { prompt_tokens: 10, completion_tokens: 5 },
428+
});
429+
}) as typeof fetch;
430+
431+
const res = await handleResponses(
432+
compactionRequest(baseCompactionBody({ text: { format: { type: "json_object" } } })),
433+
keyProviderConfig({ adapter: "openai-chat" }),
434+
{ model: "", provider: "" },
435+
);
436+
437+
expect(bodies.length).toBe(1);
438+
// The compaction turn is a prose summary; the caller's structured-output request must not
439+
// constrain it (core.ts routedCompaction deletes options.textFormat).
440+
expect(bodies[0]!.response_format).toBeUndefined();
441+
const json = await res.json() as { output?: Array<{ type?: string }> };
442+
expect((json.output ?? []).filter(item => item.type === "compaction").length).toBe(1);
443+
});
444+
421445
test("strips additional_tools even when top-level tools are absent", async () => {
422446
const bodies: Array<Record<string, unknown>> = [];
423447
globalThis.fetch = (async (_url: unknown, init?: RequestInit) => {

0 commit comments

Comments
 (0)