Soft structured variant of llm_mutate(). It asks the model to return simple
XML-like tags, then parses those tags into columns.
Usage
llm_mutate_tags(
.data,
output,
prompt = NULL,
.messages = NULL,
.config,
.system_prompt = NULL,
.before = NULL,
.after = NULL,
.tags,
.fields = NULL,
.rows_per_prompt = 1L,
.rowpack_payload = c("user", "system"),
.rowpack_recovery = c("halve_recursive", "halve_once", "singletons", "retry_same",
"none"),
...
)Arguments
- .data
A data.frame / tibble.
- output
Unquoted name that becomes the new column (generative) or the prefix for embedding columns. In shorthand form, omit this argument and pass
newcol = "<glue prompt>"ornewcol = c(system = "...", user = "...")through....- prompt
Optional glue template string for a single user turn; reference any columns in
.data(e.g."{id}. {question}\nContext: {context}"). Ignored if.messagesis supplied.- .messages
Optional named character vector of glue templates to build a multi-turn message, using roles in
c("system","user","assistant","file"). Values are glue templates evaluated per-row; all can reference multiple columns. For multimodal, use role"file"with a column containing a path template.- .config
An llm_config object (generative or embedding).
- .system_prompt
Optional system message sent with every request when
.messagesdoes not include asystementry.- .before, .after
Standard dplyr::relocate helpers controlling where the generated column(s) are placed.
Character vector of tag names to request and parse.
- .fields
NULLto extract all tags, a character vector of tags, a named vector such asc(person_age = "age"), orFALSEto keep onlytags_data.- .rows_per_prompt
Integer scalar, or
Inf. Number of rows packed into a single generative request. The default,1, sends one request per row (the historical behaviour). When greater than1, rows are grouped and sent in one call wrapped in numbered<row_1>...</row_1>tags (see Row batching below);Infsends all rows at once. Works in generative, tag, and structured modes; not applicable to embedding configurations.- .rowpack_payload
One of
c("user","system"). Channel to which the<row_i>data block is appended when batching. The default"user"keeps a static system prompt cacheable; the imperative instruction is always placed in the system message.- .rowpack_recovery
How to handle rows a batched call leaves unresolved. One of
"halve_recursive"(default),"halve_once","singletons","retry_same", or"none"; seellm_fn()for the precise meaning of each.- ...
Passed to the underlying calls:
call_llm_broadcast()in generative mode,get_batched_embeddings()in embedding mode.
Value
.data with the output column, the diagnostic columns, tags_ok,
tags_data, and one column per requested tag or field.
Details
Returns the mutated data frame plus:
tags_okTRUEwhen all requested tags were found.tags_dataA list-column of parsed tag lists.
- tag columns
One column per requested tag or field. Scalar columns are coerced to numeric or logical when all non-missing values allow it.
Shorthand syntax
df |> llm_mutate_tags(result = "{text}", .tags = c("age", "job"), .config = cfg)Examples
if (FALSE) { # \dontrun{
df <- tibble::tibble(city = c("Cairo", "Lima"))
cfg <- llm_config("groq", "openai/gpt-oss-20b", temperature = 0)
df |>
llm_mutate_tags(
geo = "Where is {city}? Give country and continent in their own tags.",
.config = cfg,
.system_prompt = paste(
"Use XML tags for different parts of the answer, but do not nest tags.",
"Return <country>...</country> and <continent>...</continent>."
),
.tags = c("country", "continent")
)
} # }