def test_prepare_chat_with_tools(dashscope_llm):
    """Test _prepare_chat_with_tools correctly prepares chat with tools."""
    tools: Sequence[DummyTool] = [DummyTool()]

    chat_history: List[ChatMessage] = [
        ChatMessage(role="assistant", content="Previous message")
    ]
    user_msg: str = "User's question"

    extra_kwargs = {"extra_param": 123}

    result = dashscope_llm._prepare_chat_with_tools(
        tools=tools,
        user_msg=user_msg,
        chat_history=chat_history,
        verbose=True,
        allow_parallel_tool_calls=False,
        **extra_kwargs,
    )

    assert "messages" in result
    assert "tools" in result
    assert "stream" in result
    assert result["extra_param"] == 123

    messages: List[ChatMessage] = result["messages"]
    assert len(messages) == 2
    assert messages[0].role == "assistant"
    assert messages[0].content == "Previous message"
    assert messages[1].role == "user"
    assert messages[1].content == "User's question"

    tools_spec = result["tools"]
    expected_tool_spec = {
        "type": "function",
        "function": {
            "name": "dummy_tool",
            "description": "A dummy tool for testing.",
            "parameters": {
                "type": "object",
                "properties": {
                    "param1": {"type": "string", "description": "A test parameter."}
                },
            },
            "required": ["param1"],
        },
    }
    assert len(tools_spec) == 1
    assert tools_spec[0] == expected_tool_spec

    assert result["stream"] is True
Test _prepare_chat_with_tools correctly prepares chat with tools.
llama-index-integrations/llms/llama-index-llms-dashscope/tests/test_dashscope.py