Skip to contents

Schema-first variant of llm_fn(). It enables structured output on the config, calls the model via call_llm_broadcast(), parses JSON, and optionally validates.

Usage

llm_fn_structured(
  x,
  prompt,
  .config,
  .system_prompt = NULL,
  ...,
  .schema = NULL,
  .fields = NULL,
  .local_only = FALSE,
  .validate_local = TRUE,
  .rows_per_prompt = 1L,
  .rowpack_payload = c("user", "system"),
  .rowpack_recovery = c("halve_recursive", "halve_once", "singletons", "retry_same",
    "none")
)

Arguments

x

A character vector or a data.frame/tibble.

prompt

A glue template string. With a data-frame you may reference columns ({col}); with a vector the placeholder is {x}.

.config

An llm_config object.

.system_prompt

Optional system message (character scalar).

...

Passed unchanged to call_llm_broadcast() (e.g. tries, progress, verbose).

.schema

Optional JSON Schema list; if NULL, only JSON object is enforced.

.fields

Optional fields to hoist from parsed JSON (supports nested paths).

.local_only

If TRUE, do not send schema to the provider (parse/validate locally).

.validate_local

If TRUE and .schema provided, validate locally.

.rows_per_prompt

Integer scalar, or Inf. Number of input elements packed into a single generative request. The default, 1, sends one request per element (the historical behaviour). When greater than 1, elements are grouped and transmitted in one call wrapped in numbered <row_1>...</row_1> tags (see Row batching below); Inf sends all elements in a single call. Ignored for embedding configurations, which use get_batched_embeddings() instead.

.rowpack_payload

One of c("user","system"). Channel to which the <row_i> data block is appended when batching. The imperative instruction is always placed in the system message; this argument controls only where the data goes. The default, "user", keeps a static system prompt cacheable.

.rowpack_recovery

How to handle rows that a batched call leaves unresolved (dropped, malformed, or truncated). One of:

"halve_recursive"

(default) re-issue the unresolved rows at half the batch size, recursing down to single rows.

"halve_once"

re-issue at half the batch size exactly once, then give up on any still-unresolved rows.

"singletons"

re-issue each unresolved row on its own.

"retry_same"

re-issue the failed batch once at the same size.

"none"

do not recover; unresolved rows are returned as NA.

Recovery is bounded by an internal call budget so it always terminates.

Value

A tibble: the call_llm_broadcast() diagnostics plus the parsed structured columns (structured_ok, structured_data, one column per hoisted field, and structured_valid/structured_error when .schema is validated locally).