Skip to content

Testing

Guide: Testing.

FakeModel

FakeModel(replies: Iterable[Any], *, name: str = 'fake', context_window: int = 200000, price: Price | None = None)

Bases: Model

A Model that returns prepared replies in order, with no API key or network.

Each request (think, one attempt of ask, compact) uses the next item:

  • str: a text reply with no tool calls
  • ToolCall: a reply with just that call (see tool_call)
  • list or tuple of str, ToolCall and RawBlock: a reply with those blocks in order
  • Reply: used as is
  • an exception instance: raised, for testing error paths
  • a callable: called with the Request, and its result is used by the rules above

Token usage is estimated from the text, so state.usage fills in as it would with a real model. Safe to use from several threads.

Parameters:

Name Type Description Default
replies Iterable[Any]

The reply items, in order.

required
name str

Model name.

'fake'
context_window int

Context window size in tokens. A request estimated above it raises ContextTooLongError, so compaction can be tested.

200000
price Price | None

Token prices for usage.cost.

None
Example
fake = FakeModel([tool_call("read_file", path="main.py"), "The bug is on line 3"])
agent.copy(model=fake, reporter=None).run(State("Find the bug"))
assert fake.remaining == 0

provider class-attribute instance-attribute

provider = 'fake'

Always "fake".

name instance-attribute

name: str = name

Model name, "fake" by default.

price instance-attribute

price: Price | None = price

Token prices used for usage.cost.

requests instance-attribute

requests: list[Request] = []

Every request received, in order. Use it to assert what the agent sent.

context_window property

context_window: int

Context window size in tokens, as passed to context_window=.

remaining property

remaining: int

Number of replies not used yet.

respond

respond(request: Request, on_text: OnText | None = None, on_event: OnEvent | None = None) -> Reply

Records request in requests and returns the next prepared reply.

Each text block is passed to on_text once.

Raises:

Type Description
ContextTooLongError

The estimated request size is above context_window. No item is used.

RuntimeError

Every prepared reply has been used.

FakeHuman

FakeHuman(answers: Iterable[Any])

Bases: Human

A Human that returns prepared answers in order, for testing code that calls ask_human.

Each answer is converted to the requested returns type the same way a typed answer is. An answer that does not fit is skipped and the next one is used, as if the person answered again. An empty answer does not fit returns=str. Safe to use from several threads; questions are answered one at a time.

Parameters:

Name Type Description Default
answers Iterable[Any]

The answers, in order.

required
Example
human = FakeHuman(["yes", "always"])
agent.copy(model=fake, human=human, reporter=None).run(state)
assert human.remaining == 0

questions instance-attribute

questions: list[str] = []

Every question asked, in order.

remaining property

remaining: int

Number of answers not used yet.

ask

ask(state: State, prompt: str, returns: Any = str) -> Any

Records prompt in questions and returns the next answer that fits returns.

Raises:

Type Description
RuntimeError

Every prepared answer has been used.

tool_call

tool_call(name: str, /, **args: Any) -> ToolCall

Builds a tool call to put in FakeModel replies.

Parameters:

Name Type Description Default
name str

The tool name.

required
**args Any

The tool arguments.

{}

Returns:

Type Description
ToolCall

A ToolCall with a new id each time ("call_" + 12 hex digits).

Example
FakeModel([tool_call("read_file", path="main.py"), "Done"])