Skip to contents

chain_of_thought() creates a prediction module with one of three reasoning modes. The default, "rationale", keeps the existing behavior: it adds a reasoning output field before the requested fields. "plan" adds a short, explicit plan field before the requested fields. "native" keeps the signature unchanged and exposes provider-supplied reasoning metadata when the provider records it in ellmer turns.

Usage

chain_of_thought(
  x,
  prefix = "Let's think step by step in order to",
  chat = NULL,
  template = "",
  demos = list(),
  config = list(),
  mode = c("rationale", "native", "plan"),
  ...
)

Arguments

x

A signature from signature(), or a signature string.

prefix

Start of the reasoning field's description; see with_reasoning(). Used only when mode = "rationale".

chat, template, demos, config

As in module().

mode

The reasoning behavior: "rationale" (the default), "native" or "plan". "native" does not select a model or enable provider reasoning by itself. Configure provider parameters such as reasoning_effort through chat or config, using ellmer's supported parameters.

...

Must be empty.

Value

A prediction module, as from module(). The "rationale" and "plan" modes include their field in the output. With "native", the output keeps the signature's fields; on a structured result, get_metadata() exposes native_reasoning as a list of ellmer ContentThinking objects from the current call, or NULL when the provider did not expose any. It never synthesizes a reasoning string.

Native metadata is supported in structured results from synchronous scalar run() calls and direct run_async() calls. Native mode errors before provider work for batch or streaming execution, whose current result contracts do not preserve these turns.

Examples

solver <- chain_of_thought("question -> answer: float")
solver
#> 
#> ── PredictModule ──
#> 
#> ── Signature 
#> 
#> ── Signature ──
#> 
#> ── Inputs 
#> • question: "string" - Input: question
#> 
#> ── Output 
#> Type: "object(reasoning: string, answer: number)"
#> 
#> ── Instructions 
#> Given the fields `question`, produce the fields `answer`. Think through your
#> reasoning step by step before providing the answer.

native_solver <- chain_of_thought(
  "question -> answer: float",
  mode = "native",
  config = list(reasoning_effort = "low")
)

if (FALSE) { # \dontrun{
result <- run(
  solver,
  question = "What is 15 * 24?",
  .llm = ellmer::chat_openai(model = "gpt-6-luna")
)
result$reasoning
result$answer

native_result <- run(
  native_solver,
  question = "What is 15 * 24?",
  .llm = ellmer::chat_openai(model = "gpt-6-luna"),
  .return_format = "structured"
)
get_metadata(native_result)$native_reasoning
} # }