Skip to content

Commit 9cfb7a0

Browse files
authored
fix: fix bug of image data cannot be traced (#88)
* fix: fix bug of image data cannot be traced * fix: add mask
1 parent e30983c commit 9cfb7a0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

veadk/tracing/base_tracer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@
2727
logger = get_logger(__name__)
2828

2929

30+
def replace_bytes_with_empty(data):
31+
"""
32+
Recursively traverse the data structure and replace all bytes types with empty strings.
33+
Supports handling any nested structure of lists and dictionaries.
34+
"""
35+
if isinstance(data, dict):
36+
# Handle dictionary: Recursively process each value
37+
return {k: replace_bytes_with_empty(v) for k, v in data.items()}
38+
elif isinstance(data, list):
39+
# Handle list: Recursively process each element
40+
return [replace_bytes_with_empty(item) for item in data]
41+
elif isinstance(data, bytes):
42+
# When encountering the bytes type, replace it with an empty string
43+
return "<image data>"
44+
else:
45+
# Keep other types unchanged
46+
return data
47+
48+
3049
class BaseTracer(ABC):
3150
def __init__(self, name: str):
3251
self.app_name = "veadk_app_name"
@@ -117,8 +136,11 @@ def tracer_hook_after_model(
117136
role = getattr(user_content, "role", None)
118137

119138
if user_content and getattr(user_content, "parts", None):
139+
# content = user_content.model_dump_json(exclude_none=True)
120140
content = user_content.model_dump(exclude_none=True).get("parts", None)
121-
content = json.dumps(content) if content else None
141+
if content:
142+
content = replace_bytes_with_empty(content)
143+
content = json.dumps(content, ensure_ascii=False) if content else None
122144

123145
if role and content:
124146
attributes["gen_ai.prompt.0.role"] = role

veadk/tracing/telemetry/opentelemetry_tracer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def dump(
179179
self._trace_id = trace_id
180180
file_path = f"{path}/{self.name}_{user_id}_{session_id}_{trace_id}.json"
181181
with open(file_path, "w") as f:
182-
json.dump(data, f, indent=4)
182+
json.dump(
183+
data, f, indent=4, ensure_ascii=False
184+
) # ensure_ascii=False to support Chinese characters
183185

184186
self._trace_file_path = file_path
185187

0 commit comments

Comments
 (0)