Skip to content

Commit

Permalink
Merge pull request #1963 from SciPhi-AI/patch/serialization-issue
Browse files Browse the repository at this point in the history
fix serialization
  • Loading branch information
emrgnt-cmplxty authored Feb 11, 2025
2 parents 1f582da + f6786c1 commit dc6f0d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
29 changes: 27 additions & 2 deletions py/core/main/services/retrieval_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import time
import uuid
from copy import deepcopy
from datetime import datetime
from typing import Any, Optional
Expand Down Expand Up @@ -54,10 +55,29 @@
import tiktoken


def convert_uuids(obj):
"""
Recursively convert UUID instances within the object into strings.
Handles dict, list, tuple, and set.
"""
if isinstance(obj, dict):
return {k: convert_uuids(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_uuids(item) for item in obj]
elif isinstance(obj, tuple):
return tuple(convert_uuids(item) for item in obj)
elif isinstance(obj, set):
return {convert_uuids(item) for item in obj}
elif isinstance(obj, uuid.UUID):
return str(obj)
else:
return obj


def dump_collector(collector: SearchResultsCollector) -> list[dict[str, Any]]:
dumped = []
for source_type, result_obj, _ in collector.get_all_results():
# Try to convert the result_obj to a dict.
# Get the dictionary from the result object
if hasattr(result_obj, "model_dump"):
result_dict = result_obj.model_dump()
elif hasattr(result_obj, "dict"):
Expand All @@ -66,6 +86,10 @@ def dump_collector(collector: SearchResultsCollector) -> list[dict[str, Any]]:
result_dict = (
result_obj # Fallback if no conversion method is available
)

# Use the recursive conversion on the entire dictionary
result_dict = convert_uuids(result_dict)

dumped.append(
{
"source_type": source_type,
Expand Down Expand Up @@ -974,7 +998,7 @@ async def stream_response():

# 4) Persist everything in the conversation DB
await self.providers.database.conversations_handler.add_message(
conversation_id=conversation_id,
conversation_id=str(conversation_id),
content=assistant_message,
parent_id=message_id,
metadata={
Expand All @@ -985,6 +1009,7 @@ async def stream_response():
),
},
)

if needs_conversation_name:
conversation_name = None
try:
Expand Down
2 changes: 0 additions & 2 deletions py/sdk/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ def _make_request(

try:
response = self.client.request(method, url, **request_args)
print("response =", response)

self._handle_response(response)

if "application/json" in response.headers.get("Content-Type", ""):
Expand Down

0 comments on commit dc6f0d4

Please sign in to comment.