From ab18831580e46c43854e37a314e2bfff6c09f269 Mon Sep 17 00:00:00 2001 From: Eduard van Valkenburg Date: Mon, 6 Jan 2025 22:29:56 +0100 Subject: [PATCH] Python: change order used to check variants in (de)ser steps (#10051) ### Motivation and Context While reviewing the docs, noticed some illogical behavior, this fixes that. ### Description ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone :smile: --- .../vector_storage/vector_store_record_collection.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/semantic_kernel/data/vector_storage/vector_store_record_collection.py b/python/semantic_kernel/data/vector_storage/vector_store_record_collection.py index 2774ebfb2fd8..cf2d301bf956 100644 --- a/python/semantic_kernel/data/vector_storage/vector_store_record_collection.py +++ b/python/semantic_kernel/data/vector_storage/vector_store_record_collection.py @@ -518,10 +518,10 @@ def _serialize_data_model_to_dict(self, record: TModel, **kwargs: Any) -> OneOrM """ if self.data_model_definition.to_dict: return self.data_model_definition.to_dict(record, **kwargs) - if isinstance(record, BaseModel): - return self._serialize_vectors(record.model_dump()) if isinstance(record, ToDictMethodProtocol): return self._serialize_vectors(record.to_dict()) + if isinstance(record, BaseModel): + return self._serialize_vectors(record.model_dump()) store_model = {} for field_name in self.data_model_definition.field_names: @@ -611,14 +611,14 @@ def _deserialize_dict_to_data_model(self, record: OneOrMany[dict[str, Any]], **k "Cannot deserialize multiple records to a single record unless you are using a container." ) record = record[0] - if issubclass(self.data_model_type, BaseModel): - if include_vectors: - record = self._deserialize_vector(record) - return self.data_model_type.model_validate(record) # type: ignore if func := getattr(self.data_model_type, "from_dict", None): if include_vectors: record = self._deserialize_vector(record) return func(record) + if issubclass(self.data_model_type, BaseModel): + if include_vectors: + record = self._deserialize_vector(record) + return self.data_model_type.model_validate(record) # type: ignore data_model_dict: dict[str, Any] = {} for field_name in self.data_model_definition.fields: if not include_vectors and field_name in self.data_model_definition.vector_field_names: