Implementation:Microsoft Semantic kernel ProcessBuilder
| Knowledge Sources | |
|---|---|
| Domains | Process_Orchestration, Builder_Pattern |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
Concrete API for declaratively constructing event-driven process workflows using ProcessBuilder and AddStepFromType<T>().
Description
ProcessBuilder is the primary class for incrementally defining a Semantic Kernel process. It provides methods to add steps, wire event routes between steps, define process entry points, and build the final executable KernelProcess object. The builder maintains internal collections of steps, entry steps, and external event target mappings that are compiled into a process graph when Build() is called.
Steps are added via AddStepFromType<T>(), which accepts a generic type parameter that must derive from KernelProcessStep. The builder creates a ProcessStepBuilder for the step and adds it to the internal step collection. An optional id parameter allows custom naming; if omitted, the type name is used. The builder enforces step name uniqueness and throws InvalidOperationException on duplicates.
Usage
Use ProcessBuilder at the start of any process definition. Construct it with a process name, add steps with AddStepFromType<T>(), define the entry event with OnInputEvent(), wire step-to-step routes, and call Build() to produce the KernelProcess.
Code Reference
Source Location
- Repository: semantic-kernel
- File:
dotnet/src/Experimental/Process.Core/ProcessBuilder.cs:L19-100
Key Signatures
public sealed partial class ProcessBuilder : ProcessStepBuilder
{
// Constructor
public ProcessBuilder(string id, string? description = null,
ProcessBuilder? processBuilder = null, Type? stateType = null);
// Add a step by type
public ProcessStepBuilder AddStepFromType<TStep>(string? id = null,
IReadOnlyList<string>? aliases = null) where TStep : KernelProcessStep;
// Add a step with initial state
public ProcessStepBuilder AddStepFromType<TStep, TState>(TState initialState,
string? id = null, IReadOnlyList<string>? aliases = null)
where TStep : KernelProcessStep<TState>
where TState : class, new();
// Define the entry event
public ProcessEdgeBuilder OnInputEvent(string eventId);
// Define error handler
public ProcessEdgeBuilder OnError();
// Add a subprocess
public ProcessBuilder AddStepFromProcess(ProcessBuilder kernelProcess,
IReadOnlyList<string>? aliases = null);
// Build the process
public KernelProcess Build(KernelProcessStateMetadata? stateMetadata = null);
}
Import
using Microsoft.SemanticKernel;
I/O Contract
Constructor Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| id | string |
Yes | The name of the process. Must not be null or whitespace. |
| description | string? |
No | A semantic description of the process being built. |
| processBuilder | ProcessBuilder? |
No | An existing ProcessBuilder to copy from. |
| stateType | Type? |
No | The type of user-defined process state. |
AddStepFromType<T> Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| TStep | Type parameter | Yes | The step class type. Must derive from KernelProcessStep. |
| id | string? |
No | Custom identifier for the step. Defaults to the type name. |
| aliases | IReadOnlyList<string>? |
No | Previous step names for backward compatibility with saved state. |
Build Output
| Name | Type | Description |
|---|---|---|
| return | KernelProcess |
An executable process object containing the compiled step graph and event edges. |
Usage Examples
Simple Linear Process
using Microsoft.SemanticKernel;
// Create the process builder with a name
ProcessBuilder process = new("ChatBot");
// Add steps from their types
var startStep = process.AddStepFromType<StartStep>();
var doSomeWorkStep = process.AddStepFromType<DoSomeWorkStep>();
var doMoreWorkStep = process.AddStepFromType<DoMoreWorkStep>();
var lastStep = process.AddStepFromType<LastStep>();
// Define the entry event and wire steps together
process
.OnInputEvent(ProcessEvents.StartProcess)
.SendEventTo(new ProcessFunctionTargetBuilder(startStep));
startStep
.OnFunctionResult()
.SendEventTo(new ProcessFunctionTargetBuilder(doSomeWorkStep));
doSomeWorkStep
.OnFunctionResult()
.SendEventTo(new ProcessFunctionTargetBuilder(doMoreWorkStep));
doMoreWorkStep
.OnFunctionResult()
.SendEventTo(new ProcessFunctionTargetBuilder(lastStep));
lastStep
.OnFunctionResult()
.StopProcess();
// Build the executable process
KernelProcess kernelProcess = process.Build();
Process with Named Function Targets
ProcessBuilder process = new("AccountOpening");
var formStep = process.AddStepFromType<CompleteNewCustomerFormStep>();
var userInputStep = process.AddStepFromType<UserInputStep>();
var displayStep = process.AddStepFromType<DisplayAssistantMessageStep>();
// Entry point targets a specific function on the step
process
.OnInputEvent("StartProcess")
.SendEventTo(new ProcessFunctionTargetBuilder(
formStep,
CompleteNewCustomerFormStep.ProcessStepFunctions.NewAccountWelcome));
// User input routed to a specific function and parameter
userInputStep
.OnEvent(CommonEvents.UserInputReceived)
.SendEventTo(new ProcessFunctionTargetBuilder(
formStep,
CompleteNewCustomerFormStep.ProcessStepFunctions.NewAccountProcessUserInfo,
"userMessage"));
Step with Custom Id
// Assign a custom id to differentiate multiple instances of the same step type
var validationStep1 = process.AddStepFromType<ValidationStep>(id: "ValidateEmail");
var validationStep2 = process.AddStepFromType<ValidationStep>(id: "ValidatePhone");