JsonOutputParser<ParserInput extends Object?> class

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

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

Example:

final model = ChatOpenAI(
  apiKey: openAiApiKey,
  defaultOptions: ChatOpenAIOptions(
    responseFormat: ChatOpenAIResponseFormat(
      type: ChatOpenAIResponseFormatType.jsonObject,
    ),
  ),
);
final parser = JsonOutputParser<ChatResult>();
final chain = model.pipe(parser);
final stream = chain.stream(
  PromptValue.string(
    'Output a list of the countries france, spain and japan and their '
    'populations in JSON format. Use a dict with an outer key of '
    '"countries" which contains a list of countries. '
    'Each country should have the key "name" and "population"',
  ),
);
await stream.forEach((final chunk) => print('$chunk|'));
// {}|
// {countries: []}|
// {countries: [{name: France}]}|
// {countries: [{name: France, population: 67076000}, {}]}|
// {countries: [{name: France, population: 67076000}, {name: Spain}]}|
// {countries: [{name: France, population: 67076000}, {name: Spain, population: 46723749}]}|
// {countries: [{name: France, population: 67076000}, {name: Spain, population: 46723749}, {name: Japan}]}|
// {countries: [{name: France, population: 67076000}, {name: Spain, population: 46723749}, {name: Japan, population: 126476461}]}|
Inheritance

Constructors

JsonOutputParser({bool reduceOutputStream = false})
Output parser that returns the output of the previous Runnable as a JSON Map.

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<Map<String, dynamic>>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(OutputParserOptions options) → RunnableBinding<ParserInput, OutputParserOptions, Map<String, dynamic>>
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<Map<String, dynamic>>
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<Map<String, dynamic>, 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<Map<String, dynamic>>
Streams the output of invoking the Runnable on the given input.
override
streamFromInputStream(Stream<ParserInput> inputStream, {OutputParserOptions? options}) → Stream<Map<String, dynamic>>
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, Map<String, dynamic>>> fallbacks) → RunnableWithFallback<ParserInput, Map<String, dynamic>>
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, Map<String, dynamic>>
Adds retry logic to an existing runnable.
inherited

Operators

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