fix: strip orphaned OpenAI reasoning blocks before responses API call

Wire downgradeOpenAIReasoningBlocks into the openai-responses stream
wrapper alongside the existing function-call pairing sanitizer. This
prevents 400 errors when conversation history contains reasoning items
without their required following content block (e.g. after compaction,
error recovery, or session restore).

The function already existed and was tested but was only called in the
Google provider path, not the OpenAI responses path.

Fixes #54534.
This commit is contained in:
Subash Natarajan
2026-03-27 18:21:09 +05:30
committed by Josh Lehman
parent beb2fded6d
commit 02c4851164

View File

@@ -72,6 +72,7 @@ import {
import type { EmbeddedContextFile } from "../../pi-embedded-helpers.js";
import {
downgradeOpenAIFunctionCallReasoningPairs,
downgradeOpenAIReasoningBlocks,
isCloudCodeAssistFormatError,
resolveBootstrapMaxChars,
resolveBootstrapPromptTruncationWarningMode,
@@ -1367,7 +1368,12 @@ export async function runEmbeddedAttempt(
if (!Array.isArray(messages)) {
return inner(model, context, options);
}
const sanitized = downgradeOpenAIFunctionCallReasoningPairs(messages as AgentMessage[]);
const pairSanitized = downgradeOpenAIFunctionCallReasoningPairs(
messages as AgentMessage[],
);
// Also strip orphaned reasoning blocks that lack a required following
// content item — OpenAI rejects these with a 400 error.
const sanitized = downgradeOpenAIReasoningBlocks(pairSanitized);
if (sanitized === messages) {
return inner(model, context, options);
}