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,
...
)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.- ...
Passed to the underlying calls:
call_llm_broadcast()in generative mode,get_batched_embeddings()in embedding mode.
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("openai", "gpt-4.1-nano", 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")
)
} # }