override fun processObject()

in prompt/prompt-executor/prompt-executor-clients/prompt-executor-openai-client-base/src/commonMain/kotlin/ai/koog/prompt/executor/clients/openai/base/structure/OpenAIStandardJsonSchemaGenerator.kt [77:114]


    override fun processObject(context: GenerationContext): JsonObject {
        val refObject = super.processObject(context).toMutableMap()

        if (context.descriptor !in context.currentDefPath) {
            val schema = context.processedTypeDefs.getValue(context.descriptor).toMutableMap()

            // OpenAI requires all existing properties to be present in "required" list
            schema[JsonSchemaConsts.Keys.REQUIRED] = JsonArray(
                schema.getValue(JsonSchemaConsts.Keys.PROPERTIES).jsonObject
                    .keys
                    .map { JsonPrimitive(it) }
            )

            context.processedTypeDefs[context.descriptor] = JsonObject(schema)
        }

        /**
         * OpenAI doesn't allow other fields at the same level with "$ref", such as "description"
         * Wrap it to "anyOf" with a single element to work around this limitation.
         */
        if (JsonSchemaConsts.Keys.REF in refObject && refObject.size > 1) {
            refObject[JsonSchemaConsts.Keys.ANY_OF] = buildJsonArray {
                add(
                    buildJsonObject {
                        put(JsonSchemaConsts.Keys.REF, refObject.getValue(JsonSchemaConsts.Keys.REF))
                    }
                )
            }

            refObject.remove(JsonSchemaConsts.Keys.REF)
        }

        if (JsonSchemaConsts.Keys.ONE_OF in refObject) {
            refObject.replaceOneOf()
        }

        return JsonObject(refObject)
    }