Skip to main content

Types

ToolMock = Any | Callable[[Any], Any]
ToolMocks = Mapping[str, ToolMock]
A ToolMock is either:
  • A static value returned when the tool is called
  • A callable (tool_input) -> output called with the tool’s arguments
  • A BaseException instance — raises the exception when the tool is called

Function

mock_tools()

@contextmanager
def mock_tools(
    mocks: ToolMocks | None = None,
    *,
    default: ToolMock | object = USE_GLOBAL_DEFAULT,
) -> MockTools
Context manager that intercepts tool calls and returns mock responses instead of executing real tools.
mocks
ToolMocks
default:"None"
Mapping of tool name → mock value or callable.
default
ToolMock
default:"USE_GLOBAL_DEFAULT"
Default mock for any tool not in mocks. Falls back to the process-wide default (set by test_mode=True).

Examples

Per-test mocks

from rdk import mock_tools, observe, init
from anthropic import Anthropic

init(endpoint="...", api_key="...", test_mode=True)

@observe()
def agent(question: str) -> str:
    client = Anthropic()
    # ... tool-calling loop

def test_agent_uses_weather_tool():
    with mock_tools({"get_weather": "72°F, sunny"}):
        result = agent("What's the weather?")
    assert "72" in result

Callable mock

with mock_tools({
    "search": lambda args: f"Results for: {args['query']}",
    "calculate": lambda args: str(eval(args["expression"])),
}):
    result = agent("What is 2 + 2?")

Raise an error

with mock_tools({"search": ValueError("Service unavailable")}):
    result = agent("Search for something")
    # agent receives the error and handles it

Default mock for all tools

# All tool calls return "OK" unless explicitly overridden
with mock_tools(default="OK"):
    result = agent("Do anything")

test_mode and tool mocks

When init(test_mode=True) is used, all tool calls return "MOCKED" by default. You can override specific tools with mock_tools():
init(endpoint="...", api_key="...", test_mode=True)
# Default: all tools return "MOCKED"

with mock_tools({"search": "Custom search result"}):
    # "search" returns "Custom search result"
    # All other tools still return "MOCKED"
    result = agent("Search for data")

pytest fixture

import pytest
from rdk import init, mock_tools

@pytest.fixture(autouse=True)
def rdk_test_mode():
    init(endpoint="https://placeholder", api_key="test", test_mode=True)
    yield
    from rdk import shutdown
    shutdown()

def test_my_agent():
    with mock_tools({"my_tool": {"status": "ok"}}):
        result = my_agent("Do something")
    assert result == "expected"

See Also