Empty vector when adding cross references (ref2vec-centroid)

My collections:

            "Short",
            properties=[
                Property(
                    name=ShortProps.ID.value,
                    data_type=DataType.INT,
                    description="Unique identifier for the short.",
                    skip_vectorization=True,
                ),
                Property(
                    name=ShortProps.HASH_NAME.value,
                    data_type=DataType.TEXT,
                    description="Unique content hash name for the short.",
                    skip_vectorization=True,
                ),
                Property(
                    name=ShortProps.DESCRIPTION.value,
                    data_type=DataType.TEXT,
                    description="Short description of the content.",
                ),
                Property(
                    name=ShortProps.CATEGORY.value,
                    data_type=DataType.TEXT,
                    description="Category of the short.",
                ),
                Property(
                    name=ShortProps.CONTENT.value,
                    data_type=DataType.BLOB,
                    description="Content of the short (e.g., video, image)."
                ),
            ],
            vectorizer_config=[
                # Set a named vector
                Configure.NamedVectors.multi2vec_bind(
                    name="shorts_vec",
                    text_fields=[
                        Multi2VecField(name=ShortProps.DESCRIPTION.value, weight=0.3),
                        Multi2VecField(name=ShortProps.CATEGORY.value, weight=0.2),
                    ],
                    # video_fields=[
                    #     Multi2VecField(name=ShortProps.CONTENT.value, weight=0.5),
                    # ],
                ),
            ],
        )

        await cls.client.collections.create(
            "UserInteractions",
            properties=[
                Property(
                    name=UserInteractionsProps.ID.value,
                    data_type=DataType.INT,
                    description="Unique identifier for the user."
                ),
            ],
            references=[
                ReferenceProperty(
                    name=UserInteractionsProps.LIKED_SHORTS.value,
                    target_collection="Short",
                    description="Short which liked by current user",
                ),
            ],
            vectorizer_config=[
                Configure.NamedVectors.ref2vec_centroid(
                    name="liked_vector",
                    reference_properties=[
                        UserInteractionsProps.LIKED_SHORTS.value,
                    ],
                ),
            ],
        )

I tried add cross-references like this:

async def create_interaction(
        self,
        user_interactions_uuid: UUID,
        short_uuid: UUID,
        interaction_type: InteractionType,
    ) -> None:
        await self.collection.data.reference_add(
            from_uuid=user_interactions_uuid,
            from_property=interaction_type_property[interaction_type.value],
            to=short_uuid
        )

but got object with empty vector:

Object(uuid=_WeaviateUUIDInt('ede63ad5-ae6b-4041-be34-1db9864177c7'), metadata=MetadataReturn(creation_time=None, last_update_time=None, distance=None, certainty=None, score=None, explain_score=None, is_consistent=None, rerank_score=None), properties={'object_id': 2}, references={'liked_shorts': <weaviate.collections.classes.internal._CrossReference object at 0x75f4bf33ae10>}, vector={'liked_vector': []}, collection='UserInteractions')

hi @Bohdan_Klishchov !!

Welcome to our community :hugs:

Can you share a full running code? This has proven more efficient to discover this kind of issue as sometimes the issue is on underlying code that is not share :grimacing:

Here we have a set of recipes here, that can serve as base for this:

I have searched, and there isn’t a recipe for ref2vec-centroid :grimacing:

Maybe we can create one :slight_smile:

Thanks!

1 Like

hello @DudaNogueira
I simplified my code:

import asyncio

import weaviate
from weaviate.classes.config import Configure
from weaviate.client import WeaviateAsyncClient
from weaviate.collections.classes.grpc import QueryReference
from weaviate.collections.classes.config_vectorizers import Multi2VecField
from weaviate.collections.classes.config import Property, DataType, ReferenceProperty

from core.settings import get_settings

settings = get_settings()


async def init_collections(client: WeaviateAsyncClient):

    if await client.collections.exists('Short'):
        return
    if await client.collections.exists('UserInteractions'):
        return
    await client.collections.create(
        "Short",
        properties=[
            Property(
                name='object_id',
                data_type=DataType.INT,
                description="Unique identifier for the short.",
                skip_vectorization=True,
            ),
            Property(
                name='description',
                data_type=DataType.TEXT,
                description="Short description of the content.",
            ),
            Property(
                name='category',
                data_type=DataType.TEXT,
                description="Category of the short.",
            ),
        ],
        vectorizer_config=[
            # Set a named vector
            Configure.NamedVectors.multi2vec_bind(
                name="shorts_vec",
                text_fields=[
                    Multi2VecField(name='description', weight=0.6),
                    Multi2VecField(name='category', weight=0.4),
                ]
            ),
        ],
    )

    await client.collections.create(
        "UserInteractions",
        properties=[
            Property(
                name='object_id',
                data_type=DataType.INT,
                description="Unique identifier for the user."
            ),
        ],
        references=[
            ReferenceProperty(
                name='liked_shorts',
                target_collection="Short",
                description="Short which liked by current user",
            ),
        ],
        vectorizer_config=Configure.Vectorizer.ref2vec_centroid(
            reference_properties=[
                'liked_shorts'
            ]
        ),
    )


async def main():
    client: WeaviateAsyncClient = weaviate.use_async_with_local(
        host=settings.WEAVIATE_HOST,
        port=settings.WEAVIATE_PORT,
        skip_init_checks=True
    )
    await client.connect()
    try:
        await init_collections(client=client)

        short_collection = client.collections.get('Short')
        user_collection = client.collections.get('UserInteractions')

        short_uuid = await short_collection.data.insert(properties={
            'object_id': 1,
            'description': 'description1',
            'category': 'category1'
        }, uuid=uuid.uuid4())
        print(f'created short: {short_uuid}')

        user_uuid = await user_collection.data.insert(properties={'object_id': 1}, uuid=uuid.uuid4())
        print(f'created user: {user_uuid}')

        # creating cross-reference
        await user_collection.data.reference_add(
            from_uuid=user_uuid,
            from_property='liked_shorts',
            to=short_uuid
        )

        resp = await user_collection.query.fetch_object_by_id(
            uuid=user_uuid,
            include_vector=True,
            return_references=QueryReference(
                link_on='liked_shorts',
                return_properties=["object_id"],
            ),
        )

        print(resp)

    finally:
        await client.close()

if __name__ == '__main__':
    asyncio.run(main())

got this output:

created short: 09e06d77-a1f6-4274-b0ae-485f532322fd
created user: 0c6ff736-ef4a-45e9-9e34-93ff67a53dfe
ObjectSingleReturn(uuid=_WeaviateUUIDInt('0c6ff736-ef4a-45e9-9e34-93ff67a53dfe'), metadata=MetadataSingleObjectReturn(creation_time=datetime.datetime(2025, 1, 6, 15, 20, 12, 837000, tzinfo=datetime.timezone.utc), last_update_time=datetime.datetime(2025, 1, 6, 15, 20, 12, 840000, tzinfo=datetime.timezone.utc), is_consistent=None), properties={'object_id': 1}, references={'liked_shorts': <weaviate.collections.classes.internal._CrossReference object at 0x7c28da11e610>}, vector={}, collection='UserInteractions')

i use python 3.11 and weaviate-client 4.9.6
for me asynchronicity is important, so I can’t downgrade to the old 3rd client, where it is not supported, even if there are more examples
thank you for your reply

p.s. I’m working on a recommendation system