Concurrent component evaluations share the @observe metric object, so one case's score is recorded for another
Describe the bug
In the documented component-level workflow (@observe(metrics=[...]), dataset.evals_iterator(), dataset.evaluate(asyncio.create_task(app(...)))), the metric object given to the decorator is shared by every call of that component. _a_execute_span_test_case in deepeval/evaluate/execute/agentic.py does list(span.metrics or []), which copies the list but not the metric objects, and metric evaluation awaits a gather before the result is captured. Under the default concurrency, one span's measurement overwrites the shared object's score before another span records it, so a case can be reported with actualOutput="wrong", expectedOutput="correct", score=1.0, success=true, reason "The actual and expected outputs are exact matches."
No judge is involved: this reproduces with ExactMatchMetric. Present at 86b6fb5 (current main) and in the deepeval==4.2.5 wheel from PyPI.
To Reproduce
import asyncio, os
from deepeval.dataset import EvaluationDataset, Golden
from deepeval.metrics import ExactMatchMetric
from deepeval.tracing import observe, update_current_span
from deepeval.test_case import LLMTestCase
from deepeval.test_run import global_test_run_manager
@observe(metrics=[ExactMatchMetric()])
async def component(answer):
update_current_span(test_case=LLMTestCase(input=answer, actual_output=answer, expected_output="correct"))
return answer
@observe()
async def app(answer):
return await component(answer)
data = EvaluationDataset(goldens=[Golden(input=x) for x in os.environ.get("ANSWERS", "wrong,correct").split(",")])
for golden in data.evals_iterator():
data.evaluate(asyncio.create_task(app(golden.input)))
run = global_test_run_manager.get_test_run()
print("passed", run.test_passed, "failed", run.test_failed)ANSWERS=wrong,correct python repro.py # passed 2 failed 0, expected 1 and 1
ANSWERS=correct,wrong python repro.py # passed 0 failed 2, expected 1 and 1| Cases in dispatch order | Expected passes | Reported, default concurrency | Reported, concurrency 1 |
|---|---|---|---|
| wrong, correct | 1/2 | 2/2 | 1/2 |
| correct, wrong | 1/2 | 0/2 | 1/2 |
| 19 distinct wrong answers, then correct | 1/20 | 2/20 | not tested |
| correct, then 19 distinct wrong answers | 1/20 | 0/20 | not tested |
Each of the six configurations ran three times on both sources: all 24 concurrent runs had wrong totals, all 12 serial controls were right, and every requested case was recorded. Each app call has one top-level observed function with one observed component, so this is not the multi-root omission in #2599.
Expected behavior
Each span's metric result reflects that span's own test case. Cloning the span metrics with copy_metrics in _a_execute_span_test_case (the same clone the end-to-end path already does, and the same idea as #2559, which fixed trace-level metrics passed to _a_evaluate_traces but does not reach metrics attached to @observe spans) gives the right totals in all six configurations, three runs each. That is evidence of cause; copy_metrics rebuilds a metric from its constructor arguments, so custom metrics with state set outside __init__ need a look before it ships.
Desktop (please complete the following information):
- OS: macOS and Linux
- Python 3.12
- deepeval 4.2.5 (PyPI wheel) and main at 86b6fb5
Additional context Searched issues and PRs for shared decorator and component metric scores; #2559 (trace-level metric sharing, merged), #2616 (locks around trace and span collections) and #2828 (TypeScript tracing performance) are related but different; #2599 is the multi-root omission. Apologies if it was reported somewhere I did not look.
A PR with the span-path clone and a regression test that runs two goldens concurrently through @observe(metrics=[ExactMatchMetric()]) and asserts one pass and one failure in both orders follows this issue.
Disclosure: I maintain Driftproof, an evaluation tool. This came from a direct read of the evaluator and the reproduction above, not from any tool.
Source: confident-ai/deepeval