CogneeWriter
Write ChatMessage objects into a Cognee knowledge graph memory.
Key Features
- Persists
ChatMessageobjects into a Cognee knowledge graph for long-term agent memory. - Supports both the permanent knowledge graph (no
session_id) and session-scoped caching. - The writer's
session_idoverrides the store'ssession_id, so one store can back multiple writers targeting different memory tiers. - Passes written messages through unchanged so they can continue to flow to downstream components.
- Works with
CogneeRetrieverto form a complete memory write/read pair in agent pipelines.
Configuration
- Drag the
CogneeWritercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Configure the
memory_storewith aCogneeMemoryStore, setting thedataset_nameand optionallysession_id. - Optionally set a
session_idon the writer to override the store's session for this writer's writes.
- Configure the
Connections
CogneeWriter receives a list of ChatMessage objects and an optional user_id. It stores the messages in Cognee and passes them through unchanged via its messages_written output.
Use this component in agent pipelines to persist conversation history or other messages into Cognee's knowledge graph. Connect the source of messages (for example, an LLM reply) to its messages input.
Source Code
To check this component's source code, open memory_writer.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
CogneeWriter:
type: haystack_integrations.components.writers.cognee.memory_writer.CogneeWriter
init_parameters:
memory_store:
type: haystack_integrations.memory_stores.cognee.memory_store.CogneeMemoryStore
init_parameters:
search_type: GRAPH_COMPLETION
top_k: 5
dataset_name: haystack_memory
session_id:
self_improvement: true
timeout: 300
session_id:
Using the Component in a Pipeline
This is an example of an agent pipeline that writes LLM replies into Cognee memory for future retrieval.
# haystack-pipeline
components:
llm:
type: haystack.components.generators.chat.openai.OpenAIChatGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-4o-mini
CogneeWriter:
type: haystack_integrations.components.writers.cognee.memory_writer.CogneeWriter
init_parameters:
memory_store:
type: haystack_integrations.memory_stores.cognee.memory_store.CogneeMemoryStore
init_parameters:
search_type: GRAPH_COMPLETION
top_k: 5
dataset_name: haystack_memory
session_id:
self_improvement: true
timeout: 300
session_id:
connections:
- sender: llm.replies
receiver: CogneeWriter.messages
max_runs_per_component: 100
metadata: {}
inputs:
messages:
- llm.messages
outputs:
messages_written: CogneeWriter.messages_written
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
messages | List[ChatMessage] | The messages to persist into Cognee memory. |
user_id | Optional[str] | Cognee user UUID to scope the write to a specific user. |
Outputs
| Parameter | Type | Description |
|---|---|---|
messages_written | List[ChatMessage] | The same messages that were written, passed through unchanged. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
memory_store | CogneeMemoryStore | The backing CogneeMemoryStore to write into. Configure it with dataset_name and optionally session_id. | |
session_id | Optional[str] | None | Overrides the store's session_id for this writer's writes. Use to write to the session-cache tier instead of the permanent graph. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
messages | List[ChatMessage] | The messages to persist into Cognee memory. | |
user_id | Optional[str] | None | Cognee user UUID to scope the write to a specific user. |
Related Information
Was this page helpful?