RunnableSequence<RunInput extends Object?, RunOutput extends Object?> class

A RunnableSequence allows you to run multiple Runnable objects sequentially, passing the output of the previous Runnable to the next one.

You can create a RunnableSequence in several ways:

final chain = promptTemplate.pipe(chatModel);
  • Using the | operator. This is a convenience method that calls Runnable.pipe under the hood (note that it offers less type safety than Runnable.pipe because of Dart limitations). E.g.:
final chain = promptTemplate | chatModel;
final chain = Runnable.fromList([promptTemplate, chatModel]);

When you call invoke on a RunnableSequence, it will invoke each Runnable in the sequence in order, passing the output of the previous Runnable to the next one. The output of the last Runnable in the sequence is returned.

You can think of RunnableSequence as the replacement for SequentialChain.

Example:

final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final model = ChatOpenAI(apiKey: openaiApiKey);

final promptTemplate = ChatPromptTemplate.fromTemplate(
  'Tell me a joke about {topic}',
);

// The following three chains are equivalent:
final chain1 = promptTemplate | model | StringOutputParser();
final chain2 = promptTemplate.pipe(model).pipe(StringOutputParser());
final chain3 = Runnable.fromList(
  [promptTemplate, model, StringOutputParser()],
);

final res = await chain1.invoke({'topic': 'bears'});
print(res);
// Why don't bears wear shoes? Because they have bear feet!
Inheritance
Available extensions

Constructors

RunnableSequence({required Runnable<RunInput, RunnableOptions, Object?> first, List<Runnable<Object?, RunnableOptions, Object?>> middle = const [], required Runnable<Object?, RunnableOptions, RunOutput> last})
A RunnableSequence allows you to run multiple Runnable objects sequentially, passing the output of the previous Runnable to the next one.
const

Properties

defaultOptions RunnableOptions
The default options to use when invoking the Runnable.
finalinherited
first Runnable<RunInput, RunnableOptions, Object?>
The first Runnable in the RunnableSequence.
final
hashCode int
The hash code for this object.
no setterinherited
last Runnable<Object?, RunnableOptions, RunOutput>
The last Runnable in the RunnableSequence.
final
middle List<Runnable<Object?, RunnableOptions, Object?>>
The middle Runnables in the RunnableSequence.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
steps List<Runnable<Object?, RunnableOptions, Object?>>
Returns a list of all the Runnables in the RunnableSequence.
no setter

Methods

batch(List<RunInput> inputs, {List<RunnableOptions>? options}) Future<List<RunOutput>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(RunnableOptions options) RunnableBinding<RunInput, RunnableOptions, RunOutput>
Binds the Runnable to the given options.
inherited
close() → void
Cleans up any resources associated with it the Runnable.
override
getCompatibleOptions(RunnableOptions? options) RunnableOptions?
Returns the given options if they are compatible with the Runnable, otherwise returns null.
inherited
invoke(RunInput input, {RunnableOptions? options}) Future<RunOutput>
Invokes the RunnableSequence on the given input.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pipe<NewRunOutput extends Object?, NewCallOptions extends RunnableOptions>(Runnable<RunOutput, NewCallOptions, NewRunOutput> next) RunnableSequence<RunInput, NewRunOutput>
Pipes the output of this RunnableSequence into another Runnable.
override
stream(RunInput input, {RunnableOptions? options}) Stream<RunOutput>
Streams the output of invoking the Runnable on the given input.
override
streamFromInputStream(Stream<RunInput> inputStream, {RunnableOptions? options}) Stream<RunOutput>
Streams the output of invoking the Runnable on the given inputStream.
override
toString() String
A string representation of this object.
inherited
withFallbacks(List<Runnable<RunInput, RunnableOptions, RunOutput>> fallbacks) RunnableWithFallback<RunInput, RunOutput>
Adds fallback runnables to be invoked if the primary runnable fails.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

from(List<Runnable<Object?, RunnableOptions, Object?>> runnables) RunnableSequence<Object?, Object?>
Creates a RunnableSequence from a list of Runnables.