使用线程¶
先决条件
本指南将展示如何创建、查看和检查线程。
创建线程¶
要运行图并持久化状态,必须首先创建线程。
空线程¶
要创建新线程,请使用 LangGraph SDK 的 create
方法。有关更多信息,请参阅 Python 和 JS SDK 参考文档。
输出
{
"thread_id": "123e4567-e89b-12d3-a456-426614174000",
"created_at": "2025-05-12T14:04:08.268Z",
"updated_at": "2025-05-12T14:04:08.268Z",
"metadata": {},
"status": "idle",
"values": {}
}
复制线程¶
或者,如果您在应用程序中已有要复制其状态的线程,可以使用 copy
方法。这将创建一个独立的线程,其历史记录在操作时与原始线程相同。有关更多信息,请参阅 Python 和 JS SDK 参考文档。
预填充状态¶
最后,您可以通过向 create
方法提供 supersteps
列表来创建具有任意预定义状态的线程。supersteps
描述了一系列状态更新的列表。例如:
from langgraph_sdk import get_client
client = get_client(url=<DEPLOYMENT_URL>)
thread = await client.threads.create(
graph_id="agent",
supersteps=[
{
updates: [
{
values: {},
as_node: '__input__',
},
],
},
{
updates: [
{
values: {
messages: [
{
type: 'human',
content: 'hello',
},
],
},
as_node: '__start__',
},
],
},
{
updates: [
{
values: {
messages: [
{
content: 'Hello! How can I assist you today?',
type: 'ai',
},
],
},
as_node: 'call_model',
},
],
},
])
print(thread)
import { Client } from "@langchain/langgraph-sdk";
const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
const thread = await client.threads.create({
graphId: 'agent',
supersteps: [
{
updates: [
{
values: {},
asNode: '__input__',
},
],
},
{
updates: [
{
values: {
messages: [
{
type: 'human',
content: 'hello',
},
],
},
asNode: '__start__',
},
],
},
{
updates: [
{
values: {
messages: [
{
content: 'Hello! How can I assist you today?',
type: 'ai',
},
],
},
asNode: 'call_model',
},
],
},
],
});
console.log(thread);
curl --request POST \
--url <DEPLOYMENT_URL>/threads \
--header 'Content-Type: application/json' \
--data '{"metadata":{"graph_id":"agent"},"supersteps":[{"updates":[{"values":{},"as_node":"__input__"}]},{"updates":[{"values":{"messages":[{"type":"human","content":"hello"}]},"as_node":"__start__"}]},{"updates":[{"values":{"messages":[{"content":"Hello\u0021 How can I assist you today?","type":"ai"}]},"as_node":"call_model"}]}]}'
输出
{
"thread_id": "f15d70a1-27d4-4793-a897-de5609920b7d",
"created_at": "2025-05-12T15:37:08.935038+00:00",
"updated_at": "2025-05-12T15:37:08.935046+00:00",
"metadata": {"graph_id": "agent"},
"status": "idle",
"config": {},
"values": {
"messages": [
{
"content": "hello",
"additional_kwargs": {},
"response_metadata": {},
"type": "human",
"name": null,
"id": "8701f3be-959c-4b7c-852f-c2160699b4ab",
"example": false
},
{
"content": "Hello! How can I assist you today?",
"additional_kwargs": {},
"response_metadata": {},
"type": "ai",
"name": null,
"id": "4d8ea561-7ca1-409a-99f7-6b67af3e1aa3",
"example": false,
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": null
}
]
}
}
列出线程¶
LangGraph SDK¶
要列出线程,请使用 LangGraph SDK 的 search
方法。这将列出应用程序中与提供的过滤器匹配的线程。有关更多信息,请参阅 Python 和 JS SDK 参考文档。
按线程状态过滤¶
使用 status
字段根据线程的状态进行过滤。支持的值有 idle
(空闲)、busy
(忙碌)、interrupted
(中断)和 error
(错误)。有关每个状态的信息,请参阅此处。例如,要查看 idle
线程:
输出
[
{
'thread_id': 'cacf79bb-4248-4d01-aabc-938dbd60ed2c',
'created_at': '2024-08-14T17:36:38.921660+00:00',
'updated_at': '2024-08-14T17:36:38.921660+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'idle',
'config': {'configurable': {}}
}
]
按元数据过滤¶
search
方法允许您按元数据过滤
输出
[
{
'thread_id': 'cacf79bb-4248-4d01-aabc-938dbd60ed2c',
'created_at': '2024-08-14T17:36:38.921660+00:00',
'updated_at': '2024-08-14T17:36:38.921660+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'idle',
'config': {'configurable': {}}
}
]
排序¶
SDK 还支持使用 sort_by
和 sort_order
参数按 thread_id
、status
、created_at
和 updated_at
对线程进行排序。
LangGraph 平台用户界面¶
您还可以通过 LangGraph 平台用户界面查看部署中的线程。
在您的部署中,选择“线程”选项卡。这将加载部署中所有线程的表格。
要按线程状态过滤,请在顶部栏中选择一个状态。要按支持的属性排序,请单击所需列的箭头图标。
检查线程¶
LangGraph SDK¶
获取线程¶
要根据 thread_id
查看特定线程,请使用 get
方法
输出
{
'thread_id': 'cacf79bb-4248-4d01-aabc-938dbd60ed2c',
'created_at': '2024-08-14T17:36:38.921660+00:00',
'updated_at': '2024-08-14T17:36:38.921660+00:00',
'metadata': {'graph_id': 'agent'},
'status': 'idle',
'config': {'configurable': {}}
}
检查线程状态¶
要查看给定线程的当前状态,请使用 get_state
方法
输出
{
"values": {
"messages": [
{
"content": "hello",
"additional_kwargs": {},
"response_metadata": {},
"type": "human",
"name": null,
"id": "8701f3be-959c-4b7c-852f-c2160699b4ab",
"example": false
},
{
"content": "Hello! How can I assist you today?",
"additional_kwargs": {},
"response_metadata": {},
"type": "ai",
"name": null,
"id": "4d8ea561-7ca1-409a-99f7-6b67af3e1aa3",
"example": false,
"tool_calls": [],
"invalid_tool_calls": [],
"usage_metadata": null
}
]
},
"next": [],
"tasks": [],
"metadata": {
"thread_id": "f15d70a1-27d4-4793-a897-de5609920b7d",
"checkpoint_id": "1f02f46f-7308-616c-8000-1b158a9a6955",
"graph_id": "agent_with_quite_a_long_name",
"source": "update",
"step": 1,
"writes": {
"call_model": {
"messages": [
{
"content": "Hello! How can I assist you today?",
"type": "ai"
}
]
}
},
"parents": {}
},
"created_at": "2025-05-12T15:37:09.008055+00:00",
"checkpoint": {
"checkpoint_id": "1f02f46f-733f-6b58-8001-ea90dcabb1bd",
"thread_id": "f15d70a1-27d4-4793-a897-de5609920b7d",
"checkpoint_ns": ""
},
"parent_checkpoint": {
"checkpoint_id": "1f02f46f-7308-616c-8000-1b158a9a6955",
"thread_id": "f15d70a1-27d4-4793-a897-de5609920b7d",
"checkpoint_ns": ""
},
"checkpoint_id": "1f02f46f-733f-6b58-8001-ea90dcabb1bd",
"parent_checkpoint_id": "1f02f46f-7308-616c-8000-1b158a9a6955"
}
可选地,要查看线程在给定检查点时的状态,只需传入检查点 ID(或整个检查点对象)
检查完整线程历史¶
要查看线程的历史记录,请使用 get_history
方法。这会返回线程经历的所有状态列表。有关更多信息,请参阅 Python 和 JS 参考文档。
LangGraph 平台用户界面¶
您还可以通过 LangGraph 平台用户界面查看部署中的线程。
在您的部署中,选择“线程”选项卡。这将加载部署中所有线程的表格。
选择一个线程以检查其当前状态。要查看其完整历史记录并进行进一步调试,请在 LangGraph Studio 中打开该线程。