Skip to content
Python

fix(observability): a changed rating clears the earlier note in Langfuse

Devika Verma75b4ecbcfb29
diff --git a/coworker/observability/export_spans.py b/coworker/observability/export_spans.py
index dd12e072..1f5af7bb 100644
--- a/coworker/observability/export_spans.py
+++ b/coworker/observability/export_spans.py
@@ -805,8 +805,9 @@ def langfuse_score(fb: dict[str, Any], *, sw: dict[str, bool], masker: Masker) -
"name": FEEDBACK_SCORE, "value": 1 if fb["rating"] == "up" else -1,
"dataType": "NUMERIC", "metadata": {"reasons": list(fb.get("reasons") or [])}}
note = (fb.get("content") or {}).get("note")
- if note and sw.get("prompts"):
- body["comment"] = cut(masker.clean(note))
+ # Always sent, empty when there is no note to send: Langfuse merges a score sent again
+ # under the same id, so a left-out comment would keep an earlier rating's note there.
+ body["comment"] = cut(masker.clean(note)) if note and sw.get("prompts") else ""
return body
diff --git a/tests/test_observability_phase3_engine.py b/tests/test_observability_phase3_engine.py
index 55a6815a..8a0b5fbd 100644
--- a/tests/test_observability_phase3_engine.py
+++ b/tests/test_observability_phase3_engine.py
@@ -291,7 +291,8 @@ def test_ratings_go_out_as_a_langfuse_score_and_an_otlp_span(tmp_path):
assert fb[0][0] == "http://lf.test/api/public/scores"
assert body["name"] == "user_feedback" and body["value"] == -1
assert body["id"] == spans.feedback_id("T1") and body["traceId"] == spans.trace_id("T1")
- assert body["metadata"] == {"reasons": ["did_unasked"]} and "comment" not in body
+ # the note stays out without capture.prompts, and an empty comment replaces an earlier one
+ assert body["metadata"] == {"reasons": ["did_unasked"]} and body["comment"] == ""
else:
span = fb[0][1]["resourceSpans"][0]["scopeSpans"][0]["spans"][0]
attrs = {a["key"]: a["value"] for a in span["attributes"]}