StringOutputParser<ParserInput extends Object?> class

Output parser that returns the output of the previous Runnable as a String.

  • ParserInput - The type of the input to the parser.

If the input is:

  • null, the parser returns an empty String.
  • A LLMResult, the parser returns the output String.
  • A ChatResult, the parser returns the content of the output message as a String.
  • A ChatMessage, the parser returns the content of the message as a String.
  • A Document, the parser returns the page content as a String.
  • Anything else, the parser returns the String representation of the input.

Example:

final model = ChatOpenAI(apiKey: openAiApiKey);
final promptTemplate = ChatPromptTemplate.fromTemplate(
  'Tell me a joke about {topic}',
);
final chain = promptTemplate | model | StringOutputParser();
final res = await chain.invoke({'topic': 'bears'});
print(res);
// Why don't bears wear shoes? Because they have bear feet!
Inheritance

Constructors

StringOutputParser({bool reduceOutputStream = false})
Output parser that returns the output of the previous Runnable as a String.
const

Properties

defaultOptions OutputParserOptions
The default options to use when invoking the Runnable.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
reduceOutputStream bool
When invoking this parser with Runnable.stream, every item from the input stream will be parsed and emitted by default.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

batch(List<ParserInput> inputs, {List<OutputParserOptions>? options}) Future<List<String>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(OutputParserOptions options) RunnableBinding<ParserInput, OutputParserOptions, String>
Binds the Runnable to the given options.
inherited
close() → void
Cleans up any resources associated with it the Runnable.
inherited
getCompatibleOptions(RunnableOptions? options) OutputParserOptions?
Returns the given options if they are compatible with the Runnable, otherwise returns null.
inherited
invoke(ParserInput input, {OutputParserOptions? options}) Future<String>
Invokes the output parser 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<String, NewCallOptions, NewRunOutput> next) RunnableSequence<ParserInput, NewRunOutput>
Pipes the output of this Runnable into another Runnable using a RunnableSequence.
inherited
stream(ParserInput input, {OutputParserOptions? options}) Stream<String>
Streams the output of invoking the Runnable on the given input.
inherited
streamFromInputStream(Stream<ParserInput> inputStream, {OutputParserOptions? options}) Stream<String>
Streams the output of invoking the Runnable on the given inputStream.
override
toString() String
A string representation of this object.
inherited
withFallbacks(List<Runnable<ParserInput, RunnableOptions, String>> fallbacks) RunnableWithFallback<ParserInput, String>
Adds fallback runnables to be invoked if the primary runnable fails.
inherited
withRetry({int maxRetries = 3, FutureOr<bool> retryIf(Object e)?, List<Duration?>? delayDurations, bool addJitter = false}) RunnableRetry<ParserInput, String>
Adds retry logic to an existing runnable.
inherited

Operators

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