Exporting experiment results

Export a full experiment result set to CSV with the Python SDK
View as Markdown

In Opik 2.0, datasets and experiments are project-scoped. Pass a project_name when looking up an experiment by name so it resolves in the right project.

The experiments comparison page exports what you are looking at: select rows to export those, or export the whole filtered result set with nothing selected. That file is built in your browser, so it is capped at 2,000 rows.

Above that, export with the SDK. It reads the result set through the API, so there is no practical size limit, and the script below produces the same columns the comparison page does.

Exporting an experiment to CSV

One row per dataset item, with the dataset fields, the evaluated output and every feedback score as its own column — the shape the comparison page exports.

Python
import csv
import opik
client = opik.Opik()
experiment = client.get_experiments_by_name(
name="my-experiment",
project_name="my-project",
)[0]
rows = []
for item in experiment.get_items(max_results=100_000):
row = {f"dataset.{key}": value for key, value in (item.dataset_item_data or {}).items()}
row["output"] = item.evaluation_task_output
for score in item.feedback_scores:
row[f"feedback_scores.{score['name']}"] = score["value"]
rows.append(row)
# Columns are data-driven - dataset fields and score names vary per item - so collect them from the rows.
columns = sorted({key for row in rows for key in row})
with open("experiment-results.csv", "w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=columns)
writer.writeheader()
writer.writerows(rows)

If you have the experiment’s id — from its URL, for instance — client.get_experiment_by_id("<id>") fetches it directly and needs no project name, since the id already identifies it. get_experiments_by_name returns every experiment whose name contains the string you pass, which is why the example above takes the first result.

What each item carries

FieldDescription
idThe experiment item id
dataset_item_idThe dataset item this result belongs to, stable across experiments on the same dataset
trace_idThe trace produced by the evaluation task
dataset_item_dataThe dataset item’s fields, as a dictionary
evaluation_task_outputWhat the evaluated application returned
feedback_scoresScores on the item, each with a name, value and optional reason
assertion_resultsAssertion outcomes, when the experiment defines assertions

Two differences from the comparison page are worth knowing. Score reasons are available here as score["reason"], which the page exports as a separate feedback_scores.<name>_reason column — add it the same way if you want it. Duration, token usage and estimated cost are not part of an experiment item: they belong to the trace, so fetch the trace by trace_id when you need them.

max_results defaults to 10,000, so raise it for a larger experiment. Values come back untruncated and the whole result set is held in memory at once, so a very large experiment is worth exporting in slices, by filtering the dataset down.