Skip to content

Commit ca443ca

Browse files
committed
fix(responses): keep untyped assistant messages inside their tool-call turn
ZCode sends assistant text as the Responses message shorthand ({role, content} with no type). isAssistantTurnItem only recognized a typed message, so the shorthand closed the assistant turn and backfillBareToolCallTurnReasoning inserted its placeholder after the text instead of at the head of the turn. DeepSeek's thinking mode rejects that ordering with "The reasoning_text in the thinking mode must be passed back to the API", and every continuation of the session fails the same way. Normalize the item type the way the Responses parser already does, so a shorthand with role=assistant belongs to the surrounding assistant turn. The placeholder then precedes the whole turn, matching the typed-message shape that already passes.
1 parent 98bd8c0 commit ca443ca

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎src/adapters/openai-responses/reasoning.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,15 @@ export function sanitizeReasoningInputContent(
174174

175175
/** Assistant-authored history items: what DeepSeek merges into one assistant message. */
176176
function isAssistantTurnItem(item: unknown): boolean {
177-
if (!isPlainObject(item) || typeof item.type !== "string") return false;
178-
if (item.type === "reasoning") return true;
179-
if (item.type === "message") return item.role === "assistant";
180-
return item.type.endsWith("_call");
177+
if (!isPlainObject(item)) return false;
178+
// Responses input accepts the message shorthand without `type`. ZCode sends its assistant
179+
// text that way, directly ahead of the same turn's function calls; reading only a typed
180+
// `message` would split the turn and place the placeholder backfill after the text, which
181+
// DeepSeek refuses with the same reasoning_text 400.
182+
const type = typeof item.type === "string" ? item.type : "role" in item ? "message" : undefined;
183+
if (type === "reasoning") return true;
184+
if (type === "message") return item.role === "assistant";
185+
return type?.endsWith("_call") === true;
181186
}
182187

183188
function isClientToolCallItem(item: unknown): boolean {

‎tests/providers/deepseek-bare-tool-call-replay.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe("backfillBareToolCallTurnReasoning (#7298)", () => {
3535
expect(backfillBareToolCallTurnReasoning(input)).toBe(input);
3636
});
3737

38+
test("a shorthand assistant message without `type` stays inside its tool-call turn", () => {
39+
const shorthand: Item = { role: "assistant", content: [{ type: "output_text", text: "checking" }] };
40+
const input = [user("go"), shorthand, call("c1"), output("c1")];
41+
expect(backfillBareToolCallTurnReasoning(input)).toEqual([
42+
user("go"), PLACEHOLDER, shorthand, call("c1"), output("c1"),
43+
]);
44+
});
45+
3846
test("custom tool calls count as tool calls", () => {
3947
const custom: Item = { type: "custom_tool_call", call_id: "p1", name: "apply_patch", input: "*** Begin" };
4048
const customOutput: Item = { type: "custom_tool_call_output", call_id: "p1", output: "ok" };
@@ -69,5 +77,15 @@ describe("DeepSeek Responses wire receives a replayable reasoning item for a for
6977
expect(wire.map(item => item.type)).toEqual(["message", "reasoning", "function_call", "function_call_output"]);
7078
expect(wire[1]!.content).toEqual([{ type: "reasoning_text", text: " " }]);
7179
});
80+
81+
test("ZCode's untyped assistant text keeps the placeholder ahead of the text", () => {
82+
const shorthand: Item = { role: "assistant", content: [{ type: "output_text", text: "let me check" }] };
83+
const wire = build([user("weather?"), shorthand, call("c1"), output("c1")], "deepseek");
84+
expect(wire.map(item => item.type)).toEqual([
85+
"message", "reasoning", undefined, "function_call", "function_call_output",
86+
]);
87+
expect(wire[2]).toMatchObject({ role: "assistant" });
88+
expect(wire[1]!.content).toEqual([{ type: "reasoning_text", text: " " }]);
89+
});
7290
});
7391

0 commit comments

Comments
 (0)