executeStructured

suspend fun <T> PromptExecutor.executeStructured(prompt: Prompt, mainModel: LLModel, structure: StructuredData<T>, retries: Int = 1, fixingModel: LLModel = OpenAIModels.Chat.GPT4o): Result<StructuredResponse<T>>(source)

Executes a prompt and ensures the response is properly structured by applying automatic output coercion.

This method enhances structured output parsing reliability by:

  1. Injecting structured output instructions into the original prompt

  2. Executing the enriched prompt to receive a raw response

  3. Using a separate LLM call to parse/coerce the response if direct parsing fails

Unlike execute(prompt, structure) which simply attempts to parse the raw response and fails if the format doesn't match exactly, this method actively works to transform unstructured or malformed outputs into the expected structure through additional LLM processing.

Return

A StructuredResponse containing both parsed structure and raw text

Parameters

structure

The structured data definition with schema and parsing logic

prompt

The prompt to execute

mainModel

The main model to execute prompt

retries

Number of parsing attempts before giving up

fixingModel

LLM used for output coercion (transforming malformed outputs)

Throws

if parsing fails after all retries