fix(confidence-engine): honor openai alternate output schema

This commit is contained in:
2026-09-07 10:18:41 +01:00
parent bb3082d633
commit 0eadef6e3b
3 changed files with 66 additions and 6 deletions
+13 -6
View File
@@ -160,22 +160,24 @@ function zodAcceptsNull(schema) {
function projectOpenAIStrictSchema(schema, zodSchema, rootSchema) {
const resolved = resolveSchema(schema, rootSchema);
const shape = zodObjectShape(zodSchema);
if (resolved?.properties && shape) {
if (resolved?.properties) {
if (shape) {
for (const [key, property] of Object.entries(resolved.properties)) {
const propertyZodSchema = shape[key];
if (propertyZodSchema?.isOptional?.() && !schemaAllowsNull(property, rootSchema)) {
resolved.properties[key] = { anyOf: [property, { type: "null" }] };
}
}
}
resolved.required = Object.keys(resolved.properties);
}
if (resolved?.items) {
projectOpenAIStrictSchema(resolved.items, zodArrayItem(zodSchema), rootSchema);
}
if (resolved?.properties && shape) {
if (resolved?.properties) {
for (const [key, property] of Object.entries(resolved.properties)) {
projectOpenAIStrictSchema(property, shape[key], rootSchema);
projectOpenAIStrictSchema(property, shape?.[key], rootSchema);
}
}
}
@@ -458,8 +460,11 @@ class OpenAIReconstructionProvider {
this.fetchImpl = fetchImpl;
}
async generateReconstruction(prompt, modelName = "gpt-5.6-terra") {
async generateReconstruction(prompt, modelName = "gpt-5.6-terra", outputSchema) {
if (!this.apiKey) throw new Error("OPENAI_API_KEY is not set");
const structuredOutputSchema = outputSchema
? createOpenAIStrictSchema(outputSchema, null)
: openAIReconstructionJsonSchema;
const response = await this.fetchImpl("https://api.openai.com/v1/responses", {
method: "POST",
@@ -475,7 +480,7 @@ class OpenAIReconstructionProvider {
type: "json_schema",
name: "reconstruction",
strict: true,
schema: openAIReconstructionJsonSchema,
schema: structuredOutputSchema,
},
},
}),
@@ -501,7 +506,9 @@ class OpenAIReconstructionProvider {
}
return {
response: normaliseOpenAITransportResponse(recoverJson(outputText)),
response: outputSchema
? recoverJson(outputText)
: normaliseOpenAITransportResponse(recoverJson(outputText)),
providerApiPath: "/v1/responses",
};
}