Skip to content

Commit 843ed96

Browse files
RiviaAzusaclaude
andcommitted
fix: 修复中文字符在LangChain CallbackHandler中被过度转义的问题
在所有json.dumps调用中添加ensure_ascii=False参数,确保中文等非ASCII字符不会被转义为\uXXXX格式。 修改的文件: - langfuse/_client/attributes.py: 核心序列化函数 - langfuse/_utils/request.py: API请求数据序列化 - langfuse/_client/utils.py: 调试输出格式化 - langfuse/_task_manager/score_ingestion_consumer.py: 分数处理序列化 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent fed7240 commit 843ed96

4 files changed

Lines changed: 5 additions & 4 deletions

File tree

langfuse/_client/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _serialize(obj: Any) -> Optional[str]:
164164
if obj is None or isinstance(obj, str):
165165
return obj
166166

167-
return json.dumps(obj, cls=EventSerializer)
167+
return json.dumps(obj, cls=EventSerializer, ensure_ascii=False)
168168

169169

170170
def _flatten_and_serialize_metadata(

langfuse/_client/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def span_formatter(span: ReadableSpan) -> str:
5555
"instrumentationScope": instrumentationScope,
5656
},
5757
indent=2,
58+
ensure_ascii=False,
5859
)
5960
+ "\n"
6061
)

langfuse/_task_manager/score_ingestion_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _next(self) -> list:
8585

8686
# check for serialization errors
8787
try:
88-
json.dumps(event, cls=EventSerializer)
88+
json.dumps(event, cls=EventSerializer, ensure_ascii=False)
8989
except Exception as e:
9090
self._log.error(
9191
f"Data error: Failed to serialize score object for ingestion. Score will be dropped. Error: {e}"
@@ -117,7 +117,7 @@ def _next(self) -> list:
117117

118118
def _get_item_size(self, item: Any) -> int:
119119
"""Return the size of the item in bytes."""
120-
return len(json.dumps(item, cls=EventSerializer).encode())
120+
return len(json.dumps(item, cls=EventSerializer, ensure_ascii=False).encode())
121121

122122
def run(self) -> None:
123123
"""Run the consumer."""

langfuse/_utils/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def post(self, **kwargs: Any) -> httpx.Response:
6060
"""Post the `kwargs` to the API"""
6161
log = logging.getLogger("langfuse")
6262
url = self._remove_trailing_slash(self._base_url) + "/api/public/ingestion"
63-
data = json.dumps(kwargs, cls=EventSerializer)
63+
data = json.dumps(kwargs, cls=EventSerializer, ensure_ascii=False)
6464
log.debug("making request: %s to %s", data, url)
6565
headers = self.generate_headers()
6666
res = self._session.post(

0 commit comments

Comments
 (0)