Home / Function/ test_runnable_agent_with_function_calls() — langchain Function Reference

test_runnable_agent_with_function_calls() — langchain Function Reference

Architecture documentation for the test_runnable_agent_with_function_calls() function in test_agent.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d4491aa2_0101_31cb_1fad_6616de3b605d["test_runnable_agent_with_function_calls()"]
  47a7b285_8e60_f78f_282d_429958c446fa["test_agent.py"]
  d4491aa2_0101_31cb_1fad_6616de3b605d -->|defined in| 47a7b285_8e60_f78f_282d_429958c446fa
  style d4491aa2_0101_31cb_1fad_6616de3b605d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/tests/unit_tests/agents/test_agent.py lines 539–639

async def test_runnable_agent_with_function_calls() -> None:
    """Test agent with intermediate agent actions."""
    # Will alternate between responding with hello and goodbye
    infinite_cycle = cycle(
        [
            AIMessage(content="looking for pet..."),
            AIMessage(content="Found Pet"),
        ],
    )
    model = GenericFakeChatModel(messages=infinite_cycle)

    template = ChatPromptTemplate.from_messages(
        [
            ("system", "You are Cat Agent 007"),
            ("human", "{question}"),
        ],
    )

    parser_responses = cycle(
        [
            AgentAction(
                tool="find_pet",
                tool_input={
                    "pet": "cat",
                },
                log="find_pet()",
            ),
            AgentFinish(
                return_values={"foo": "meow"},
                log="hard-coded-message",
            ),
        ],
    )

    def fake_parse(_: dict) -> AgentFinish | AgentAction:
        """A parser."""
        return cast("AgentFinish | AgentAction", next(parser_responses))

    @tool
    def find_pet(pet: str) -> str:
        """Find the given pet."""
        if pet != "cat":
            msg = "Only cats allowed"
            raise ValueError(msg)
        return "Spying from under the bed."

    agent = template | model | fake_parse
    executor = AgentExecutor(agent=agent, tools=[find_pet])

    # Invoke
    result = await asyncio.to_thread(executor.invoke, {"question": "hello"})
    assert result == {"foo": "meow", "question": "hello"}

    # ainvoke
    result = await executor.ainvoke({"question": "hello"})
    assert result == {"foo": "meow", "question": "hello"}

    # astream
    results = [r async for r in executor.astream({"question": "hello"})]
    assert results == [
        {
            "actions": [
                AgentAction(
                    tool="find_pet",
                    tool_input={"pet": "cat"},
                    log="find_pet()",
                ),
            ],
            "messages": [AIMessage(content="find_pet()")],
        },
        {
            "messages": [HumanMessage(content="Spying from under the bed.")],
            "steps": [
                AgentStep(
                    action=AgentAction(
                        tool="find_pet",
                        tool_input={"pet": "cat"},
                        log="find_pet()",
                    ),
                    observation="Spying from under the bed.",
                ),

Domain

Subdomains

Frequently Asked Questions

What does test_runnable_agent_with_function_calls() do?
test_runnable_agent_with_function_calls() is a function in the langchain codebase, defined in libs/langchain/tests/unit_tests/agents/test_agent.py.
Where is test_runnable_agent_with_function_calls() defined?
test_runnable_agent_with_function_calls() is defined in libs/langchain/tests/unit_tests/agents/test_agent.py at line 539.

Analyze Your Own Codebase

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

Try Supermodel Free