Ragas (tracing)

Ragas is a framework for evaluating LLM and RAG applications. respan-instrumentation-ragas traces Ragas 0.4 metrics, dataset evaluations, and experiments as canonical Respan task spans, preserving the active workflow hierarchy around them.

The adapter covers both the preferred Ragas 0.4 metric collections API and the legacy evaluation surfaces. Nested internal calls are collapsed so one logical metric operation does not produce duplicate sync and async spans.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-ragas
2

Set the Respan API key

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

Metric-specific provider credentials are still required for metrics that call an LLM or embedding model. Deterministic metrics such as exact match do not require a provider key.

3

Initialize and score a sample

1from ragas.metrics.collections import ExactMatch, StringPresence
2from respan import Respan
3from respan_instrumentation_ragas import RagasInstrumentor
4
5respan = Respan(
6 app_name="ragas-answer-evaluation",
7 instrumentations=[RagasInstrumentor()],
8)
9
10try:
11 exact_match = ExactMatch().score(
12 reference="Paris",
13 response="Paris",
14 )
15 string_presence = StringPresence().score(
16 reference="Paris",
17 response="Paris, France",
18 )
19 print(exact_match.value, string_presence.value)
20finally:
21 respan.shutdown()
4

View your trace

Open the Traces page to inspect one task span per metric with its inputs, score, timing, and status.

Supported operations

Ragas surfaceTraced operation
Collection metricsscore, ascore, batch_score, and abatch_score
Metric base classesSingle-turn and multi-turn sync and async scoring
Dataset evaluationevaluate and aevaluate
ExperimentsExperiment arun plus each experiment row

All of these operations emit canonical Respan task spans. Successful spans include status_code=200. Failures are re-raised unchanged after recording OpenTelemetry error status, an exception event, backend-visible status_code, and error.message.

Async metrics

1from ragas.metrics.collections import ExactMatch
2
3
4async def score_answer() -> float:
5 result = await ExactMatch().ascore(
6 reference="Paris",
7 response="Paris",
8 )
9 return result.value

Batch metric methods are traced as one logical operation, without an extra span for each internal sync-to-async delegation.

Dataset evaluation

The legacy evaluate and aevaluate entry points remain supported:

1from ragas import EvaluationDataset, evaluate
2from ragas.metrics import ExactMatch, StringPresence
3
4dataset = EvaluationDataset.from_list(
5 [
6 {"response": "Paris", "reference": "Paris"},
7 {"response": "Lyon", "reference": "Paris"},
8 ]
9)
10result = evaluate(
11 dataset,
12 metrics=[ExactMatch(), StringPresence()],
13 experiment_name="capital_answers",
14 show_progress=False,
15)
16print(result.to_pandas())

Experiments and rows

Ragas experiments produce a task for the experiment run and a child task for every evaluated row:

1from ragas import Dataset, experiment
2from ragas.metrics.collections import ExactMatch
3
4
5@experiment()
6async def score_row(row: dict) -> dict:
7 score = await ExactMatch().ascore(
8 reference=row["reference"],
9 response=row["response"],
10 )
11 return {**row, "exact_match": score.value}
12
13
14async def run_experiment(dataset: Dataset) -> object:
15 return await score_row.arun(dataset, name="answer_quality")

Configuration

ParameterTypeDefaultDescription
capture_contentboolTrueCapture evaluation inputs, references, responses, scores, and outputs.
instrumentationslist[]Pass [RagasInstrumentor()] to activate Ragas tracing.
api_keystr | NoneNoneFalls back to RESPAN_API_KEY.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL.
customer_identifierstr | NoneNoneDefault customer identifier for all emitted spans.
metadatadict | NoneNoneDefault metadata attached to all emitted spans.

Disable content capture

1from respan import Respan
2from respan_instrumentation_ragas import RagasInstrumentor
3
4respan = Respan(
5 instrumentations=[RagasInstrumentor(capture_content=False)],
6)

With capture disabled, evaluation inputs and outputs are omitted. Operation names, duration, lifecycle, status, and exception type remain visible.

Group evaluations in a workflow

Use a Respan workflow or propagated attributes to group several Ragas operations into one trace:

1from respan import Respan, workflow
2
3
4@workflow(name="ragas_regression_suite")
5def run_regression_suite() -> dict[str, float]:
6 score = ExactMatch().score(reference="Paris", response="Paris")
7 return {"exact_match": score.value}
8
9
10with Respan.propagate_attributes(
11 trace_group_identifier="ragas_regression_suite",
12 metadata={"dataset": "capital-cities-v2"},
13):
14 results = run_regression_suite()