Skip to content
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
24 changes: 23 additions & 1 deletion veadk/tracing/base_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@
logger = get_logger(__name__)


def replace_bytes_with_empty(data):
"""
Recursively traverse the data structure and replace all bytes types with empty strings.
Supports handling any nested structure of lists and dictionaries.
"""
if isinstance(data, dict):
# Handle dictionary: Recursively process each value
return {k: replace_bytes_with_empty(v) for k, v in data.items()}
elif isinstance(data, list):
# Handle list: Recursively process each element
return [replace_bytes_with_empty(item) for item in data]
elif isinstance(data, bytes):
# When encountering the bytes type, replace it with an empty string
return "<image data>"
else:
# Keep other types unchanged
return data


class BaseTracer(ABC):
def __init__(self, name: str):
self.app_name = "veadk_app_name"
Expand Down Expand Up @@ -117,8 +136,11 @@ def tracer_hook_after_model(
role = getattr(user_content, "role", None)

if user_content and getattr(user_content, "parts", None):
# content = user_content.model_dump_json(exclude_none=True)
content = user_content.model_dump(exclude_none=True).get("parts", None)
content = json.dumps(content) if content else None
if content:
content = replace_bytes_with_empty(content)
content = json.dumps(content, ensure_ascii=False) if content else None

if role and content:
attributes["gen_ai.prompt.0.role"] = role
Expand Down
4 changes: 3 additions & 1 deletion veadk/tracing/telemetry/opentelemetry_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def dump(
self._trace_id = trace_id
file_path = f"{path}/{self.name}_{user_id}_{session_id}_{trace_id}.json"
with open(file_path, "w") as f:
json.dump(data, f, indent=4)
json.dump(
data, f, indent=4, ensure_ascii=False
) # ensure_ascii=False to support Chinese characters

self._trace_file_path = file_path

Expand Down