fix(confidence-engine): extract raw openai response text
This commit is contained in:
+31
-3
@@ -69,6 +69,32 @@ function recoverJson(raw) {
|
||||
throw new SyntaxError("Model output could not be parsed as JSON: " + result.slice(0, 300) + "...");
|
||||
}
|
||||
|
||||
function extractOpenAIResponseText(data) {
|
||||
if (typeof data.output_text === "string" && data.output_text.length > 0) {
|
||||
return data.output_text;
|
||||
}
|
||||
|
||||
const outputText = (data.output ?? []).flatMap((item) =>
|
||||
item?.type === "message"
|
||||
? (item.content ?? []).flatMap((part) =>
|
||||
part?.type === "output_text" && typeof part.text === "string"
|
||||
? [part.text]
|
||||
: [],
|
||||
)
|
||||
: [],
|
||||
);
|
||||
if (outputText.length > 0) return outputText.join("");
|
||||
|
||||
const outputTypes = (data.output ?? []).map((item) => item?.type ?? "unknown");
|
||||
const contentTypes = (data.output ?? []).flatMap((item) =>
|
||||
(item?.content ?? []).map((part) => part?.type ?? "unknown"),
|
||||
);
|
||||
const refusal = contentTypes.includes("refusal");
|
||||
throw new Error(
|
||||
`OpenAI Responses API returned no output_text (output types: ${outputTypes.join(",") || "none"}; content types: ${contentTypes.join(",") || "none"}; refusal: ${refusal})`,
|
||||
);
|
||||
}
|
||||
|
||||
let _chatSupported = null;
|
||||
const reconstructionJsonSchema = z.toJSONSchema(reconstructionV2Schema);
|
||||
const openAIReconstructionJsonSchema = createOpenAIStrictSchema(
|
||||
@@ -458,9 +484,11 @@ class OpenAIReconstructionProvider {
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const outputText = data.output_text;
|
||||
if (typeof outputText !== "string") {
|
||||
const error = new Error("OpenAI Responses API returned no output_text");
|
||||
let outputText;
|
||||
try {
|
||||
outputText = extractOpenAIResponseText(data);
|
||||
} catch (cause) {
|
||||
const error = new Error(cause.message);
|
||||
error.providerApiPath = "/v1/responses";
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user