fix(ai): Fix OpenAI Responses provider multi-turn conversation support

- Collect complete output items during streaming instead of building blocks incrementally
- Handle reasoning summary parts with proper newline separation
- Support refusal content in message outputs
- Preserve full reasoning items and message IDs for multi-turn resubmission
- Emit proper streaming events for text and thinking deltas
This commit is contained in:
Mario Zechner 2025-08-31 22:11:08 +02:00
parent a72e6d08d4
commit ee4c131873
3 changed files with 171 additions and 132 deletions

View file

@ -150,19 +150,20 @@ export class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOptions> {
} }
// Append to text block // Append to text block
if (currentBlock.type === "text") { if (currentBlock.type === "text") {
currentBlock.text += choice.delta.content;
options?.onEvent?.({ options?.onEvent?.({
type: "text_delta", type: "text_delta",
content: currentBlock.text, content: currentBlock.text,
delta: choice.delta.content, delta: choice.delta.content,
}); });
currentBlock.text += choice.delta.content;
} }
} }
// Handle reasoning_content field // Handle reasoning_content field
if ( if (
(choice.delta as any).reasoning_content !== null && (choice.delta as any).reasoning_content !== null &&
(choice.delta as any).reasoning_content !== undefined (choice.delta as any).reasoning_content !== undefined &&
(choice.delta as any).reasoning_content.length > 0
) { ) {
// Check if we need to switch to thinking block // Check if we need to switch to thinking block
if (!currentBlock || currentBlock.type !== "thinking") { if (!currentBlock || currentBlock.type !== "thinking") {
@ -184,13 +185,17 @@ export class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOptions> {
// Append to thinking block // Append to thinking block
if (currentBlock.type === "thinking") { if (currentBlock.type === "thinking") {
const delta = (choice.delta as any).reasoning_content; const delta = (choice.delta as any).reasoning_content;
options?.onEvent?.({ type: "thinking_delta", content: currentBlock.thinking, delta });
currentBlock.thinking += delta; currentBlock.thinking += delta;
options?.onEvent?.({ type: "thinking_delta", content: currentBlock.thinking, delta });
} }
} }
// Handle reasoning field // Handle reasoning field
if ((choice.delta as any).reasoning !== null && (choice.delta as any).reasoning !== undefined) { if (
(choice.delta as any).reasoning !== null &&
(choice.delta as any).reasoning !== undefined &&
(choice.delta as any).reasoning.length > 0
) {
// Check if we need to switch to thinking block // Check if we need to switch to thinking block
if (!currentBlock || currentBlock.type !== "thinking") { if (!currentBlock || currentBlock.type !== "thinking") {
// Save current block if exists // Save current block if exists
@ -211,8 +216,8 @@ export class OpenAICompletionsLLM implements LLM<OpenAICompletionsLLMOptions> {
// Append to thinking block // Append to thinking block
if (currentBlock.type === "thinking") { if (currentBlock.type === "thinking") {
const delta = (choice.delta as any).reasoning; const delta = (choice.delta as any).reasoning;
options?.onEvent?.({ type: "thinking_delta", content: currentBlock.thinking, delta });
currentBlock.thinking += delta; currentBlock.thinking += delta;
options?.onEvent?.({ type: "thinking_delta", content: currentBlock.thinking, delta });
} }
} }

View file

@ -2,13 +2,14 @@ import OpenAI from "openai";
import type { import type {
Tool as OpenAITool, Tool as OpenAITool,
ResponseCreateParamsStreaming, ResponseCreateParamsStreaming,
ResponseFunctionToolCall,
ResponseInput, ResponseInput,
ResponseInputContent, ResponseInputContent,
ResponseInputImage, ResponseInputImage,
ResponseInputText, ResponseInputText,
ResponseOutputMessage,
ResponseReasoningItem, ResponseReasoningItem,
} from "openai/resources/responses/responses.js"; } from "openai/resources/responses/responses.js";
import type { ResponseOutputMessage } from "openai/resources/responses/responses.mjs";
import type { import type {
AssistantMessage, AssistantMessage,
Context, Context,
@ -17,6 +18,7 @@ import type {
Message, Message,
Model, Model,
StopReason, StopReason,
TextContent,
Tool, Tool,
ToolCall, ToolCall,
Usage, Usage,
@ -83,11 +85,9 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
signal: options?.signal, signal: options?.signal,
}); });
let content = ""; const outputItems: (ResponseReasoningItem | ResponseOutputMessage | ResponseFunctionToolCall)[] = []; // any for function_call items
let contentSignature = ""; let currentTextAccum = ""; // For delta accumulation
let thinking = ""; let currentThinkingAccum = ""; // For delta accumulation
const toolCalls: ToolCall[] = [];
const reasoningItems: ResponseReasoningItem[] = [];
let usage: Usage = { let usage: Usage = {
input: 0, input: 0,
output: 0, output: 0,
@ -98,41 +98,61 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
let stopReason: StopReason = "stop"; let stopReason: StopReason = "stop";
for await (const event of stream) { for await (const event of stream) {
// Handle reasoning summary for models that support it // Handle output item start
if (event.type === "response.reasoning_summary_text.delta") { if (event.type === "response.output_item.added") {
const item = event.item;
if (item.type === "reasoning") {
options?.onEvent?.({ type: "thinking_start" });
currentThinkingAccum = "";
} else if (item.type === "message") {
options?.onEvent?.({ type: "text_start" });
currentTextAccum = "";
}
}
// Handle reasoning summary deltas
else if (event.type === "response.reasoning_summary_text.delta") {
const delta = event.delta; const delta = event.delta;
thinking += delta; currentThinkingAccum += delta;
options?.onThinking?.(delta, false); options?.onEvent?.({ type: "thinking_delta", content: currentThinkingAccum, delta });
} else if (event.type === "response.reasoning_summary_text.done") {
if (event.text) {
thinking = event.text;
} }
options?.onThinking?.("", true); // Add a new line between summary parts (hack...)
else if (event.type === "response.reasoning_summary_part.done") {
currentThinkingAccum += "\n\n";
options?.onEvent?.({ type: "thinking_delta", content: currentThinkingAccum, delta: "\n\n" });
} }
// Handle main text output // Handle text output deltas
else if (event.type === "response.output_text.delta") { else if (event.type === "response.output_text.delta") {
const delta = event.delta; const delta = event.delta;
content += delta; currentTextAccum += delta;
options?.onText?.(delta, false); options?.onEvent?.({ type: "text_delta", content: currentTextAccum, delta });
} else if (event.type === "response.output_text.done") {
if (event.text) {
content = event.text;
} }
options?.onText?.("", true); // Handle refusal output deltas
contentSignature = event.item_id; else if (event.type === "response.refusal.delta") {
const delta = event.delta;
currentTextAccum += delta;
options?.onEvent?.({ type: "text_delta", content: currentTextAccum, delta });
} }
// Handle function calls // Handle output item completion
else if (event.type === "response.output_item.done") { else if (event.type === "response.output_item.done") {
const item = event.item; const item = event.item;
if (item?.type === "function_call") {
toolCalls.push({ if (item.type === "reasoning") {
const thinkingContent = item.summary?.map((s: any) => s.text).join("\n\n") || "";
options?.onEvent?.({ type: "thinking_end", content: thinkingContent });
outputItems.push(item);
} else if (item.type === "message") {
const textContent = item.content.map((c) => (c.type === "output_text" ? c.text : c.refusal)).join("");
options?.onEvent?.({ type: "text_end", content: textContent });
outputItems.push(item);
} else if (item.type === "function_call") {
const toolCall: ToolCall = {
type: "toolCall",
id: item.call_id + "|" + item.id, id: item.call_id + "|" + item.id,
name: item.name, name: item.name,
arguments: JSON.parse(item.arguments), arguments: JSON.parse(item.arguments),
}); };
} options?.onEvent?.({ type: "toolCall", toolCall });
if (item.type === "reasoning") { outputItems.push(item);
reasoningItems.push(item);
} }
} }
// Handle completion // Handle completion
@ -150,38 +170,68 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
// Map status to stop reason // Map status to stop reason
stopReason = this.mapStopReason(response?.status); stopReason = this.mapStopReason(response?.status);
if (toolCalls.length > 0 && stopReason === "stop") {
stopReason = "toolUse";
}
} }
// Handle errors // Handle errors
else if (event.type === "error") { else if (event.type === "error") {
return { const errorOutput = {
role: "assistant", role: "assistant",
content: [],
provider: this.modelInfo.provider, provider: this.modelInfo.provider,
model: this.modelInfo.id, model: this.modelInfo.id,
usage, usage,
stopReason: "error", stopReason: "error",
error: `Code ${event.code}: ${event.message}` || "Unknown error", error: `Code ${event.code}: ${event.message}` || "Unknown error",
}; } satisfies AssistantMessage;
options?.onEvent?.({ type: "error", error: errorOutput.error || "Unknown error" });
return errorOutput;
} }
} }
return { // Convert output items to blocks
const blocks: AssistantMessage["content"] = [];
for (const item of outputItems) {
if (item.type === "reasoning") {
blocks.push({
type: "thinking",
thinking: item.summary?.map((s: any) => s.text).join("\n\n") || "",
thinkingSignature: JSON.stringify(item), // Full item for resubmission
});
} else if (item.type === "message") {
blocks.push({
type: "text",
text: item.content.map((c) => (c.type === "output_text" ? c.text : c.refusal)).join(""),
textSignature: item.id, // ID for resubmission
});
} else if (item.type === "function_call") {
blocks.push({
type: "toolCall",
id: item.call_id + "|" + item.id,
name: item.name,
arguments: JSON.parse(item.arguments),
});
}
}
// Check if we have tool calls for stop reason
if (blocks.some((b) => b.type === "toolCall") && stopReason === "stop") {
stopReason = "toolUse";
}
const output = {
role: "assistant", role: "assistant",
content: content || undefined, content: blocks,
contentSignature: contentSignature || undefined,
thinking: thinking || undefined,
thinkingSignature: JSON.stringify(reasoningItems) || undefined,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
provider: this.modelInfo.provider, provider: this.modelInfo.provider,
model: this.modelInfo.id, model: this.modelInfo.id,
usage, usage,
stopReason, stopReason,
}; } satisfies AssistantMessage;
options?.onEvent?.({ type: "done", reason: output.stopReason, message: output });
return output;
} catch (error) { } catch (error) {
return { const output = {
role: "assistant", role: "assistant",
content: [],
provider: this.modelInfo.provider, provider: this.modelInfo.provider,
model: this.modelInfo.id, model: this.modelInfo.id,
usage: { usage: {
@ -193,7 +243,9 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
}, },
stopReason: "error", stopReason: "error",
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : String(error),
}; } satisfies AssistantMessage;
options?.onEvent?.({ type: "error", error: output.error || "Unknown error" });
return output;
} }
} }
@ -241,13 +293,27 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
}); });
} }
} else if (msg.role === "assistant") { } else if (msg.role === "assistant") {
// Assistant messages - add both content and tool calls to output // Process content blocks in order
const output: ResponseInput = []; const output: ResponseInput = [];
if (msg.thinkingSignature) {
output.push(...JSON.parse(msg.thinkingSignature)); for (const block of msg.content) {
if (block.type === "thinking") {
// Push the full reasoning item(s) from signature
if (block.thinkingSignature) {
const reasoningItem = JSON.parse(block.thinkingSignature);
output.push(reasoningItem);
} }
if (msg.toolCalls) { } else if (block.type === "text") {
for (const toolCall of msg.toolCalls) { const textBlock = block as TextContent;
output.push({
type: "message",
role: "assistant",
content: [{ type: "output_text", text: textBlock.text, annotations: [] }],
status: "completed",
id: textBlock.textSignature || "msg_" + Math.random().toString(36).substring(2, 15),
} satisfies ResponseOutputMessage);
} else if (block.type === "toolCall") {
const toolCall = block as ToolCall;
output.push({ output.push({
type: "function_call", type: "function_call",
id: toolCall.id.split("|")[1], // Extract original ID id: toolCall.id.split("|")[1], // Extract original ID
@ -257,15 +323,7 @@ export class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
}); });
} }
} }
if (msg.content) {
output.push({
type: "message",
role: "assistant",
content: [{ type: "output_text", text: msg.content, annotations: [] }],
status: "completed",
id: msg.contentSignature || "msg_" + Math.random().toString(36).substring(2, 15),
} satisfies ResponseOutputMessage);
}
// Add all output items to input // Add all output items to input
input.push(...output); input.push(...output);
} else if (msg.role === "toolResult") { } else if (msg.role === "toolResult") {

View file

@ -203,7 +203,7 @@ async function multiTurn<T extends LLMOptions>(llm: LLM<T>, thinkingOptions: T)
// Process content blocks // Process content blocks
for (const block of response.content) { for (const block of response.content) {
if (block.type === "text") { if (block.type === "text") {
allTextContent += block.text + " "; allTextContent += block.text;
} else if (block.type === "thinking") { } else if (block.type === "thinking") {
hasSeenThinking = true; hasSeenThinking = true;
} else if (block.type === "toolCall") { } else if (block.type === "toolCall") {
@ -250,7 +250,7 @@ async function multiTurn<T extends LLMOptions>(llm: LLM<T>, thinkingOptions: T)
} }
describe("AI Providers E2E Tests", () => { describe("AI Providers E2E Tests", () => {
describe.skipIf(!process.env.GEMINI_API_KEY)("Gemini Provider", () => { describe.skipIf(!process.env.GEMINI_API_KEY)("Gemini Provider (gemini-2.5-flash)", () => {
let llm: GoogleLLM; let llm: GoogleLLM;
beforeAll(() => { beforeAll(() => {
@ -282,7 +282,7 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider", () => { describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider (gpt-4o-mini)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
beforeAll(() => { beforeAll(() => {
@ -306,7 +306,7 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider", () => { describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider (gpt-5-mini)", () => {
let llm: OpenAIResponsesLLM; let llm: OpenAIResponsesLLM;
beforeAll(() => { beforeAll(() => {
@ -338,7 +338,7 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider", () => { describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider (claude-sonnet-4-0)", () => {
let llm: AnthropicLLM; let llm: AnthropicLLM;
beforeAll(() => { beforeAll(() => {
@ -370,7 +370,35 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.XAI_API_KEY)("xAI Provider (via OpenAI Completions)", () => { describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic Provider (Haiku 3.5)", () => {
let llm: AnthropicLLM;
beforeAll(() => {
llm = createLLM("anthropic", "claude-3-5-haiku-latest");
});
it("should complete basic text generation", async () => {
await basicTextGeneration(llm);
});
it("should handle tool calling", async () => {
await handleToolCall(llm);
});
it("should handle streaming", async () => {
await handleStreaming(llm);
});
it("should handle multi-turn with thinking and tools", async () => {
await multiTurn(llm, {thinking: {enabled: true}});
});
it("should handle image input", async () => {
await handleImage(llm);
});
});
describe.skipIf(!process.env.XAI_API_KEY)("xAI Provider (grok-code-fast-1 via OpenAI Completions)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
beforeAll(() => { beforeAll(() => {
@ -398,7 +426,7 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.GROQ_API_KEY)("Groq Provider (via OpenAI Completions)", () => { describe.skipIf(!process.env.GROQ_API_KEY)("Groq Provider (gpt-oss-20b via OpenAI Completions)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
beforeAll(() => { beforeAll(() => {
@ -426,7 +454,7 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.CEREBRAS_API_KEY)("Cerebras Provider (via OpenAI Completions)", () => { describe.skipIf(!process.env.CEREBRAS_API_KEY)("Cerebras Provider (gpt-oss-120b via OpenAI Completions)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
beforeAll(() => { beforeAll(() => {
@ -454,11 +482,11 @@ describe("AI Providers E2E Tests", () => {
}); });
}); });
describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter Provider (via OpenAI Completions)", () => { describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter Provider (glm-4.5v via OpenAI Completions)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
beforeAll(() => { beforeAll(() => {
llm = new OpenAICompletionsLLM(getModel("openrouter", "z-ai/glm-4.5")!, process.env.OPENROUTER_API_KEY!);; llm = new OpenAICompletionsLLM(getModel("openrouter", "z-ai/glm-4.5v")!, process.env.OPENROUTER_API_KEY!);;
}); });
it("should complete basic text generation", async () => { it("should complete basic text generation", async () => {
@ -480,6 +508,10 @@ describe("AI Providers E2E Tests", () => {
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
await multiTurn(llm, {reasoningEffort: "medium"}); await multiTurn(llm, {reasoningEffort: "medium"});
}); });
it("should handle image input", async () => {
await handleImage(llm);
});
}); });
// Check if ollama is installed // Check if ollama is installed
@ -491,7 +523,7 @@ describe("AI Providers E2E Tests", () => {
ollamaInstalled = false; ollamaInstalled = false;
} }
describe.skipIf(!ollamaInstalled)("Ollama Provider (via OpenAI Completions)", () => { describe.skipIf(!ollamaInstalled)("Ollama Provider (gpt-oss-20b via OpenAI Completions)", () => {
let llm: OpenAICompletionsLLM; let llm: OpenAICompletionsLLM;
let ollamaProcess: ChildProcess | null = null; let ollamaProcess: ChildProcess | null = null;
@ -579,60 +611,4 @@ describe("AI Providers E2E Tests", () => {
await multiTurn(llm, {reasoningEffort: "medium"}); await multiTurn(llm, {reasoningEffort: "medium"});
}); });
}); });
describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter Provider (GLM 4.5)", () => {
let llm: OpenAICompletionsLLM;
beforeAll(() => {
llm = createLLM("openrouter", "z-ai/glm-4.5", process.env.OPENROUTER_API_KEY!);
});
it("should complete basic text generation", async () => {
await basicTextGeneration(llm);
});
it("should handle tool calling", async () => {
await handleToolCall(llm);
});
it("should handle streaming", async () => {
await handleStreaming(llm);
});
it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"});
});
it("should handle multi-turn with thinking and tools", async () => {
await multiTurn(llm, {reasoningEffort: "medium"});
});
});
describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic Provider (Haiku 3.5)", () => {
let llm: AnthropicLLM;
beforeAll(() => {
llm = createLLM("anthropic", "claude-3-5-haiku-latest");
});
it("should complete basic text generation", async () => {
await basicTextGeneration(llm);
});
it("should handle tool calling", async () => {
await handleToolCall(llm);
});
it("should handle streaming", async () => {
await handleStreaming(llm);
});
it("should handle multi-turn with thinking and tools", async () => {
await multiTurn(llm, {thinking: {enabled: true}});
});
it("should handle image input", async () => {
await handleImage(llm);
});
});
}); });