> ## 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.

# Link Python root spans to external trace IDs

> Use an external OpenTelemetry trace ID as a root span row ID and record feedback against it without a separate Braintrust-specific lookup.

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - Link Braintrust root spans and user feedback to an external OpenTelemetry trace ID

## Summary

Your OpenTelemetry trace ID can serve as the Braintrust root span row ID. Create the root span with `id=<external_trace_id>` and leave `root_span_id` SDK-managed. Call `logger.log_feedback(id=<external_trace_id>, ...)` to attach feedback to the same row.

## What is happening

Braintrust creates a distinct root row ID for traces by default. That ID is the value `log_feedback` expects. Your OpenTelemetry trace ID is different. That prevents a single ID from linking logs, traces, and feedback.

When you create the root span via `logger.start_span`, the SDK accepts an explicit span row ID via the `id` parameter. Child spans should keep SDK-generated IDs. Reusing a row ID recreates or replaces the root row for that ID.

## Fix or suggestion

### Option 1: Use your OpenTelemetry trace ID as the root row ID (recommended)

1. Create the root span with `logger.start_span(..., id=trace_id)` and add the external ID to metadata. Keep child spans using the SDK defaults.
2. Log feedback using the same ID with `logger.log_feedback(id=trace_id, scores={...})`.
3. Flush the logger to ensure events are sent.

Minimal Python example:

Use your existing OpenTelemetry span as `otel_span`. Format its trace ID as a string and pass the same value to both helpers.

```python theme={null}
from braintrust import init_logger
from opentelemetry.trace import format_trace_id

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

def record_turn(trace_id, input_data, output_data):
    with logger.start_span(
        name="turn",
        id=trace_id,
        metadata={"otel_trace_id": trace_id},
    ) as turn:
        turn.log(input=input_data, output=output_data)
    logger.flush()

def record_feedback(trace_id, thumbs_up: bool):
    logger.log_feedback(
        id=trace_id,
        scores={"user_feedback": 1 if thumbs_up else 0},
    )
    logger.flush()

trace_id = format_trace_id(otel_span.get_span_context().trace_id)
record_turn(trace_id, input_data, output_data)
record_feedback(trace_id, thumbs_up=True)
```

Notes for this option:

* Use one root row per external trace ID. Reusing the same `id` for a new root span can replace the existing row.
* Do not overwrite the SDK-managed `root_span_id` field. Set only the span row `id` on the root span.

### Option 2: Map existing Braintrust row IDs to your trace IDs

1. If Braintrust already generated row IDs, maintain a mapping from your `trace_id` to the Braintrust row ID.
2. Alternatively, if the external trace ID was already stored in `metadata.otel_trace_id`, query Braintrust with SQL to find the root row. Example query condition:

```
metadata.otel_trace_id = '<external_trace_id>' AND span_parents IS NULL
```

3. Use the root row ID returned by the query or retrieved from your stored mapping. Then call `logger.log_feedback` with that row ID:

```python theme={null}
logger.log_feedback(
    id=row_id,  # Braintrust row ID retrieved from your mapping or query
    scores={"user_feedback": 1 if thumbs_up else 0},
)
```

When to prefer this option:

* You already have traces with Braintrust-generated row IDs and cannot recreate the root spans.

## How to confirm it worked

* Create a root span with `id=<trace_id>` and verify a root row exists with that ID in Logs. The row should show `otel_trace_id` in metadata if you set it.
* Call `logger.log_feedback(id=<trace_id>, ...)` and confirm the feedback appears on the same root row.

## Notes

* Leave `root_span_id` SDK-managed as recommended in the tracing docs. Setting the root row `id` on the root span is safe for linking external trace IDs.
* If you need Braintrust spans to share actual OpenTelemetry trace context beyond storing the ID in metadata, use the [OpenTelemetry context sharing integration](https://docs.braintrust.dev/integrations/sdk-integrations/opentelemetry/link-spans.md).
* Use a stored mapping or a SQL lookup on `metadata.otel_trace_id` to handle existing traces that already have Braintrust-generated row IDs. The SQL lookup requires the external ID to have been stored in that field.


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