experiment(confidence-engine): trace live openai schema boundary
This commit is contained in:
+57
-6
@@ -195,6 +195,55 @@ function projectOpenAIStrictSchema(schema, zodSchema, rootSchema, visited) {
|
||||
}
|
||||
}
|
||||
|
||||
function satisfiesOpenAIStrictSchema(schema, visited = new WeakSet()) {
|
||||
if (!schema || typeof schema !== "object" || visited.has(schema)) return true;
|
||||
visited.add(schema);
|
||||
const isObject = schema.type === "object" || schema.properties;
|
||||
if (isObject && schema.additionalProperties !== false) return false;
|
||||
if (schema.properties) {
|
||||
const propertyKeys = Object.keys(schema.properties);
|
||||
if (
|
||||
!Array.isArray(schema.required) ||
|
||||
schema.required.length !== propertyKeys.length ||
|
||||
!propertyKeys.every((key) => schema.required.includes(key))
|
||||
) return false;
|
||||
}
|
||||
return [
|
||||
schema.items,
|
||||
...Object.values(schema.properties ?? {}),
|
||||
...(schema.anyOf ?? []),
|
||||
...(schema.oneOf ?? []),
|
||||
...(schema.allOf ?? []),
|
||||
...Object.values(schema.$defs ?? schema.definitions ?? {}),
|
||||
].every((child) => satisfiesOpenAIStrictSchema(child, visited));
|
||||
}
|
||||
|
||||
function traceOpenAISchema({ model, format, schema }) {
|
||||
if (process.env.CONFIDENCE_ENGINE_EXPERIMENT_TRACE_OPENAI_SCHEMA !== "1") return;
|
||||
const serializedSchema = JSON.stringify(schema);
|
||||
const finalSchema = JSON.parse(serializedSchema);
|
||||
const properties = finalSchema.properties ?? {};
|
||||
const required = finalSchema.required ?? [];
|
||||
const relationships = properties.relationships;
|
||||
console.info("[confidence-engine][openai-schema-trace]", {
|
||||
model,
|
||||
formatType: format.type,
|
||||
formatName: format.name,
|
||||
strict: format.strict,
|
||||
rootProperties: Object.keys(properties),
|
||||
rootRequired: required,
|
||||
rootSetsEqual:
|
||||
required.length === Object.keys(properties).length &&
|
||||
Object.keys(properties).every((key) => required.includes(key)),
|
||||
relationshipsInProperties: Object.prototype.hasOwnProperty.call(properties, "relationships"),
|
||||
relationshipsInRequired: required.includes("relationships"),
|
||||
relationshipsItemType: relationships?.items?.type ?? null,
|
||||
relationshipsItemAdditionalProperties: relationships?.items?.additionalProperties ?? null,
|
||||
rootAdditionalProperties: finalSchema.additionalProperties ?? null,
|
||||
recursiveStrictInvariant: satisfiesOpenAIStrictSchema(finalSchema),
|
||||
});
|
||||
}
|
||||
|
||||
function schemaAllowsNull(schema, rootSchema) {
|
||||
const resolved = resolveSchema(schema, rootSchema);
|
||||
return (
|
||||
@@ -479,6 +528,13 @@ class OpenAIReconstructionProvider {
|
||||
? createOpenAIStrictSchema(outputSchema, null)
|
||||
: openAIReconstructionJsonSchema;
|
||||
|
||||
const format = {
|
||||
type: "json_schema",
|
||||
name: "reconstruction",
|
||||
strict: true,
|
||||
schema: structuredOutputSchema,
|
||||
};
|
||||
traceOpenAISchema({ model: modelName, format, schema: structuredOutputSchema });
|
||||
const response = await this.fetchImpl("https://api.openai.com/v1/responses", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -489,12 +545,7 @@ class OpenAIReconstructionProvider {
|
||||
model: modelName,
|
||||
input: prompt,
|
||||
text: {
|
||||
format: {
|
||||
type: "json_schema",
|
||||
name: "reconstruction",
|
||||
strict: true,
|
||||
schema: structuredOutputSchema,
|
||||
},
|
||||
format,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user