nemoguardrails.testing.fake_model

View as Markdown

Framework-agnostic fake LLM model for testing guardrails configurations.

This module exposes :class:FakeLLMModel, a lightweight implementation of the LLMModel protocol used by NeMo Guardrails. It is intended for use in tests where a deterministic, scripted set of responses is preferable to calling out to a real model provider.

Module Contents

Classes

NameDescription
FakeLLMModelFramework-agnostic fake LLM for testing. Implements the LLMModel protocol.

API

class nemoguardrails.testing.fake_model.FakeLLMModel(
responses: typing.Optional[typing.List[str]] = None,
llm_responses: typing.Optional[typing.List[nemoguardrails.types.LLMResponse]] = None,
llm_exception: typing.Optional[Exception] = None,
token_usage: typing.Optional[typing.List[typing.Dict[str, int]]] = None,
should_return_token_usage: bool = False
)

Framework-agnostic fake LLM for testing. Implements the LLMModel protocol.

Parameters:

responses
Optional[List[str]]Defaults to None

A list of plain string responses. Each call to :meth:generate_async (or :meth:stream_async) consumes the next entry. Mutually exclusive with llm_responses.

llm_responses
Optional[List[LLMResponse]]Defaults to None

A list of :class:~nemoguardrails.types.LLMResponse objects. Useful when tool calls or structured fields need to be asserted. Takes precedence over responses when provided.

llm_exception
Optional[Exception]Defaults to None

An exception instance to raise on every generation, useful for exercising error-handling paths.

token_usage
Optional[List[Dict[str, int]]]Defaults to None

Optional list of token usage dictionaries (one per response). Each entry may include prompt_tokens, completion_tokens and total_tokens keys.

should_return_token_usage
boolDefaults to False

When True, populate :attr:LLMResponse.usage from token_usage.

inference_count
= 0
model_name
str
provider_name
Optional[str]
provider_url
Optional[str]
responses
nemoguardrails.testing.fake_model.FakeLLMModel._get_usage() -> typing.Optional[nemoguardrails.types.UsageInfo]
nemoguardrails.testing.fake_model.FakeLLMModel._next_response() -> nemoguardrails.types.LLMResponse
nemoguardrails.testing.fake_model.FakeLLMModel.generate_async(
prompt,
stop = None,
kwargs = {}
) -> nemoguardrails.types.LLMResponse
async
nemoguardrails.testing.fake_model.FakeLLMModel.stream_async(
prompt,
stop = None,
kwargs = {}
)
async