ApoStudio History
Multi-turn chat in ComfyUI, the sneaky way — conversation state lives in a file
- history_in
- history_out
- history_json
- history_md
- turn_count
Here's the thing about chat in a ComfyUI graph: ComfyUI can't loop data inside a single run, so a conversation can't feed back into itself the way it would in a normal chat app. ApoStudio's answer is a cheat that works - persist the conversation to a JSON file on disk, and let two History nodes point at that same file: one loads the past before Chat runs, the other saves the new state after.
How it works. Chat receives a history list through its history_in, appends the new exchange, and hands the whole updated list back out through history_out. History just makes that list survive between runs. The left node (action load) reads the session file and feeds Chat; the right node (action load & append) takes Chat's history_out and writes it back. Both nodes must point at the exact same session_file path - that's the single most common mistake, and the failure is silent (you just get empty history).
Inputs. action is a dropdown with four options:
load- read the file, output its history (place before Chat)load & append- take incoming history and save it (place after Chat)clear- delete the session file and reset (run once to start fresh, then switch back)save- write incoming history without loading
session_file is a full path like C:/chats/session.json or /home/you/chats/session.json. And history_in accepts the APO_HISTORY output from Chat.
Outputs. history_out (APO_HISTORY - wire the load node's into Chat, or Chat's history_out into a save node), history_json (the raw conversation as JSON), history_md (the same thing formatted as readable Markdown with 💬/🤖 role headers - wire it to an ApoStudio Display to actually read the conversation), and turn_count, which counts completed assistant replies. That last one is a handy "how far into this conversation am I" meter.
Wiring it up:
[History: load] ──history_out──► Chat (history_in)
Chat (history_out) ──► [History: load & append]
Chat (response) ──► Display
To start a fresh conversation: switch the save node to clear, run once, switch it back to load & append. The file gets deleted and history resets.
Gotchas. The default session_file lives inside the pack's own folder (prompts/session.json), which is fine until you reinstall or update the pack - set your own path and never look back. Also note the node forces itself to run every time (it returns an always-changed marker), which is exactly what you want for history but means it defeats ComfyUI's cache for anything downstream of it. Keep history nodes on the chat branch, not in the middle of your diffusion pipeline.
If your conversation comes back empty: check both session_file values character-for-character, and make sure the save node isn't still on clear. Multi-turn is the difference between "one-shot prompt enhancer" and a tool that actually remembers what you asked two runs ago - once this is wired, that's the workflow you'll keep going back to.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| action | COMBO | 4 options: load, load & append, clear, save | |
| session_file | STRING | /tmp/ComfyUI/custom_nodes/ApoStudio/prompts/session.json | — |
| history_inopt | APO_HISTORY | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| history_out | APO_HISTORY | — |
| history_json | STRING | — |
| history_md | STRING | — |
| turn_count | INT | — |