public suspend fun parse()

in prompt/prompt-structure/src/commonMain/kotlin/ai/koog/prompt/structure/StructureFixingParser.kt [45:67]


    public suspend fun <T> parse(executor: PromptExecutor, structure: Structure<T, *>, content: String): T {
        try {
            return structure.parse(content)
        } catch (initialException: SerializationException) {
            var currentContent = content
            var currentException: SerializationException = initialException
            var attempt = 0

            while (++attempt <= retries) {
                logger.debug { "$attempt/$retries: Try to fix LLM structured response:\n$currentContent" }
                currentContent = executeFixStructure(executor, currentContent, structure, currentException)

                try {
                    return structure.parse(currentContent)
                } catch (e: SerializationException) {
                    logger.warn(e) { "Failed to parse structure from content:\n$currentContent" }
                    currentException = e
                }
            }

            throw LLMStructuredParsingError("Unable to parse structure after $retries retries", currentException)
        }
    }