executeStructured

Executes a prompt with structured output, enhancing it with schema instructions or native structured output parameter, and parses the response into the defined structure.

Note: While many language models advertise support for structured output via JSON schema, the actual level of support varies between models and even between versions of the same model. Some models may produce malformed outputs or deviate from the schema in subtle ways, especially with complex structures like polymorphic types.

Return

kotlin.Result with parsed StructuredResponse or error.

Parameters

prompt

The prompt to be executed.

model

LLM to execute requests.

config

A configuration defining structures and behavior.


suspend fun <T> PromptExecutor.executeStructured(prompt: Prompt, model: LLModel, serializer: KSerializer<T>, examples: List<T> = emptyList(), fixingParser: StructureFixingParser? = null): Result<StructuredResponse<T>>(source)

Executes a prompt with structured output, enhancing it with schema instructions or native structured output parameter, and parses the response into the defined structure.

This is a simple version of the full executeStructured. Unlike the full version, it does not require specifying struct definitions and structured output modes manually. It attempts to find the best approach to provide a structured output based on the defined model capabilities.

For example, it chooses which JSON schema to use (simple or full) and with which mode (native or manual).

Return

kotlin.Result with parsed StructuredResponse or error.

Parameters

prompt

The prompt to be executed.

model

LLM to execute requests.

serializer

Serializer for the requested structure type.

examples

Optional list of examples in case manual mode will be used. These examples might help the model to understand the format better.

fixingParser

Optional parser that handles malformed responses by using an auxiliary LLM to intelligently fix parsing errors. When specified, parsing errors trigger additional LLM calls with error context to attempt correction of the structure format.


inline suspend fun <T> PromptExecutor.executeStructured(prompt: Prompt, model: LLModel, examples: List<T> = emptyList(), fixingParser: StructureFixingParser? = null): Result<StructuredResponse<T>>(source)

Executes a prompt with structured output, enhancing it with schema instructions or native structured output parameter, and parses the response into the defined structure.

This is a simple version of the full executeStructured. Unlike the full version, it does not require specifying struct definitions and structured output modes manually. It attempts to find the best approach to provide a structured output based on the defined model capabilities.

For example, it chooses which JSON schema to use (simple or full) and with which mode (native or manual).

Return

kotlin.Result with parsed StructuredResponse or error.

Parameters

T

The structure to request.

prompt

The prompt to be executed.

model

LLM to execute requests.

examples

Optional list of examples in case manual mode will be used. These examples might help the model to understand the format better.

fixingParser

Optional parser that handles malformed responses by using an auxiliary LLM to intelligently fix parsing errors. When specified, parsing errors trigger additional LLM calls with error context to attempt correction of the structure format.