Skip to content

Commit

Permalink
Python: change order used to check variants in (de)ser steps (#10051)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
While reviewing the docs, noticed some illogical behavior, this fixes
that.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [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 😄
  • Loading branch information
eavanvalkenburg authored Jan 6, 2025
1 parent 8751cbc commit ab18831
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ab18831

Please sign in to comment.