Context
When creating threads in LangGraph, you may notice that threads are being automatically deleted or not persisting after request completion. In the agent pod logs, you might see threads marked with "temporary": true, which indicates that the threads are being treated as stateless runs and will be deleted upon completion.
Answer
This issue occurs when you're using the stateless runs endpoint instead of the stateful runs endpoint. Stateless runs create temporary threads that are automatically deleted after completion, while stateful runs preserve threads for future interactions.
To resolve this issue, you need to:
Use the stateful runs endpoint - Instead of calling the stateless endpoint, use the stateful endpoint that includes a thread ID in the URL path:
/threads/{thread_id}/runs/streamProvide a thread ID - Include a specific thread ID in your request URL. You can generate your own thread ID or let the system create one.
Include "if_not_exists": "create" - Add this parameter to your payload to automatically create the thread if it doesn't exist.
Example of correct payload structure:
{
"input": {
"messages": [
{
"type": "user",
"content": [
{
"text": "test question?",
"type": "text"
}
]
}
]
},
"config": {
"configurable": {
"agent_id": "2e21ab61-ffftf-s1221"
}
},
"assistant_id": "react_agent",
"if_not_exists": "create"
}Example curl request:
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream \
--header 'Content-Type: application/json' \
--header 'x-api-key: <API_KEY>' \
--data "{
\"assistant_id\": \"agent\",
\"input\": <inputs>,
\"stream_mode\": \"updates\"
}"For more detailed information, refer to the LangChain streaming documentation and the API reference.