Skip to main content

runStreamNode

workflow.runStreamNode() executes the streaming node. It iterates through the chunks of the node.stream(input, context) output, triggers the chunk hooks one by one, and calls the optional node.finalize(chunks, context, input) when it's done.

Signature

workflow.runStreamNode<Input, Chunk, Output>(
node: StreamNodeDefinition<Input, Chunk, Output>,
input: Input,
context: NodeContext,
): Promise<{
chunks: Chunk[];
output: Output | undefined;
}>

Parameter

ParameterTypeDescription
nodeStreamNodeDefinitionThe streaming node to execute, typically from createLLMStreamNode or streaming createOutputNode.
inputInputNode input.
contextNodeContextruntime context for hooks, provider, KV, files, abortSignal.

StreamNodeDefinition

FieldDescription
nameThe node name. Required.
standardNameStandard node type name, such as llm-stream, output.
metadataStructure diagram and UI meta information.
streamStreaming execution function, receiving input and NodeContext. Required.
finalizeAn optional aggregate function that uses chunks to generate the final output.

Execution process

StepsDescription
1. Check for interruptionsIf context.abortSignal is interrupted, throw execution_aborted.
2. Send Start EventTriggers the streaming node to start a hook and record the executionId, nodeName, metadata, and startedAt.
3. Traversing the streamCollect each chunk and trigger the chunk hook.
4. finalizeIf the node provides finalize, the final output is generated with chunks.
5. Send Completion EventRecord the output, duration, and executionInfo, and trigger the completion of the hook.
6. Wrong packagingConvert to stream_interrupted on failure and write report.

Return value

FieldTypeDescription
chunksChunk[]All chunks collected, in the same order as the stream output.
outputOutput | undefinedfinalize Results. undefined when the node has no finalize.

Error

SituationError TypeDescription
Pre-execution or in-stream interruptionexecution_aborted or stream_interruptedInterrupts are checked in the streaming traversal.
stream/finalize throw errorstream_interruptedErrors are written to the node execution report.

Example

const result = await workflow.runStreamNode(streamNode, parsed, context);

return {
errCode: 0,
errMessage: "",
content: result.output ?? "",
};