- 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.
Example: evals/support/agent/eval.py
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, andbody;path: everything after the host;query: a dict of lists;text()andjson();index: how many calls this mock got before this one.
- Returns a JSON value, sent with status
200. - Returns
mock.Response(status, json=..., text=..., body=..., headers=...). - Returns
None, or raisesmock.Unmatched, for a request it doesn’t handle. The app gets501, and the request is logged as unmatched. - Raises
mock.Error(type, message)to answer500with 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:- TypeScript
- Python
Example: src/incidents.ts
observe({ mockKey: "..." }) sets another key. mockable: false makes a function always run.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
selfdropped. Python’s keyword-only arguments are incall.kwargs.
- 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 aMockedErrorwith thattypeandmessage.lmnrand@lmnr-ai/lmnrexportMockedError. - Returns
mock.PASSTHROUGHto run the real function.
@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 samectx:
ctx.case.fixture_json(path),fixture_text(path), andfixture_bytes(path)read the case’stests/fixtures/, as inverify(). Put each case’s mock data there, next toexpected.json. Don’t put it inenvironment/, because the agent can read files there.ctx.case.nameandctx.case.metadataidentify the case.ctx.stateis one dict per trial, shared by all handlers. Use it in mocks that need to remember earlier calls.ctx.callslists this key’s earlier calls.ctx.envis the trial’s env.
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 beforesetup(). 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
Inverify(), 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.