Reingest historical data into Langfuse
Build a one-off script: extract from the source, transform to the Langfuse observation model, reingest. Extract using the source vendor's documented export mechanism (SDK export client or documented API, as named in the matching migration guide on langfuse.com) — never guess REST endpoints or hostnames. If an export call fails with 404/405, stop and re-read the vendor guide instead of trying endpoint variants. Adapt the working implementation in the project-to-project migration cookbook — its OTLP reingestion, retry, and ID-mapping code applies to any source.
Transform target
Fetch migrate custom ingestion to v4 for the current span format and attributes. Non-negotiables:
- POST OTLP JSON to
/api/public/otel/v1/traces, building the payload as plain JSON — the only path that preserves originalstart_time/end_time. Do not create the spans through an OTel or Langfuse SDK tracer: tracers assign new timestamps and random trace/span IDs, and offer no way to set either. - Destination IDs must be OTel-shaped: 32-hex-char trace IDs, 16-hex-char span IDs. Derive them deterministically from source IDs (e.g. a sha256 prefix) so every source record maps to one predictable destination ID for reconciliation and resume logic. Deterministic IDs do not make re-sends safe: Langfuse v4 does not reliably deduplicate same-ID re-ingests, a re-sent span can create a duplicate observation and inflate counts and cost (see the migration guide above). Idempotency comes from not re-sending: record each successfully sent source ID in a local state file and skip recorded IDs on every re-run. Verify after the sample run that the destination trace IDs match the computed ones. If they do not, the spans went through a tracer.
- Map source spans to Langfuse observation types (generation, tool, agent, span) and set
gen_ai.usage.*and model attributes so token usage and cost tracking work. Put user, session, tags, and metadata in the documentedlangfuse.*trace attributes; overall input/output goes on the root observation.
Migration order
Dependencies dictate order: score configs → custom model definitions → prompts (all versions, in version order, then labels) → observations (traces) → scores → datasets (items, then run items linked to migrated traces). Skip whatever the user excluded in the interview.
Not migratable via API (tell the user; do not attempt): LLM-as-a-Judge evaluator configurations, custom dashboards, users/RBAC, project settings. Recreate evaluators in the destination instead, and re-run experiments rather than importing their scores.
Execution protocol
- Cutoff filter from the interview goes into the source extraction query, not post-hoc filtering. If the live cutover already happened, also bound the window at the cutover time — traces after it are already arriving in the destination, and reingesting them duplicates data.
- Sample run first: ~10 traces spanning the date range. Print direct links to the destination traces and stop until the user confirms they look correct — hierarchy, timestamps, token usage and cost, scores attached.
- Full run: paginate the source, batch the ingestion, print progress (
page X — N traces migrated, M failed), and collect failures with their source IDs into a retry file instead of aborting. Resume from the state file: skip every source ID already recorded as sent, including the sample run — re-sending creates duplicate observations and is billed again. - Reconcile: compare source vs destination counts per data type and report the table. Duplicate observations in the destination mean the same ID was sent twice - a migration bug to fix in the state-file skip, not a transient artifact that clears itself. Remind the user that reingested history is billed as new ingestion.
Historical cost may differ from what the source displayed: the destination recomputes cost from current model prices unless the source's actual cost is carried in the documented cost attributes.
Verify credentials by presence only ([ -n "$LANGFUSE_SECRET_KEY" ]); never print secret values.