stream_listener() attaches a callback to an output field for
run_stream(), like DSPy's StreamListener. The callback receives the
field's text as it is produced: chunk by chunk when the field can be
streamed, otherwise once with the complete value.
Value
A listener (class dsprrr_stream_listener) for the listeners
argument of run_stream().
Details
A field can be streamed chunk by chunk when it is the only output field of its module and is a string. Fields of modules with several outputs, or non-string outputs, arrive once, when the module finishes, because structured output cannot be streamed.
See also
Other execution:
concurrency_control(),
evaluate(),
predict.Module(),
run(),
run_async(),
run_dataset(),
run_stream(),
stream_async()
Examples
show_answer <- stream_listener("answer", function(chunk) cat(chunk))
if (FALSE) { # \dontrun{
storyteller <- module(signature("question -> answer"))
run_stream(
storyteller,
question = "Tell me a short story about a lighthouse.",
.llm = ellmer::chat_openai(model = "gpt-6-luna"),
listeners = show_answer
)
} # }