Skip to content

Commit 19b3965

Browse files
authored
docs: Update thread id instructions to use uuid7 (#3423)
## Overview CC @agola11 ## Type of change **Type:** [Replace with: New documentation page / Update existing documentation / Fix typo/bug/link/formatting / Remove outdated content / Other] ## Related issues/PRs <!-- Link to related issues, feature PRs, or discussions (if applicable) To automatically close an issue when this PR is merged, use closing keywords: - "closes #123" or "fixes #123" or "resolves #123" For regular references without auto-closing, just use: - "#123" or "See issue #123" Examples: - closes #456 (will auto-close issue #456 when PR is merged) - See #789 for context (will reference but not auto-close issue #789) --> - GitHub issue: - Feature PR: <!-- For LangChain employees, if applicable: --> - Linear issue: - Slack thread: ## Checklist <!-- Put an 'x' in all boxes that apply --> - [x] I have read the [contributing guidelines](README.md), including the [language policy](https://docs.langchain.com/oss/python/contributing/overview#language-policy) - [x] I have tested my changes locally using `docs dev` - [x] All code examples have been tested and work correctly - [x] I have used **root relative** paths for internal links - [x] I have updated navigation in `src/docs.json` if needed (Internal team members only / optional): Create a preview deployment as necessary using the [Create Preview Branch workflow](https://github.com/langchain-ai/docs/actions/workflows/create-preview-branch.yml) ## Additional notes <!-- Any other information that would be helpful for reviewers -->
1 parent 73d27cb commit 19b3965

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/langsmith/query-threads.mdx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@ The SDK exposes two methods for working with threads:
1818

1919
Each run you create can carry a `thread_id` in its metadata. LangSmith uses this to group runs into threads. The backend looks for `thread_id` in `metadata` (falling back to `session_id` or `conversation_id`).
2020

21+
<Note>
22+
We recommend using **UUID v7** thread IDs. UUIDv7 embeds a timestamp, which preserves correct time-ordering of threads. The LangSmith SDK exports a uuid7 helper (Python v0.4.43+, JS v0.3.80+):
23+
24+
- **Python**: `from langsmith import uuid7`
25+
- **JS/TS**: `import { uuid7 } from 'langsmith'`
26+
</Note>
27+
2128
If you're using a [tracing integration](/langsmith/integrations), pass `thread_id` in the run metadata:
2229

2330
<CodeGroup>
2431

2532
```python Python
26-
from langsmith import traceable
33+
from langsmith import traceable, uuid7
34+
35+
THREAD_ID = str(uuid7())
2736

28-
@traceable(metadata={"thread_id": "conv-abc123"})
37+
@traceable(metadata={"thread_id": THREAD_ID})
2938
def my_agent(user_message: str) -> str:
3039
...
3140
```
3241

3342
```typescript TypeScript
3443
import { traceable } from "langsmith/traceable";
44+
import { uuid7 } from "langsmith";
45+
46+
const THREAD_ID = uuid7();
3547

3648
const myAgent = traceable(
3749
async (userMessage: string) => {
3850
// ...
3951
},
40-
{ metadata: { thread_id: "conv-abc123" } }
52+
{ metadata: { thread_id: THREAD_ID } }
4153
);
4254
```
4355

src/langsmith/threads.mdx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ To associate traces together, you need to pass in a special `metadata` key where
1515
- `thread_id`
1616
- `conversation_id`
1717

18-
The value can be any string you want, but we recommend using UUIDs, such as `f47ac10b-58cc-4372-a567-0e02b2c3d479`. Check out [Add metadata and tags to traces](/langsmith/add-metadata-tags) for instructions.
18+
The value can be any string you want, but we recommend using **UUID v7** thread IDs.
19+
20+
The LangSmith SDK exports a uuid7 helper (Python v0.4.43+, JS v0.3.80+):
21+
22+
- **Python**: `from langsmith import uuid7`
23+
- **JS/TS**: `import { uuid7 } from 'langsmith'`
24+
25+
Check out [Add metadata and tags to traces](/langsmith/add-metadata-tags) for instructions.
1926

2027
<Warning>
2128
**Important:** To ensure filtering and token counting work correctly across your entire thread, you must set the thread metadata (`session_id`, `thread_id`, or `conversation_id`) on **all runs**, including child runs within a trace.
@@ -44,15 +51,15 @@ from dotenv import load_dotenv
4451
load_dotenv()
4552

4653
import openai
47-
from langsmith import traceable, Client
54+
from langsmith import traceable, Client, uuid7
4855
from langsmith.wrappers import wrap_openai
4956

5057
# Initialize clients
5158
langsmith_client = Client()
5259
client = wrap_openai(openai.Client())
5360

5461
# Configuration
55-
THREAD_ID = "thread-id-1"
62+
THREAD_ID = str(uuid7())
5663

5764
# Using a local directory to store thread history. For production use, use a persistent storage solution.
5865
THREADS_DIR = os.path.join(os.path.dirname(__file__), "threads")
@@ -115,6 +122,7 @@ import * as dotenv from "dotenv";
115122
import OpenAI from "openai";
116123
import { traceable } from "langsmith/traceable";
117124
import { wrapOpenAI } from "langsmith/wrappers";
125+
import { uuid7 } from "langsmith";
118126

119127
const __dirname = path.dirname(fileURLToPath(import.meta.url));
120128

@@ -125,7 +133,7 @@ dotenv.config();
125133
const client = wrapOpenAI(new OpenAI());
126134

127135
// Configuration
128-
const THREAD_ID = "thread-id-1";
136+
const THREAD_ID = uuid7();
129137

130138
// Using a local directory to store thread history. For production use, use a persistent storage solution.
131139
const THREADS_DIR = path.join(__dirname, "threads");

0 commit comments

Comments
 (0)