llm_mutate_tags: Data-frame mutate with XML-like tag output

View source: R/tags_mode.R

llm_mutate_tagsR Documentation

Data-frame mutate with XML-like tag output

Description

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>" or newcol = 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 .messages is 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 .messages does not include a system entry.

.before, .after

Standard dplyr::relocate helpers controlling where the generated column(s) are placed.

.tags

Character vector of tag names to request and parse.

.fields

NULL to extract all tags, a character vector of tags, a named vector such as c(person_age = "age"), or FALSE to keep only tags_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_ok

TRUE when all requested tags were found.

tags_data

A 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)

See Also

llm_mutate(), llm_parse_tags(), llm_parse_tags_col(), llm_mutate_structured(), llm_parse_structured_col()

Examples

## Not run: 
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")
  )

## End(Not run)


LLMR documentation built on May 22, 2026, 1:07 a.m.