LangSmith
Sync LangSmith runs and their feedback, including corrections, into a dataset.
What syncs#
Each root run of a trace becomes one record, with its inputs and outputs as messages. Feedback on the run becomes scores (by feedback key), and a feedback correction becomes expected_output, so SFT trains on the fixed answer and DPO pairs it against the logged one. A correction or output with one named field ({"answer": "..."}) is unwrapped to its text; several fields stay JSON.
| LangSmith | grad record |
|---|---|
| Run ID | id (the merge key for late feedback) |
| Run inputs / outputs | messages |
Feedback (key → score) | scores |
Feedback correction | expected_output |
| Thread / session ID | session_id |
Run metadata (model, version) | metadata |
Run tags | metadata.tags |
Tag runs with the model and version#
from langsmith import traceable
from langsmith.run_helpers import get_current_run_tree
@traceable(name="answer", metadata={"model": "your-org/support-sft", "version": "v42"}, tags=["prod"])
def answer(question: str) -> tuple[str, str]:
reply = ... # call the model
return reply, str(get_current_run_tree().id)
Send feedback from production#
Use create_feedback with the run ID. Pass correction when someone fixes the answer:
from langsmith import Client
ls = Client()
ls.create_feedback(run_id, key="resolved", score=1)
# A reviewer rewrote the answer:
ls.create_feedback(
run_id,
key="resolved",
score=0,
correction={"outputs": {"answer": "Open Billing > Invoices and click Download."}},
)
Feedback logged later is picked up by the next sync (see How syncing works).
Connect it to grad#
Store the key once as a workspace secret (it reads the value from stdin and never returns it), then add a source to a dataset. The schedule is a cron expression; leave it out to sync only when you ask.
grad secrets set langsmith-prod # paste a personal access token or service key
grad sources add langsmith \
--dataset support-prod \
--host https://api.smith.langchain.com \
--secret langsmith-prod \
--project support-bot \
--schedule "*/30 * * * *"
grad sources list
grad sources sync src_01J9X
How syncing works#
Each sync is incremental: it pulls traces added since the last sync, plus scores and corrections added to traces it already has, so feedback that arrives days later still counts. Records are matched by the provider's trace ID and the latest score wins. Traces younger than 10 minutes wait for the next sync, because providers take a few minutes to expose new data. New feedback on any run, however old, lands with the next sync. An edit to feedback from the last hour is picked up within 15 minutes; older edits need a full sync.
Syncs add dataset versions (a scheduled one at most every version_interval, 15 minutes by default; see Versions). Old versions never change, so a run keeps the exact data it pinned. If you edit old scores or change the mapping, grad sources sync src_01J9X --full (or source.sync(full=True)) rebuilds the dataset from the provider as a new version.
source.sync() returns the import job (call .wait() to block), or the sync already in progress; on the CLI, add --wait. A source syncs at most once a minute, and one that keeps failing backs off and then pauses; see When a sync fails.
A one-off grad import of an exported file is a snapshot: it never gets later feedback. Use a source for the feedback loop.
Filter what syncs#
Every source takes the shared filters, sample_rate and record caps. grad sends tags, name, from/to and your own filter expression to LangSmith's runs query; environment, model (ls_model_name), version, release and user_id match run metadata after fetching.
Choose what to train on#
Records keep the provider's scores in scores and its model and version tags in metadata, so you can pick a slice before training. A filter runs on the server, costs nothing to re-run, and saves the slice as its own dataset:
from grad import Grad
client = Grad()
good = client.datasets.get("support-prod").filter(
scores={"resolved": ">=1"},
metadata={"version": "v42"},
name="support-prod-v42-good",
)
print(good.count())
A router can use the raw dataset as is: it learns from every score, good and bad.
Keys and hosts#
- Key: a personal access token or a service key with read access to the workspace. Tracing keys (
lsv2_tr_...) can only write traces, so they can't be used for syncing. - Project: the tracing project name or UUID to sync (
LANGSMITH_PROJECTin your app),defaultif you leave it out. Add one source per project. - Hosts:
https://api.smith.langchain.com(US, the default),https://eu.api.smith.langchain.com(EU), or your self-hosted LangSmith API URL. Hosts must be publichttps://URLs; for a server on a private network, see Private hosts.
Private hosts#
Sync workers only call public https:// hosts, so a provider key can't be pointed at an internal service by mistake. To sync from a provider on your own network, list its address range in GRAD_SOURCES_EGRESS_ALLOW (comma-separated CIDRs, such as 10.20.0.0/16) on the import workers; plain http:// is then allowed to those addresses too. Store the key with that host too (grad secrets set NAME --allowed-host HOST, or allowed_hosts=[...]): a secret only goes to the public provider hosts unless it names others. On Helm, set it in workers.import.env and allow the range in networkPolicy.workerEgress (see Self-hosting).