Skip to content

Commit

Permalink
feat: remove bytes objects from steps (#969)
Browse files Browse the repository at this point in the history
* feat: remove bytes objects from steps

* feat: process list and tuples
  • Loading branch information
willydouhard authored May 7, 2024
1 parent 27cd74c commit 2cd9a0f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion backend/chainlit/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,34 @@ def __init__(
self.persisted = False
self.fail_on_persist_error = False

def _clean_content(self, content):
"""
Recursively checks and converts bytes objects in content.
"""

def handle_bytes(item):
if isinstance(item, bytes):
return "STRIPPED_BINARY_DATA"
elif isinstance(item, dict):
return {k: handle_bytes(v) for k, v in item.items()}
elif isinstance(item, list):
return [handle_bytes(i) for i in item]
elif isinstance(item, tuple):
return tuple(handle_bytes(i) for i in item)
return item

return handle_bytes(content)

def _process_content(self, content, set_language=False):
if content is None:
return ""
if isinstance(content, dict):
content = self._clean_content(content)

if (
isinstance(content, dict)
or isinstance(content, list)
or isinstance(content, tuple)
):
try:
processed_content = json.dumps(content, indent=4, ensure_ascii=False)
if set_language:
Expand Down

0 comments on commit 2cd9a0f

Please sign in to comment.