By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A live model's wording changes between runs, so asserting on the exact sentence produces a test that fails for no product reason. Test the behaviour instead: which tool was called, with which arguments, whether the result came back in a valid ToolMessage, whether a write stayed pending, and what order the trace took. Fake models make all of that deterministic, fast and free — and they run in CI with no key.
GenericFakeChatModel covers text, but it does not implement bind_tools, which create_agent calls when you pass tools. Subclass it and return self from bind_tools, then script one turn per model call: first the tool call, then the final answer.
import pytest
from langchain.agents import create_agent
from langchain.messages import AIMessage, HumanMessage, ToolCall
from langchain.tools import tool
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
class FakeToolCallingModel(GenericFakeChatModel):
"""A fake model that create_agent can bind tools to."""
def bind_tools(self, tools, **kwargs):
return self
@tool
def get_weather(city: str) -> str:
"""Return the current weather for a city."""
return f"{city}: 31°C, humid, light rain at 17:00"
@pytest.fixture
def weather_agent():
# A fresh iterator per test: an exhausted fake model cannot be reused.
model = FakeToolCallingModel(
messages=iter(
[
AIMessage(
content="",
tool_calls=[
ToolCall(
name="get_weather",
args={"city": "Mumbai"},
id="call_1",
)
],
),
AIMessage(content="Rain is likely in Mumbai this evening."),
]
)
)
return create_agent(model, tools=[get_weather])
def test_tool_is_called_with_valid_arguments(weather_agent):
result = weather_agent.invoke(
{"messages": [HumanMessage("Will it rain in Mumbai this evening?")]}
)
ai_turns = [
m for m in result["messages"] if isinstance(m, AIMessage) and m.tool_calls
]
assert len(ai_turns) == 1
assert ai_turns[0].tool_calls[0]["name"] == "get_weather"
assert ai_turns[0].tool_calls[0]["args"] == {"city": "Mumbai"}
def test_tool_result_reaches_the_final_answer(weather_agent):
result = weather_agent.invoke(
{"messages": [HumanMessage("Will it rain in Mumbai this evening?")]}
)
tool_messages = [m for m in result["messages"] if m.type == "tool"]
assert len(tool_messages) == 1
assert "Mumbai" in tool_messages[0].content
assert "rain" in result["messages"][-1].text.lower()tool.name, tool.description and tool.args_schema.model_json_schema(); assert required fields and enums, and assert that bad arguments raise.result['messages'] from the agent invocation.AIMessage with tool_calls, ToolMessage, final AIMessage.Key takeaways
Test behaviour — tool calls, arguments, results, approval state and trace order — not exact wording.
Subclass GenericFakeChatModel to implement bind_tools, then script one response per model turn.
Use a fresh fake model per test; an exhausted iterator silently breaks later tests.
Fake-model tests run in CI without a key; keep a small marked live-model suite for quality.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.