ToolsOutputParser class

A parser that returns the list of tool calls returned by the model.

When streaming, the parser attempts to “auto-complete” the partial json from each chunk into a valid state.

Example:

const tool = ToolSpec(
  name: 'joke',
  description: 'A joke',
  inputJsonSchema: {
    'type': 'object',
    'properties': {
      'setup': {
        'type': 'string',
        'description': 'The setup for the joke',
      },
      'punchline': {
        'type': 'string',
        'description': 'The punchline to the joke',
      },
    },
    'required': ['location', 'punchline'],
  },
);
final promptTemplate = ChatPromptTemplate.fromTemplate(
  'tell me a long joke about {foo}',
);
final chat = ChatOpenAI(
  apiKey: openaiApiKey,
  defaultOptions: ChatOpenAIOptions(
    temperature: 0,
  ),
).bind(
  ChatOpenAIOptions(
    tools: [tool],
    toolChoice: ChatToolChoice.forced(name: 'joke'),
  ),
);
final outputParser = ToolsOutputParser();
final chain = promptTemplate.pipe(chat).pipe(outputParser);
final res = await chain.invoke({'foo': 'bears'});
print(res);
// [ParsedToolCall{
//   id: call_5TU1iYgYO3Z81eAuTe7J23f7,
//   name: joke,
//   arguments: {
//     setup: Why don't bears like fast food restaurants?,
//     punchline: Because they can't bear the wait!
//   },
// }]
Inheritance

Constructors

ToolsOutputParser({bool reduceOutputStream = false})
A parser that returns the list of tool calls returned by the model.

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<ChatResult> inputs, {List<OutputParserOptions>? options}) → Future<List<List<ParsedToolCall>>>
Batches the invocation of the Runnable on the given inputs.
inherited
bind(OutputParserOptions options) → RunnableBinding<ChatResult, OutputParserOptions, List<ParsedToolCall>>
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(ChatResult input, {OutputParserOptions? options}) → Future<List<ParsedToolCall>>
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<List<ParsedToolCall>, NewCallOptions, NewRunOutput> next) → RunnableSequence<ChatResult, NewRunOutput>
Pipes the output of this Runnable into another Runnable using a RunnableSequence.
inherited
stream(ChatResult input, {OutputParserOptions? options}) → Stream<List<ParsedToolCall>>
Streams the output of invoking the Runnable on the given input.
override
streamFromInputStream(Stream<ChatResult> inputStream, {OutputParserOptions? options}) → Stream<List<ParsedToolCall>>
Streams the output of invoking the Runnable on the given inputStream.
override
toString() → String
A string representation of this object.
inherited
withFallbacks(List<Runnable<ChatResult, RunnableOptions, List<ParsedToolCall>>> fallbacks) → RunnableWithFallback<ChatResult, List<ParsedToolCall>>
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<ChatResult, List<ParsedToolCall>>
Adds retry logic to an existing runnable.
inherited

Operators

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