Skip to main content
A mock answers in place of a dependency that can’t run in the trial’s container. Examples are a private internal service, or a SaaS API that has no simulator. In an agent evaluation, mocks are the last resort. First, run the real service as one of the services. If there is a simulator, use it. Mock only what’s left. A mock says only what you wrote it to say, so the eval doesn’t measure that part of your app. Keep a list of what you mocked and why. There are two kinds:
  • HTTP mocks answer an API your app reaches through a base-URL setting. The app’s own HTTP code still runs.
  • Function mocks answer for a function that your app already observes with Laminar’s SDK. The function doesn’t run. Use a function mock when the dependency has no base URL to point at a mock. Examples are an SDK client, a gRPC call, or a function that makes several calls.
For example, here are mocks for a made-up support app. The app looks customers up in an internal CRM and posts replies to a helpdesk API:
Example: evals/support/agent/eval.py
Keep the model real. The app’s LLM calls should go to the model under test. A canned response only tests the harness.

HTTP mocks

The key is the API’s base URL: a host, optionally with a path. mock.http(handler, base_url_env="HELPDESK_API_URL") sets that env var to the mock’s URL in each trial. Point your app’s existing base-URL setting at it. If the app hard-codes the vendor’s URL, first change the app to read the URL from config. The handler gets req and ctx. req has:
  • method, headers, and body;
  • path: everything after the host;
  • query: a dict of lists;
  • text() and json();
  • index: how many calls this mock got before this one.
The handler does one of these:
  • Returns a JSON value, sent with status 200.
  • Returns mock.Response(status, json=..., text=..., body=..., headers=...).
  • Returns None, or raises mock.Unmatched, for a request it doesn’t handle. The app gets 501, and the request is logged as unmatched.
  • Raises mock.Error(type, message) to answer 500 with that error.

Function mocks

Your app may call an observed function whose key has a mock. Then the app’s Laminar SDK asks the mock server for the answer. It returns or raises whatever the handler says. The function itself doesn’t run. In the trace, the function’s span is marked as mocked. The key is the observed function’s mock key. Without a mock key, it’s the span name, and without that, the function name. For example:
Example: src/incidents.ts
observe({ mockKey: "..." }) sets another key. mockable: false makes a function always run.
The handler gets call and ctx. call.args is the list of positional arguments, in order:
  • In TypeScript, they are the arguments as passed.
  • In Python, they are all positional parameters, with defaults filled in and self dropped. Python’s keyword-only arguments are in call.kwargs.
The handler:
  • Returns the function’s return value. In Python, the value is converted to the function’s return annotation when it fits, such as a pydantic model or a dataclass.
  • Raises mock.Error(type, message) to make the function raise. In the app, the error is a MockedError with that type and message. lmnr and @lmnr-ai/lmnr export MockedError.
  • Returns mock.PASSTHROUGH to run the real function.
Function mocks need @lmnr-ai/lmnr 0.9.8 or later in a TypeScript app, or lmnr 0.9.3 or later in a Python app. With an older SDK, the real function runs. In TypeScript, mock only functions declared async. In both SDKs, generators can’t be mocked. A function mock doesn’t fall back to the real call. If the mock server is unreachable or the handler crashes, the function raises a MockedError of type MockFailure. The handler’s traceback is in the trial’s agent/mocks.log.

What handlers get

Both kinds of handler get the same ctx:
  • ctx.case.fixture_json(path), fixture_text(path), and fixture_bytes(path) read the case’s tests/fixtures/, as in verify(). Put each case’s mock data there, next to expected.json. Don’t put it in environment/, because the agent can read files there.
  • ctx.case.name and ctx.case.metadata identify the case.
  • ctx.state is one dict per trial, shared by all handlers. Use it in mocks that need to remember earlier calls.
  • ctx.calls lists this key’s earlier calls.
  • ctx.env is the trial’s env.
Calls are handled one at a time. So index, calls, and state are the same on each run of a case.

Where handlers run

Handlers run inside the trial’s container, in a mock server. The server starts after the services and before setup(). Handlers must be top-level functions, sync or async. Put them in eval.py or in a module under evals/, such as evals/_shared/mocks.py. Lambdas and methods don’t work. The mock server imports eval.py in the container. So the packages that eval.py imports at the top must be installed there too. By default, the server gets the same packages as eval.py on the host: the ones in evals/pyproject.toml and in eval.py’s PEP 723 block. They go into the Laminar layer under /opt/lmnr, not into your app’s environment. To install a different list, set mock_dependencies on the runner. To add to the default list, set it to [mock.PROJECT_DEPENDENCIES, "extra-package"]. Once the server has started, the handlers and the case’s fixtures exist only in its memory. So the agent can’t read them from the container.

Grade with the call log

In verify(), ctx.mocks.calls(key) lists one mock’s calls in order. Each call has its request, outcome, response, index, and ts. ctx.mocks.all() lists all calls. ctx.mocks.unmatched() lists the HTTP requests that no handler answered. Such requests usually mean the mock is missing a route your app uses. The log is saved as /logs/artifacts/mocks.jsonl, so it also works with a separate verifier. In a local run, it’s in each trial’s artifacts/logs/artifacts/ folder. The runner sets LMNR_MOCK_URL and each base_url_env in the env of each command. If your app is started with an explicit env, such as env -i, pass these variables through, like the services’ URLs.

Next steps

Services

Run the real database, or a simulated SaaS API, instead of a mock.

Match production

What to run for real, what to simulate, and what to mock.

Write eval.py

The runner’s attributes, its phases, and the ctx API.

Capture the trajectory

Get the app’s LLM and tool calls into each trial’s trace.