Skip to main content

A minimal library for capturing ML/NLP operation traces for later training data export

Project description

oplogger

A minimal library for capturing ML/NLP operation traces for later training data export.

Installation

pip install oplogger

# With PostgreSQL support
pip install oplogger[postgres]

# With export features (pandas, HuggingFace datasets)
pip install oplogger[export]

Note: The package is installed as oplogger but imported as oplog.

Quick Start

from oplog import configure, op, run, db, export

# Configure once at startup
configure(project="my_project", backend="sqlite:///traces.db")

# Log standalone operations
op("classify") \
    .model("setfit-intent") \
    .input(text="hello world") \
    .output(label="greeting", score=0.95) \
    .save()

# Log grouped operations within a run (with run-level metadata for A/B testing)
with run(strategy="rerank_v2", experiment="exp_042") as r:
    op("retrieve") \
        .model("bge-m3") \
        .input(query="capital of France?", k=10) \
        .output(candidates=["Paris is the capital..."]) \
        .save()

    op("rerank") \
        .model("bge-reranker-base") \
        .input(query="capital of France?", candidates=[...]) \
        .output(ranked=["Paris is the capital..."], scores=[0.94]) \
        .meta(latency_ms=42) \
        .save()
    # A run row is created on entry; its meta lives there (not on the ops).
    # Data discovered mid-run can be appended: r.add_meta(outcome="ok")

# Flag the run itself (lands on the run row, not its operations)
db.flag(run_id=r.id, reason="training", note="clean example")

# Query runs
flagged_runs = db.runs(flagged_for="training")
run_row = db.get_run(r.id)

# Query and export
records = db.query(operation="rerank", flagged_for="training")
export.to_jsonl(records, "training_data.jsonl")

API Reference

Configuration

configure(project="name", backend="sqlite:///traces.db")

Backend formats:

  • SQLite: sqlite:///path/to/traces.db (auto-creates file and parent directories)
  • PostgreSQL: postgresql://user:pass@host:port/dbname

Operations

op("operation_type")        # Start building an operation
    .model("model-name")    # Model identifier
    .input(**kwargs)        # Input data (JSON)
    .output(**kwargs)       # Output data (JSON)
    .meta(**kwargs)         # Metadata (latency, tokens, etc.)
    .tags("tag1", "tag2")   # Categorical tags
    .save()                 # Persist and return operation ID

Runs

with run() as r:            # Auto-generated run ID
    op(...).save()          # seq=0
    op(...).save()          # seq=1
    print(r.id)             # Access run ID

with run("custom-id"):      # Explicit run ID
    ...

# Run-level metadata (propagates to all operations in the run)
with run(strategy="methodA", experiment_id="exp123") as r:
    op("test").save()                      # meta={"strategy": "methodA", "experiment_id": "exp123"}
    op("test").meta(latency_ms=42).save()  # meta includes both run + op metadata

Run metadata is merged with operation metadata. Operation-level values override run-level on conflicts.

Database Operations

# Query
records = db.query(
    operation="rerank",     # Filter by operation type
    model="model-name",     # Filter by model
    run_id="...",           # Filter by run
    flagged_for="training", # Filter by flag
    tags=["tag1", "tag2"],  # Filter by tags (AND logic)
    limit=100,              # Pagination
    offset=0,
)

# Runs (first-class rows: run-level meta, flags, and notes live here)
run_row = db.get_run("...")
rows = db.runs(project="...", flagged_for="review", limit=50)

# Flag — ids target operation rows; run_id targets the run row itself
db.flag(ids=[...], reason="training", note="optional note")
db.flag(run_id="...", reason="review", note="odd resolution")

# Unflag
db.unflag(ids=[...])
db.unflag(run_id="...")

Export

# JSONL
export.to_jsonl(records, "output.jsonl")

# CSV
export.to_csv(records, "output.csv")

# pandas DataFrame
df = export.to_dataframe(records)

# HuggingFace Dataset
dataset = export.to_dataset(records)

# Field selection (dot notation for nested fields)
export.to_jsonl(records, "output.jsonl", fields=["inputs.query", "outputs.score"])

Multi-Tracer Usage

For multiple projects or explicit control:

from oplog import Tracer

tracer = Tracer(project="my_project", backend="sqlite:///traces.db")

tracer.op("classify").input(...).save()

with tracer.run() as r:
    tracer.op("rerank").input(...).save()

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oplogger-0.2.1.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oplogger-0.2.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file oplogger-0.2.1.tar.gz.

File metadata

  • Download URL: oplogger-0.2.1.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for oplogger-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f0e5e7c200f06f3d9fe602c59fc4a56c1d687a5942d2af7e452f758042613692
MD5 5002ad3e8503f1e14ca070be1d14980a
BLAKE2b-256 f20ac309f1eb81aa1814fb6a1a76000e31d4d7381d2ac12ea784798aaab29fa3

See more details on using hashes here.

File details

Details for the file oplogger-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: oplogger-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for oplogger-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 64cbf7d7a0268189dd9291a0055d165e3e84102d152aa588f38b017e6cd38e9c
MD5 855d48567b3db0181243eb4eb60241da
BLAKE2b-256 aa3989585e581e4799057186537ea377c4c2fe46cb318d029979e88e0a7ebb4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page