|
27 | 27 | logger = get_logger(__name__) |
28 | 28 |
|
29 | 29 |
|
| 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 | + |
30 | 49 | class BaseTracer(ABC): |
31 | 50 | def __init__(self, name: str): |
32 | 51 | self.app_name = "veadk_app_name" |
@@ -117,8 +136,11 @@ def tracer_hook_after_model( |
117 | 136 | role = getattr(user_content, "role", None) |
118 | 137 |
|
119 | 138 | if user_content and getattr(user_content, "parts", None): |
| 139 | + # content = user_content.model_dump_json(exclude_none=True) |
120 | 140 | 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 |
122 | 144 |
|
123 | 145 | if role and content: |
124 | 146 | attributes["gen_ai.prompt.0.role"] = role |
|
0 commit comments