Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Explodinggradients Ragas OpikTracer Class

From Leeroopedia


Metadata Value
Source src/ragas/integrations/opik.py (Lines 22-67)
Domains Integration, Observability
Last Updated 2026-02-10

Overview

A callback tracer that extends the Opik LangChain integration to log Ragas evaluation traces and feedback scores to the Opik observability platform.

Description

OpikTracer extends opik.integrations.langchain.OpikTracer (aliased as LangchainOpikTracer) to customize trace handling specifically for Ragas evaluation runs. It overrides three key methods:

  • _process_start_trace: Detects the top-level Ragas evaluation chain (identified by RAGAS_EVALUATION_CHAIN_NAME and having no parent run). When found, it stores the evaluation run ID in _evaluation_run_id. For child runs that are direct descendants of the evaluation chain, it sets parent_run_id to None so they are logged as independent top-level traces in Opik rather than nested under the evaluation umbrella.
  • _process_end_trace: When a trace completes (excluding the evaluation chain itself), it checks if the run name starts with "row " (indicating an individual evaluation row). For such runs, it extracts the output scores and logs them as feedback scores to Opik via _opik_client.log_traces_feedback_scores, rounding values to 4 decimal places.
  • _persist_run: Filters out the top-level evaluation chain run from persistence, ensuring only individual row traces are stored in Opik.

The class accepts tags (list of strings) and metadata (dictionary) attributes inherited from the parent tracer, which are applied to each logged trace.

Usage

Use OpikTracer when you want to log Ragas evaluation results to the Opik platform for observability, debugging, and analysis. Pass it as a callback to Ragas evaluation functions to automatically capture per-row traces and metric scores.

Code Reference

Source Location

Item Detail
File src/ragas/integrations/opik.py
Lines 22-67
Module ragas.integrations.opik

Class Signature

class OpikTracer(LangchainOpikTracer):
    _evaluation_run_id: Optional[str] = None

    def _process_start_trace(self, run: Run) -> None: ...
    def _process_end_trace(self, run: Run) -> None: ...
    def _persist_run(self, run: Run) -> None: ...

Import

from ragas.integrations.opik import OpikTracer

I/O Contract

Inherited Attributes

Name Type Description
tags list[str] Tags to set on each trace in Opik
metadata dict Additional metadata to log for each trace

Callback Methods

Method Input Behavior
_process_start_trace Run Identifies evaluation chain, detaches child runs for independent logging
_process_end_trace Run Logs metric scores as Opik feedback scores for row-level traces
_persist_run Run Persists all runs except the top-level evaluation chain

Feedback Score Format

Field Type Description
id str Trace ID from Opik
name str Metric name (from run outputs keys)
value float Metric score rounded to 4 decimal places

Usage Examples

Basic Usage with Ragas Evaluate

from ragas.integrations.opik import OpikTracer
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy

# Create the Opik tracer
opik_tracer = OpikTracer(
    tags=["rag-eval", "v1"],
    metadata={"experiment": "baseline"},
)

# Run Ragas evaluation with Opik tracing
results = evaluate(
    dataset=eval_dataset,
    metrics=[faithfulness, answer_relevancy],
    callbacks=[opik_tracer],
)

# Traces and scores are now visible in the Opik dashboard

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment