Trajectories

Store your production logs, labeled examples, and agent runs in a Dataset so any recipe can train on them.

A trajectory is a single recorded run: the input messages, what the model answered, any tool calls it made, and how well it did. You can fill a Dataset in five ways:

  1. Plain chat records pushed from code or a JSONL file, in the same {"messages": [...]} format as OpenAI fine-tuning files.
  2. Hugging Face datasets, imported by name.
  3. Automatic capture from the Gradiated gateway, through the built-in gradiated source (coming soon on Gradiated Cloud).
  4. File import from Parquet or JSONL trace exports.
  5. Scheduled sync from your observability provider.

Start from chat messages#

If you have conversations, you already have training data. Push plain chat records, one per example. The final assistant turn is the training target, and everything before it is the prompt. You don't need IDs, wrappers, or a schema:

Python
from grad import Grad

client = Grad()

dataset = client.datasets.push("support-sft", [
    {"messages": [
        {"role": "user", "content": "How do I reset my password?"},
        {"role": "assistant", "content": "Go to Settings → Security → Reset password, then check your email for the link."},
    ]},
    {"messages": [
        {"role": "system", "content": "You are a concise billing assistant."},
        {"role": "user", "content": "Can I get an invoice for last month?"},
        {"role": "assistant", "content": "Yes. Open Billing → Invoices and click Download next to October."},
    ]},
])
print(dataset.count())

client.datasets.push(name, ...) creates the dataset if it doesn't exist and appends otherwise. It accepts a list of dicts, a list of Trajectory objects, or a path to a .jsonl, .json or .parquet file. The format is detected automatically, so OpenAI chat fine-tuning files and grad.trajectory/1 exports both work. Each record can also carry these optional keys:

  • "tools": the tool definitions available in that conversation, in OpenAI function format. Assistant tool_calls and tool messages train as you'd expect.
  • "session_id": groups records from the same conversation so they always land on the same side of the train/eval split. One is generated per record if you leave it out.
  • "scores": any outcome you already know, such as {"tests_passed": 1.0}.
  • "timestamp": when the conversation happened (ISO 8601). Synced records carry one; set it yourself if you want to filter pushed records by date.

push with a list sends records as they are, and the server rejects keys it doesn't know (put your own fields under "metadata"). A file path goes through the importer, which drops them instead. OpenAI's per-message weight isn't read: mark a turn "train": false to keep it out of the loss, or "train": true to train an earlier assistant turn.

By default only the final assistant turn is trained. To train on every assistant turn of a multi-turn agent trace, pass train_on="all_assistant" to the recipe. Run grad render <dataset> --model qwen/qwen3-8b to see exactly which tokens will be trained; the other chat-template settings are in the reference.

Import a Hugging Face dataset#

Import any public or gated dataset from the Hugging Face Hub by name. The trainer worker downloads it, so nothing lands on your machine. If the dataset already has a messages column, nothing else is needed:

Python
from grad import Grad

client = Grad()

chats = client.datasets.from_hf("HuggingFaceH4/ultrachat_200k", split="train_sft")
print(chats.count())

For other layouts, map columns onto the fields a recipe needs:

Training dataColumn mapping
Chat (SFT)messages="messages" (the default)
Prompt and completion (SFT)prompt="question", completion="answer"
Preferences (DPO)prompt="prompt", chosen="chosen", rejected="rejected"
Tasks with a known answer (evals)prompt="question", answer="answer"
Python
math = client.datasets.from_hf(
    "openai/gsm8k",
    subset="main",
    split="train",
    prompt="question",
    answer="answer",
)

prefs = client.datasets.from_hf(
    "trl-lib/ultrafeedback_binarized",
    split="train",
    chosen="chosen",
    rejected="rejected",
)

answer becomes the record's expected_output and completion its answer turn. limit= imports only the first N rows. For gated or private datasets, store your Hugging Face token once with grad secrets set hf-token and pass secret=client.secrets.get("hf-token") (CLI: --secret hf-token).

Push trajectories with metadata#

When you need more than messages, such as a target that isn't the last turn, per-record scores, or cost data for a router, push Trajectory objects. Trajectory.from_messages(...) builds one from plain messages, and the last assistant turn stays the training target:

Python
from grad import Grad, Trajectory

client = Grad()

traj = Trajectory.from_messages(
    [
        {"role": "user", "content": "Merge these two diffs into one patch."},
        {"role": "assistant", "content": "--- a/app.py\n+++ b/app.py\n@@ -1 +1 @@\n-print('hi')\n+print('hello')"},
    ],
    session_id="pr_481",
)
traj.scores["tests_passed"] = 1.0

client.datasets.push("diff-merge-gold", [traj])

Import from files#

Upload JSONL, JSON or Parquet exports into a Dataset. Gradiated detects common formats (OpenAI chat JSONL, grad.trajectory/1, OpenTelemetry, Langfuse, Braintrust, etc.) and maps messages, tool calls, sessions, model names and scores automatically. Token counts and costs aren't extracted from provider exports; a router that needs metrics.cost_usd reads it from native records, or prices candidates with pricing=. Pass --format only if you want to pick a parser yourself.

Python
from grad import Grad

client = Grad()
dataset = client.datasets.create("support-prod")

job = dataset.import_file("./traces.parquet").wait()

print(job.imported_count, job.dropped_count, job.detected_scores)

The import result lists how many trajectories were parsed, how many were dropped, and every scores key detected in the file so you can check target coverage before training.

Score and filter datasets#

Filter a Dataset by field to get a new view of it. Field filters run on the server and call no model, so re-running one with a new threshold costs nothing:

Python
from grad import Grad

client = Grad()
dataset = client.datasets.get("support-prod")

recent_good = dataset.filter(scores={"resolved": ">=0.8"}, created_after="2026-11-01")
print(recent_good.count())

A score condition is an operator (>=, <=, >, <, ==, !=) and a number; a record without that score doesn't match. metadata={"env": "prod"} matches metadata fields exactly, and created_after / created_before compare the record timestamp (a record without one never matches). The view is saved as a dataset named <dataset>-f-<hash> (pass name= to choose one), so you can train or evaluate on it like any other dataset, and the same filter on the same version returns the same view.

If you want to filter by something a human or LLM needs to judge (like "did the assistant answer accurately?"), score the dataset once with a Judge. dataset.score(...) grades each record's own answer on the server, skips records that already have every score, and saves the results to trajectory.scores in the dataset's next version, so you can tweak thresholds or reuse the scores across runs without paying again. It is billed like an evaluation: judge tokens at the judge model's catalog price, and validators and regexes for free:

Python
from grad import Grad, Judge

client = Grad()
dataset = client.datasets.get("support-prod")

# Score once (server-side, persisted into `trajectory.scores["resolved"]`)
judge = Judge(
    name="resolved",
    model="anthropic/claude-sonnet-5-5",
    prompt="Did the assistant accurately resolve the user's request without hallucinating?",
)
scored = dataset.score(judge).wait()

# Filter on the stored score (server-side, $0 to re-run with a new threshold)
passing = scored.filter(scores={"resolved": ">=0.8"})
print(passing.count())

A scoring job is an eval with mode: "score": scored = dataset.score(...) returns before it finishes, and its progress and cost are visible with grad eval status <id>. Python Scorer functions work too; .wait() runs them on your machine, like an eval's local scorers. Records need an id to receive scores (push and imports assign one).

For logic a field filter can't express, pass any Python function (Trajectory) -> bool. The SDK streams the dataset to your machine, runs the function there once, and saves the selected IDs as a server-side view. Training on the view doesn't need your machine afterwards:

Python
from grad import Grad

client = Grad()
dataset = client.datasets.get("support-prod")

short_chats = dataset.filter(lambda t: len(t.input.messages) <= 6)
print(short_chats.count())

Sync from observability providers#

Connect an observability tool to a Dataset to pull in new traces and scores on a schedule. Save your provider key once as a workspace secret (grad secrets set langfuse-prod, or client.secrets.set(name, value)) and pass it via client.secrets.get(...): the value is encrypted on the server and never returned, so raw keys never end up in code, datasets or artifacts. A secret only goes to the hosts it allows: by default the public hosts of the supported providers, Hugging Face and W&B. For a self-hosted tool, name its host when you store the key (grad secrets set phoenix-prod --allowed-host phoenix.internal.example, or allowed_hosts=[...]). Naming a secret in a source, import or run logger needs the secrets:read scope. A source feeds one dataset; a dataset can have several sources.

Python
from grad import Grad, sources

client = Grad()
dataset = client.datasets.get("support-prod")
secret = client.secrets.get("langfuse-prod")

source = dataset.add_source(
    provider=sources.Langfuse(
        host="https://cloud.langfuse.com",
        secret=secret,
    ),
    schedule="*/15 * * * *",
)

source.sync()

How syncing works#

  • Incremental. Each sync fetches traces added since the last one, plus scores and corrections added to traces it already has, so late feedback still lands. Records are matched by the provider's trace ID and the latest score wins.
  • A little behind. Traces younger than 10 minutes wait for the next sync, because providers take a few minutes to expose new data. New feedback lands with the next sync; edits to feedback from the last hour are picked up within 15 minutes (each provider page says how it finds them), and older edits need a full sync.
  • Versioned. Syncs add dataset versions: a scheduled sync at most every version_interval (15 minutes by default), a sync you start yourself at once; see Versions. Old versions never change, so a run keeps training on the version it pinned.
  • Filtered. filters, sample_rate and record caps choose which traces a source syncs; see Choose which traces sync.
  • A sync is an import job. source.sync() returns the sync's import job without waiting; call .wait() on it to block until it lands (grad sources sync ID --wait, with --timeout SECS). A sync already in progress is returned, never a second one.
  • Rebuildable. grad sources sync ID --full (or source.sync(full=True)) rebuilds the dataset from the provider as a new version, for when scores were edited long ago or you changed the mapping.
  • Files are snapshots. Records from grad import or dataset.import_file(...) never get later feedback. Use a source for the feedback loop.
  • One dataset per source. A source feeds exactly one dataset; a dataset can have several sources, for example Langfuse for one app and PostHog for another. Deleting a dataset deletes its sources; grad sources remove ID disconnects a source and keeps the dataset.
  • At most once a minute. A schedule is a five-field UTC cron (no @hourly-style names), so the most often it can fire is * * * * *; a missed tick runs once at the next one and never back-fills. A sync within a minute of the last sync's start returns that sync instead of starting another (the API adds a Retry-After header), so repeating or retrying it is safe. A full sync you ask for while an incremental one runs starts as soon as that one ends.
  • Counts. A dataset's record_count is the number of merged records: score updates fold into the records they belong to, and a full sync restarts the count. Pushing the same records again is a no-op: a record without an id gets one from a hash of its content, and a batch already pushed to the dataset returns the current version instead of adding one, even when the same push arrives twice at once. grad import of a file the dataset already imported returns that import.

When a sync fails#

A failed sync leaves the dataset and its watermark as they were, so the next sync reads the same window again. It never holds up other sources or training runs. The source records what went wrong:

  • status is active or paused; last_error and last_error_at hold the last failure (provider keys are redacted), and consecutive_failures counts failures since the last success. A successful sync clears all three.
  • A scheduled source backs off while it fails: each failure doubles the wait before the next scheduled sync, up to an hour, but never syncs more often than its schedule.
  • After 10 failures in a row the source is paused and its schedule stops firing; paused_reason says why. Running grad sources sync ID or storing a new key with grad secrets set NAME resumes it.

grad sources list shows each source's status and last error:

Shell
grad sources list
# id         dataset       provider  schedule      status            last sync      synced   last error
# src_01J9X  support-prod  langfuse  */15 * * * *  active (2 failed)  failed         12:00    langfuse: 401 Unauthorized

On Gradiated Cloud#

On Gradiated Cloud, the gateway records every request and its feedback, so you don't need an observability tool. Connect them with the built-in gradiated source: grad sources add gradiated --dataset support-prod --schedule "0 * * * *".

Supported providers#

ProviderFile import --formatSync with grad sources addGuide
Gradiated Cloud (coming soon)gradiatedgradiatedIntegrations
LangfuselangfuselangfuseLangfuse
LangSmithlangsmithlangsmithLangSmith
BraintrustbraintrustbraintrustBraintrust
PostHogposthogposthogPostHog
Arize Phoenix (OpenInference)otelphoenixArize Phoenix
OpenAI chat fine-tuningopenai_chatnone (dataset.push())
Native schemagrad_trajectoriesnone (dataset.push())
Hugging Face Hubnonenone (grad datasets import hf)Import a Hugging Face dataset

Braintrust and PostHog sources also need project (the Braintrust project id or name, the numeric PostHog project id); LangSmith and Phoenix take an optional one (default default). File imports accept JSONL, JSON and Parquet. Any OpenTelemetry GenAI export imports with --format otel.

Send feedback from production#

grad has no feedback API of its own. Send scores and corrections to the tool that already holds your traces, using its own SDK, and a source brings them into the next dataset version. Tag each trace with the model and version that produced it, so you can train on, or filter out, a specific rollout.

Python
from langfuse import Langfuse, propagate_attributes

langfuse = Langfuse(release="support-bot@2026-11-27")  # or set LANGFUSE_RELEASE

with langfuse.start_as_current_observation(as_type="span", name="answer", version="v42"):
    with propagate_attributes(session_id="sess_42", user_id="user_7", metadata={"model": "your-org/support-sft"}):
        trace_id = langfuse.get_current_trace_id()
        ...  # call the model

# Later, when the user rates the answer:
langfuse.create_score(trace_id=trace_id, name="resolved", value=1.0, comment="Fixed my invoice")

Each integration guide shows how the provider's fields map onto records and how to filter what you train on.

What to include in your data#

Every record needs messages (or input.messages). Beyond that, include only the fields your recipe uses:

If you are training...Also include
Fine-tune (SFTRecipe)Nothing else: the final assistant message is the target. When a record has expected_output (a gold answer, or a correction synced from LangSmith, Braintrust or Langfuse), that is the target instead and replaces the logged final answer. Records without a prompt (a user or system turn) don't train.
Decision model (DecisionModelRecipe)expected_output (the target label)
Model router (RouterRecipe)Optional metadata.model, scores, and metrics.cost_usd (or pass a Judge and models= to score and compare automatically)
Preference alignment (DPORecipe)chosen and rejected completions for the same prompt, or paired records sharing a group_id with role="chosen" and role="rejected"
Multi-turn conversationssession_id so turns from the same conversation stay together across train and eval splits

Need images, audio, tools, or the full JSON field list? See the Primitives reference.