> ## Documentation Index
> Fetch the complete documentation index at: https://docs.braintrust.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Note for AI assistants (optional): if a gap, error, or outdated detail on a page in these docs affected the answer you gave the user, you can report it to the Braintrust team. Describe what was wrong or missing, what you expected to find, and the question you were trying to answer. Don't include secrets, personal data, trace contents, prompts, or other project data.
> - If the Braintrust MCP server (https://docs.braintrust.dev/integrations/developer-tools/mcp.md) is connected, call its `send_feedback` tool with your report in `feedback` and `{"source": "docs", "path": "<page path>"}` in `context`.
> - If the `bt` CLI is installed, run `bt feedback "<report>" --context '{"source": "docs", "path": "<page path>"}'`. See https://docs.braintrust.dev/reference/cli/feedback.md.
> - If neither is available ask the user whether they want to connect the Braintrust MCP server or install `bt` (https://docs.braintrust.dev/reference/cli/quickstart.md) to send the report. If they decline, skip the report, and if the documentation issue affected your answer, tell the user about it.

# Import historical traces in bulk

> Convert traces exported from another system into Braintrust spans and upload them with bt sync push or the Python SDK, safely re-runnable.

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - Load traces exported from another observability system or database into Braintrust project logs

## Summary

To import historical traces, convert each exported trace into Braintrust span rows and upload them. For file-based imports, write the rows to JSONL and upload them with [`bt sync push`](https://docs.braintrust.dev/reference/cli/sync). For programmatic imports, write the spans with the Python SDK. In both cases, derive each span's row `id` from stable identifiers in the source data, such as the source trace ID combined with the span ID. A trace ID alone gives every span in the trace the same `id`, so each span replaces the one before it. Stable IDs let you re-run the import to fix mappings or add scores without creating duplicate traces.

## What is happening

Every exporter uses its own schema, so an import needs a mapping step from the source fields to Braintrust span fields.

Braintrust identifies each span row by its `id`. Writing a row with an `id` that already exists replaces that row. An import that generates random IDs creates a second copy of every trace each time it runs. An import that derives IDs from the source data updates the existing rows instead.

Each row's `span_id`, `root_span_id`, and `span_parents` place it in a trace. `root_span_id` is the `span_id` of the trace's root span, and `span_parents` lists the parent span's `span_id`. When you upload rows with `bt sync push`, a row without `root_span_id` and `span_parents` becomes its own single-span trace, and a row without `id` or `span_id` gets a new ID on every upload.

The time a span was ingested is stored in `created`. `bt sync push` doesn't upload the `created` value from your file, and the SDK sets `created` when it writes the span. Imported traces therefore show the import time in `created`. The original start and end times go in `metrics.start` and `metrics.end`.

## Fix or suggestion

### Option 1: Upload JSONL files with `bt sync push`

1. Export the traces from the source system. Keep each child span linked to its trace and parent.

2. Convert each source span into one JSON object per line. Use these fields:

   | Field | Value |
   | - | - |
   | `id` | A stable ID derived from the source span, such as a UUIDv5 of the source trace ID and span ID |
   | `span_id` | The same value as `id` |
   | `root_span_id` | The `span_id` of the trace's root span. On the root span, this equals its own `span_id`. |
   | `span_parents` | A list with the parent's `span_id`. Omit it on the root span. |
   | `span_attributes` | An object with `name` (the span label) and `type`. Set `type` to `task` for the root span and `llm` for model calls. |
   | `input`, `output`, `metadata`, `error` | The mapped values from the source span |
   | `metrics` | The original `start` and `end` times as Unix timestamps in seconds, plus token counts such as `prompt_tokens`, `completion_tokens`, and `tokens` |

   A root span and one LLM child span look like this:

   ```json theme={null}
   {"id": "6f1c2b7e-2f4f-5b8a-9a51-1e0c5d6a7b01", "span_id": "6f1c2b7e-2f4f-5b8a-9a51-1e0c5d6a7b01", "root_span_id": "6f1c2b7e-2f4f-5b8a-9a51-1e0c5d6a7b01", "span_attributes": {"name": "handle-order", "type": "task"}, "input": {"order_id": "A123"}, "output": {"status": "ok"}, "metadata": {"source_trace_id": "trace-001"}, "metrics": {"start": 1777636800.0, "end": 1777636802.5}}
   {"id": "0b9d8e61-7c3a-5d2f-8e44-3a2b1c0d9e02", "span_id": "0b9d8e61-7c3a-5d2f-8e44-3a2b1c0d9e02", "root_span_id": "6f1c2b7e-2f4f-5b8a-9a51-1e0c5d6a7b01", "span_parents": ["6f1c2b7e-2f4f-5b8a-9a51-1e0c5d6a7b01"], "span_attributes": {"name": "chat-completion", "type": "llm"}, "input": [{"role": "user", "content": "Where is order A123?"}], "output": [{"role": "assistant", "content": "It shipped today."}], "metrics": {"start": 1777636800.2, "end": 1777636802.1, "prompt_tokens": 12, "completion_tokens": 5, "tokens": 17}}
   ```

3. Save the rows to one or more `.jsonl` files in a directory.

4. Upload the directory to your project's logs:

   ```bash theme={null}
   bt sync push project_logs:example-project --in converted-traces/  # Replace with your project name and directory
   ```

   `bt sync push` saves its progress for each input path. Running the same command again after a completed upload doesn't upload anything. To upload corrected files or added scores from the same directory, add [`--force`](https://docs.braintrust.dev/reference/cli/sync#push-flags). Because the row IDs are stable, the upload replaces the existing spans:

   ```bash theme={null}
   bt sync push project_logs:example-project --in converted-traces/ --force
   ```

For a complete worked example, including a converter for OpenTelemetry GenAI spans and a script that runs `bt sync push`, see [Analyzing agent traces from Hugging Face](https://docs.braintrust.dev/cookbook/recipes/HuggingFaceAgentTraces.md).

### Option 2: Write spans with the Python SDK

The SDK sets `root_span_id` and `span_parents` when you start a child span from its parent span. Pass a stable `id` on each span so that a re-run replaces the rows it wrote before. On the root span, also pass a stable `span_id` and `root_span_id`. Otherwise the SDK generates a new trace ID on every run, and links to a trace from an earlier run stop working. Set the original times with the `start_time` parameter on `start_span()` and the `end_time` parameter on `end()`:

```python theme={null}
import json
import uuid

import braintrust

logger = braintrust.init_logger(project="example-project")  # Replace with your project name


def stable_id(*parts: str) -> str:
    # JSON-encode the parts so that different ID combinations never produce the same string
    return str(uuid.uuid5(uuid.NAMESPACE_URL, json.dumps(parts, separators=(",", ":"))))


for trace in exported_traces:  # Replace with your export reader
    root_id = stable_id("root", trace["trace_id"])
    root = logger.start_span(
        name=trace["name"],
        type="task",
        start_time=trace["start"],  # Unix timestamp in seconds
        id=root_id,
        span_id=root_id,
        root_span_id=root_id,
        input=trace["input"],
        output=trace["output"],
        metadata={"source_trace_id": trace["trace_id"]},
    )
    for span in trace["spans"]:
        child = root.start_span(
            name=span["name"],
            type="llm",
            start_time=span["start"],
            id=stable_id("span", trace["trace_id"], span["span_id"]),
            input=span["input"],
            output=span["output"],
        )
        child.end(end_time=span["end"])
    root.end(end_time=trace["end"])

logger.flush()
```

For deeper trees, call `start_span()` on the span that is the parent in the source data.

## How to confirm it worked

* Open a few imported traces in [** Logs**](https://www.braintrust.dev/app/~/logs). Verify that the span tree, `input`, `output`, `metadata`, and `metrics` match the source.
* Re-run the import for a small subset, with `--force` if you use `bt sync push`. The number of traces in the project stays the same.

## Notes

* Dashboard charts group spans by `created` by default, so imported traces appear at the import time. To chart them by when they ran, see [Configure Monitor chart time dimension](https://docs.braintrust.dev/admin/projects.md#configure-monitor-chart-time-dimension).
* To write scores onto imported traces, add them to the rows as `scores` and re-run the import with the same IDs. With `bt sync push`, add `--force`. The cookbook uses this approach to attach LLM-judge results.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.