openai/chatkit-python

Public

mirrored fromhttps://github.com/openai/chatkit-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c4f347230be4eeacbfb1b3f406abe4dc0860cbef

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docs/concepts/threads.md

123lines · modecode

1# Threads and items
2
3In ChatKit, a **thread** represents a single conversation. It is the unit that ties together everything that happens in that conversation: messages, widgets, actions, system signals, and metadata. A thread is stored as an ordered history of **thread items**, which ChatKit loads, paginates, and renders as needed.
4
5## What is a thread?
6
7A thread is an ordered timeline that contains:
8
9- Conversation history (user and assistant messages)
10- Structured content such as widgets and workflows
11- Internal signals that guide processing or model behavior
12- Metadata like titles or status flags
13
14Threads are persisted by your store implementation and can be updated, continued, or made read-only according to your application’s needs.
15
16## What are thread items?
17
18Thread items are the individual records that make up a thread. Each item represents one meaningful unit in the conversation history, such as:
19
20- A user message
21- An assistant response
22- A widget rendered by the assistant
23- A non-visible signal used only for model input
24
25ChatKit maintains the order of items, streams new ones as they are produced, and paginates them when history is loaded.
26
27## How threads are created and updated
28
29A typical thread lifecycle looks like this:
30
31- **Thread creation**: When a user submits a message and no thread exists yet, ChatKitServer creates one and persists it by calling your store’s `save_thread`.
32- **Appending items**: As the server streams a response, ChatKit persists thread items automatically as each item completes. Streaming events directly drive what gets stored.
33- **Updating metadata**: During respond, you can freely mutate the thread object (for example, to set or refine the title). ChatKit automatically persists these updates when the response completes. You can also call store.save_thread explicitly if needed.
34- **Loading history**: When history is enabled client-side, ChatKit retrieves past threads and their items. Users can continue an existing thread by default.
35- **Closing or archiving**: Threads can be marked read-only (for example, by disabling new messages) or deleted entirely if they should no longer be discoverable.
36
37
38## How thread items are used
39
40Thread items serve two primary purposes in ChatKit:
41
42### Model input
43
44Your server's [`respond`](../api/chatkit/server.md#chatkit.server.ChatKitServer.respond) logic reads thread items to construct input for the model input. This ensures the model sees the full conversational context both during an active response and when a user resumes a past thread.
45
46See [Respond to a user message](../guides/respond-to-user-message.md) for a full walkthrough.
47
48### UI rendering
49
50On the client, ChatKit.js renders items incrementally as they stream in for the active thread. When a past thread is loaded, the same persisted items are re-rendered to reconstruct the conversation UI.
51
52## Core item types
53
54### User messages
55
56[`UserMessageItem`](../api/chatkit/types.md#chatkit.types.UserMessageItem)s represent end-user input. They may include:
57
58- Plain text entered by the user
59- Optional `quoted_text` for reply-style UIs
60- Attachment metadata
61
62
63User text is not Markdown-rendered, but it may contain [@-mentions](../guides/accept-rich-user-input.md#-mentions-tag-entities-in-user-messages) if your integration enables them.
64
65
66### Assistant messages
67
68[`AssistantMessageItem`](../api/chatkit/types.md#chatkit.types.AssistantMessageItem)s represent assistant output. Their content can include:
69
70- Markdown-rendered text
71- Tool call outputs
72- Widgets and structured UI elements
73- [Inline annotations](../guides/add-annotations.md)
74
75Assistant text supports rich Markdown and is rendered progressively as it streams.
76
77#### Markdown support
78
79Assistant messages support:
80
81- GitHub-flavored Markdown (headings, lists, code blocks, links, blockquotes)
82- Stable list rendering during streaming (Safari-safe, no reflow)
83- Optional single-newline line breaks
84- Syntax-highlighted, copyable code blocks
85- LaTeX math (inline and block)
86- Tables with automatic sizing and horizontal scrolling
87- Inline annotations that create interactive affordances in the UI
88
89### Hidden context items
90
91Hidden context items are included in model input but are not rendered in the chat UI. They allow the model to react to what happened in the interface, not just what the user typed.
92
93Typical use cases include recording widget actions, selection state, or system signals.
94
95- **[`HiddenContextItem`](../api/chatkit/types.md#chatkit.types.HiddenContextItem)**: Integration-defined hidden context. You control its schema and how it is converted for the model.
96
97- **[`SDKHiddenContextItem`](../api/chatkit/types.md#chatkit.types.SDKHiddenContextItem)**: Hidden context inserted by the ChatKit Python SDK for its own internal operations. Most applications do not need to modify this unless overriding conversion behavior.
98
99## Thread item actions
100
101Thread item actions are quick action buttons associated with an assistant turn. They let users act on the output—such as retrying a response, copying content, or submitting feedback.
102
103Actions are configured client-side using the [threadItemActions option](https://openai.github.io/chatkit-js/api/openai/chatkit-react/type-aliases/threaditemactionsoption/).
104
105## Converting items to model input
106
107[`ThreadItemConverter`](../api/chatkit/agents.md#chatkit.agents.ThreadItemConverter) translates stored thread items into model-ready input items. The default converter understands common ChatKit item types such as messages, widgets, workflows, and tasks.
108
109You can override the converter when you need custom behavior. For example:
110
111- Formatting attachments for the model
112- Translating tags or mentions into structured input
113- Summarizing rich widgets into text the model can consume
114
115Custom conversion is typically paired with prompting so the model receives a coherent representation of the conversation.
116
117## Related guides
118- [Respond to a user message](../guides/respond-to-user-message.md)
119- [Pass extra app context to your model](../guides/pass-extra-app-context-to-your-model.md)
120- [Add annotations in assistant messages](../guides/add-annotations.md)
121- [Accept rich user input](../guides/accept-rich-user-input.md#-mentions-tag-entities-in-user-messages)
122- [Handle feedback](../guides/handle-feedback.md)
123- [Let users browse past threads](../guides/browse-past-threads.md)
124