Skip to content

Commit 4cc538a

Browse files
committed
test
1 parent 85eea53 commit 4cc538a

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

weave/trace_server_bindings/caching_middleware_trace_server.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import hashlib
4+
import json
45
import logging
56
from collections.abc import Iterator
67
from typing import Any, Callable, TypedDict, TypeVar
@@ -67,8 +68,6 @@ def _safe_cache_key_for_binary_data(data: Any) -> str:
6768
return data
6869
else:
6970
# For other types, try JSON serialization
70-
import json
71-
7271
return json.dumps(data, sort_keys=True, default=str)
7372
except (UnicodeDecodeError, UnicodeEncodeError, TypeError, ValueError):
7473
# If we can't serialize it, create a hash of its string representation
@@ -83,28 +82,28 @@ def _create_obj_create_cache_key(req: tsi.ObjCreateReq) -> str:
8382
val = obj_dict.pop("val", None)
8483

8584
# Serialize everything except val
86-
import json
87-
8885
base_key = json.dumps(obj_dict, sort_keys=True)
8986

9087
# Handle val separately as it might contain binary data
9188
val_key = _safe_cache_key_for_binary_data(val)
9289

93-
return f"{base_key}|val:{val_key}"
9490
except Exception:
9591
# Fallback to a hash of the entire request
9692
return hashlib.sha256(str(req).encode("utf-8", errors="ignore")).hexdigest()
93+
else:
94+
return f"{base_key}|val:{val_key}"
9795

9896

9997
def _create_file_create_cache_key(req: tsi.FileCreateReq) -> str:
10098
"""Create a cache key for FileCreateReq that handles binary content safely."""
10199
try:
102100
# Create key from project_id and name, plus hash of content
103101
content_hash = _safe_cache_key_for_binary_data(req.content)
104-
return f"project:{req.project_id}|name:{req.name}|content:{content_hash}"
105102
except Exception:
106103
# Fallback to a hash of the entire request
107104
return hashlib.sha256(str(req).encode("utf-8", errors="ignore")).hexdigest()
105+
else:
106+
return f"project:{req.project_id}|name:{req.name}|content:{content_hash}"
108107

109108

110109
class CachingMiddlewareTraceServer(tsi.TraceServerInterface):

0 commit comments

Comments
 (0)