Skip to content

[BUGFIX] serialize-chat-fields #5553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion argilla/src/argilla/records/_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,18 @@ def __init__(self, record: Record, fields: Optional[Dict[str, FieldValue]] = Non
self.record = record

def to_dict(self) -> dict:
return {key: cast_image(value) if self._is_image(key) else value for key, value in self.items()}
fields = {}

for key, value in self.items():
if value is None:
continue
elif self._is_image(key):
fields[key] = cast_image(value)
elif self._is_chat(key):
fields[key] = [message.model_dump() if not isinstance(message, dict) else message for message in value]
else:
fields[key] = value
return fields
Comment on lines +305 to +314
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to review all the serialization logic when addressing #4944.

We have a serialization logic broken down by the code and we are not sure which will apply for each case.


def __getitem__(self, key: str) -> FieldValue:
value = super().__getitem__(key)
Expand All @@ -311,6 +322,11 @@ def _is_image(self, key: str) -> bool:
return False
return self.record.dataset.settings.schema[key].type == "image"

def _is_chat(self, key: str) -> bool:
if not self.record.dataset:
return False
return self.record.dataset.settings.schema[key].type == "chat"


class RecordMetadata(dict):
"""This is a container class for the metadata of a Record."""
Expand Down
19 changes: 19 additions & 0 deletions argilla/tests/integration/test_export_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def dataset(client) -> rg.Dataset:
fields=[
rg.TextField(name="text"),
rg.ImageField(name="image"),
rg.ChatField(name="chat"),
],
questions=[
rg.LabelQuestion(name="label", labels=["positive", "negative"]),
Expand All @@ -58,18 +59,36 @@ def mock_data() -> List[dict[str, Any]]:
{
"text": "Hello World, how are you?",
"image": "http://mock.url/image",
"chat": [
{
"role": "user",
"content": "Hello World, how are you?",
}
],
"label": "positive",
"id": uuid.uuid4(),
},
{
"text": "Hello World, how are you?",
"image": "http://mock.url/image",
"chat": [
{
"role": "user",
"content": "Hello World, how are you?",
}
],
"label": "negative",
"id": uuid.uuid4(),
},
{
"text": "Hello World, how are you?",
"image": "http://mock.url/image",
"chat": [
{
"role": "user",
"content": "Hello World, how are you?",
}
],
"label": "positive",
"id": uuid.uuid4(),
},
Expand Down
Loading
Loading