Home / Function/ test_agent_loop() — langchain Function Reference

test_agent_loop() — langchain Function Reference

Architecture documentation for the test_agent_loop() function in test_chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  fa4bf4ee_d67f_0019_296c_4f40a7aeaae5["test_agent_loop()"]
  71dcb56e_a445_727d_c4bb_5dc733f24038["test_chat_models.py"]
  fa4bf4ee_d67f_0019_296c_4f40a7aeaae5 -->|defined in| 71dcb56e_a445_727d_c4bb_5dc733f24038
  style fa4bf4ee_d67f_0019_296c_4f40a7aeaae5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/ollama/tests/integration_tests/chat_models/test_chat_models.py lines 246–295

def test_agent_loop(model: str, output_version: str | None) -> None:
    """Test agent loop with tool calling and message passing."""

    @tool
    def get_weather(location: str) -> str:
        """Get the weather for a location."""
        return "It's sunny and 75 degrees."

    llm = ChatOllama(model=model, output_version=output_version, reasoning="low")
    llm_with_tools = llm.bind_tools([get_weather])

    input_message = HumanMessage("What is the weather in San Francisco, CA?")
    tool_call_message = llm_with_tools.invoke([input_message])
    assert isinstance(tool_call_message, AIMessage)

    tool_calls = tool_call_message.tool_calls
    assert len(tool_calls) == 1
    tool_call = tool_calls[0]
    assert tool_call["name"] == "get_weather"
    assert "location" in tool_call["args"]

    tool_message = get_weather.invoke(tool_call)
    assert isinstance(tool_message, ToolMessage)
    assert tool_message.content
    assert isinstance(tool_message.content, str)
    assert "sunny" in tool_message.content.lower()

    resp_message = llm_with_tools.invoke(
        [
            input_message,
            tool_call_message,
            tool_message,
        ]
    )
    follow_up = HumanMessage("Explain why that might be using a reasoning step.")
    assert isinstance(resp_message, AIMessage)
    assert len(resp_message.content) > 0

    response = llm_with_tools.invoke(
        [input_message, tool_call_message, tool_message, resp_message, follow_up]
    )
    assert isinstance(resp_message, AIMessage)
    assert len(resp_message.content) > 0

    if output_version == "v1":
        content_blocks = response.content_blocks
        assert content_blocks is not None
        assert len(content_blocks) > 0
        assert any(block["type"] == "text" for block in content_blocks)
        assert any(block["type"] == "reasoning" for block in content_blocks)

Domain

Subdomains

Frequently Asked Questions

What does test_agent_loop() do?
test_agent_loop() is a function in the langchain codebase, defined in libs/partners/ollama/tests/integration_tests/chat_models/test_chat_models.py.
Where is test_agent_loop() defined?
test_agent_loop() is defined in libs/partners/ollama/tests/integration_tests/chat_models/test_chat_models.py at line 246.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free