Skip to content

Commit 1c6d7d5

Browse files
Merge branch 'main' into chore/invoke-attr
2 parents 958abdc + 9cfb7a0 commit 1c6d7d5

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
@@ -73,6 +73,25 @@ async def on_user_message_callback(
7373

7474
return None
7575

76+
77+
def replace_bytes_with_empty(data):
78+
"""
79+
Recursively traverse the data structure and replace all bytes types with empty strings.
80+
Supports handling any nested structure of lists and dictionaries.
81+
"""
82+
if isinstance(data, dict):
83+
# Handle dictionary: Recursively process each value
84+
return {k: replace_bytes_with_empty(v) for k, v in data.items()}
85+
elif isinstance(data, list):
86+
# Handle list: Recursively process each element
87+
return [replace_bytes_with_empty(item) for item in data]
88+
elif isinstance(data, bytes):
89+
# When encountering the bytes type, replace it with an empty string
90+
return "<image data>"
91+
else:
92+
# Keep other types unchanged
93+
return data
94+
7695

7796
class BaseTracer(ABC):
7897
def __init__(self, name: str):
@@ -164,8 +183,11 @@ def tracer_hook_after_model(
164183
role = getattr(user_content, "role", None)
165184

166185
if user_content and getattr(user_content, "parts", None):
186+
# content = user_content.model_dump_json(exclude_none=True)
167187
content = user_content.model_dump(exclude_none=True).get("parts", None)
168-
content = json.dumps(content) if content else None
188+
if content:
189+
content = replace_bytes_with_empty(content)
190+
content = json.dumps(content, ensure_ascii=False) if content else None
169191

170192
if role and content:
171193
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)