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

# Large trace download fails with 'Invalid string length'

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - Downloading a single large trace via the UI 'Download this trace' option when it fails silently or throws 'Invalid string length'

## Summary

**Issue:** The **Download this trace** option in the UI can fail for very large traces, sometimes showing an `Invalid string length` error.

**Cause:** The UI fetches the trace spans through `/btql` and then serializes the full trace payload to JSON for download. For very large traces, that serialization can exceed the JavaScript runtime’s maximum string size.

**Resolution:** Fetch the trace spans through the Braintrust API with pagination instead of using the UI download button.

## Diagnosing the failure

Open browser DevTools and check the Network and Console tabs when the download fails. An `Invalid string length` error is consistent with a very large trace payload failing during JSON serialization.

## Resolution steps

### Fetch the trace via SQL

Use SQL to retrieve spans for the trace with full payloads and stable ordering:

```sql theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
SELECT *
FROM project_logs('<PROJECT_ID>', shape => 'spans')
WHERE root_span_id = '<ROOT_SPAN_ID>'
ORDER BY _pagination_key ASC
LIMIT 100
SETTINGS preview_length = -1;
```

If pagination is required, fetch the next page with the [returned cursor](https://docs.braintrust.dev/reference/sql/query-structure.md#cursors-for-pagination):

```sql theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
SELECT *
FROM project_logs('<PROJECT_ID>', shape => 'spans')
WHERE root_span_id = '<ROOT_SPAN_ID>'
ORDER BY _pagination_key ASC
LIMIT 100
OFFSET '<CURSOR_FROM_PREVIOUS_RESPONSE>'
SETTINGS preview_length = -1;
```

### Reduce trace size at logging time

If large inline payloads cause repeated issues, move large data to attachments to reduce the size of span payloads during logging and export. See "[Optimize large trace logging with attachments](https://docs.braintrust.dev/kb/optimize-large-trace-logging-with-attachments)" troubleshooting article.

## Notes

* Filtering by `root_span_id` is an efficient way to retrieve a known trace.
* `_pagination_key` provides a stable ordering for pagination.


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