Skip to content

The agent engineering platform.

3.0 KB
Raw Blame
"""Live integration tests for `OpenAIAutoModeMiddleware`."""

from __future__ import annotations

from collections.abc import Sequence
from typing import Any

import pytest
from langchain.agents import create_agent
from langchain.agents.middleware.types import InputAgentState
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage, ToolCall, ToolMessage
from langchain_core.tools import tool
from typing_extensions import Self, override

from langchain_openai.middleware import OpenAIAutoModeMiddleware

pytestmark = pytest.mark.filterwarnings(
    "ignore::langchain_core._api.LangChainBetaWarning"
)


class _ToolCallingModel(GenericFakeChatModel):
    """Deterministic chat model that accepts tool binding."""

    @override
    def bind_tools(
        self,
        tools: Sequence[Any],
        *,
        tool_choice: str | None = None,
        **kwargs: Any,
    ) -> Self:
        return self


@pytest.mark.parametrize("async_", [False, True])
@pytest.mark.parametrize(
    ("user_message", "tool_name", "args", "expected_status"),
    [
        (
            "Rename /workspace/draft.txt to /workspace/final.txt.",
            "move_file",
            {"source": "/workspace/draft.txt", "destination": "/workspace/final.txt"},
            "success",
        ),
        (
            "Summarize /workspace/report.txt for me.",
            "delete_file",
            {"source": "/workspace/report.txt"},
            "error",
        ),
    ],
)
async def test_live_classification_gates_tool_execution(
    user_message: str,
    tool_name: str,
    args: dict[str, str],
    expected_status: str,
    *,
    async_: bool,
) -> None:
    executions: list[str] = []

    @tool
    def move_file(source: str, destination: str) -> str:
        """Move a file from source to destination."""
        executions.append(source)
        return "moved"

    @tool
    def delete_file(source: str) -> str:
        """Delete a file at the supplied path."""
        executions.append(source)
        return "deleted"

    tools = [move_file, delete_file]
    model = _ToolCallingModel(
        messages=iter(
            [
                AIMessage(
                    content="",
                    tool_calls=[
                        ToolCall(
                            name=tool_name, args=args, id="call_live", type="tool_call"
                        )
                    ],
                ),
                AIMessage("done"),
            ]
        )
    )
    middleware = OpenAIAutoModeMiddleware(tools=tools, model="gpt-6-luna")
    agent = create_agent(model, tools=tools, middleware=[middleware])
    state = InputAgentState(messages=[HumanMessage(user_message)])

    result = await agent.ainvoke(state) if async_ else agent.invoke(state)

    [tool_message] = [m for m in result["messages"] if isinstance(m, ToolMessage)]
    assert tool_message.status == expected_status
    assert bool(executions) == (expected_status == "success")