FakeChatModel class final

A ChatModel that returns scripted answers and records what it was asked.

final model = FakeChatModel.text('Paris.');
await agent.run('What is the capital of France?');

expect(model.callCount, 1);
expect(model.lastRequest.messages.last.text, contains('France'));
Implemented types
Available extensions

Constructors

FakeChatModel({List<FakeTurn> turns = const <FakeTurn>[], ModelInfo? info, Duration latency = Duration.zero})
Creates a model that plays turns in order.
FakeChatModel.failing(AgenticException error, {ModelInfo? info})
Creates a model that always fails with error.
factory
FakeChatModel.failingThenAnswering({required AgenticException error, int times = 1, String then = 'Recovered.', ModelInfo? info})
Creates a model that fails times times, then answers with then.
factory
FakeChatModel.text(String text, {ModelInfo? info})
Creates a model that always answers with text.
factory
FakeChatModel.toolCall({required List<ToolCallPart> toolCalls, String then = 'Done.', ModelInfo? info})
Creates a model that requests toolCalls, then answers with then.
factory

Properties

callCount int
How many calls this model received.
no setter
hashCode int
The hash code for this object.
no setterinherited
info ModelInfo
What this model is and what it supports.
final
isDisposed bool
Whether dispose has been called.
no setter
lastRequest ChatRequest
The most recent request.
no setter
latency Duration
Artificial delay before each answer, for exercising timeouts.
final
requests List<ChatRequest>
Every request this model received, in order.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

checkSupports(ChatRequest request) → void

Available on ChatModel, provided by the ChatModelOperations extension

Verifies this model can satisfy request.
dispose() Future<void>
Releases every resource held by this object.
enqueue(FakeTurn turn) → void
Appends a turn to the script.
generate(ChatRequest request, {AgenticContext? context}) Future<ChatResponse>
Generates a complete answer.
override
generateStructured<T>(ChatRequest request, {required String name, required JsonSchema schema, required T fromJson(JsonMap json), AgenticContext? context}) Future<T>

Available on ChatModel, provided by the ChatModelOperations extension

Answers with a value conforming to schema.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
prompt(String prompt, {String? system, double? temperature, int? maxOutputTokens, AgenticContext? context}) Future<String>

Available on ChatModel, provided by the ChatModelOperations extension

Answers a single prompt and returns the text.
reset() → void
Forgets recorded requests and rewinds the script.
stream(ChatRequest request, {AgenticContext? context}) Stream<ChatChunk>
Generates an answer incrementally.
override
toString() String
A string representation of this object.
override

Operators

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