fix(langfuse): redact base64 data URIs instead of truncating into invalid base64
The Langfuse SDK treats `data:*;base64,...` strings as media and tries to decode them. `_truncate_text` was slicing those strings mid-payload, producing invalid base64 and noisy "Error parsing base64 data URI" logs. Observability only needs the metadata, not raw image/audio bytes, so redact the whole data URI (type, media_type, length) before it reaches the SDK. Salvaged the Langfuse fix from #39682 onto current main as a standalone, single-concern change (the dashboard `dist/**` and plugin-discovery parts of that PR already landed separately on main). Co-authored-by: foras910521-lab <foras910521-lab@users.noreply.github.com>
This commit is contained in:
co-authored by
foras910521-lab
parent
bf7abc2f73
commit
4642762289
@@ -171,6 +171,40 @@ class TestHooksInert:
|
||||
mod.on_post_tool_call(tool_name="read_file", args={}, result="ok", task_id="t", session_id="s")
|
||||
|
||||
|
||||
class TestPayloadSanitization:
|
||||
def test_safe_value_redacts_base64_data_uri_instead_of_truncating(self):
|
||||
sys.modules.pop("plugins.observability.langfuse", None)
|
||||
import importlib
|
||||
mod = importlib.import_module("plugins.observability.langfuse")
|
||||
|
||||
payload = "data:image/png;base64," + ("a" * 20000)
|
||||
result = mod._safe_value(payload)
|
||||
|
||||
assert result == {
|
||||
"type": "data_uri",
|
||||
"media_type": "image/png",
|
||||
"omitted": True,
|
||||
"length": len(payload),
|
||||
}
|
||||
|
||||
def test_serialize_messages_redacts_data_uri_parts(self):
|
||||
sys.modules.pop("plugins.observability.langfuse", None)
|
||||
import importlib
|
||||
mod = importlib.import_module("plugins.observability.langfuse")
|
||||
|
||||
payload = "data:image/jpeg;base64," + ("b" * 20000)
|
||||
serialized = mod._serialize_messages([
|
||||
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": payload}}]}
|
||||
])
|
||||
|
||||
assert serialized[0]["content"][0]["image_url"]["url"] == {
|
||||
"type": "data_uri",
|
||||
"media_type": "image/jpeg",
|
||||
"omitted": True,
|
||||
"length": len(payload),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Placeholder-credential guard (#23823).
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user