Nigou Julien commited on
Commit
9c8d6f1
·
1 Parent(s): b4bc906

Fix Langfuse observation tracing

Browse files
gaia_agent/observability.py CHANGED
@@ -45,8 +45,10 @@ def traced_step(trace: Any, name: str, fn: Callable[[], dict[str, Any]]) -> dict
45
  span = trace.start_observation(name=name, as_type="span")
46
  try:
47
  output = fn()
48
- span.end(output=output)
 
49
  return output
50
  except Exception as exc:
51
- span.end(level="ERROR", status_message=str(exc))
 
52
  raise
 
45
  span = trace.start_observation(name=name, as_type="span")
46
  try:
47
  output = fn()
48
+ span.update(output=output)
49
+ span.end()
50
  return output
51
  except Exception as exc:
52
+ span.update(level="ERROR", status_message=str(exc))
53
+ span.end()
54
  raise
tests/test_observability.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gaia_agent.observability import traced_step
4
+
5
+
6
+ class FakeSpan:
7
+ def __init__(self):
8
+ self.updates = []
9
+ self.ended = False
10
+
11
+ def update(self, **kwargs):
12
+ self.updates.append(kwargs)
13
+
14
+ def end(self):
15
+ self.ended = True
16
+
17
+
18
+ class FakeTrace:
19
+ def __init__(self):
20
+ self.span = FakeSpan()
21
+
22
+ def start_observation(self, name, as_type):
23
+ self.name = name
24
+ self.as_type = as_type
25
+ return self.span
26
+
27
+
28
+ def test_traced_step_records_output_then_ends_span():
29
+ trace = FakeTrace()
30
+
31
+ output = traced_step(trace, "step", lambda: {"answer": "Paris"})
32
+
33
+ assert output == {"answer": "Paris"}
34
+ assert trace.name == "step"
35
+ assert trace.as_type == "span"
36
+ assert trace.span.updates == [{"output": {"answer": "Paris"}}]
37
+ assert trace.span.ended is True
38
+
39
+
40
+ def test_traced_step_records_error_then_reraises():
41
+ trace = FakeTrace()
42
+
43
+ with pytest.raises(ValueError):
44
+ traced_step(trace, "step", lambda: (_ for _ in ()).throw(ValueError("bad")))
45
+
46
+ assert trace.span.updates == [{"level": "ERROR", "status_message": "bad"}]
47
+ assert trace.span.ended is True