Skip to main content
Add persistent, per-user memory to a Haystack pipeline as two regular components: a retriever for the read side and a writer for the write side. Both drop into existing pipelines without restructuring. For a Haystack Agent, the package also replaces the agent’s earlier turns with Synap’s compaction.
Requires Python 3.11+. On haystack-ai 3.x, use maximem-synap-haystack 0.2.3 or later: earlier versions of SynapRecordHook recorded the whole history again on every turn.

Overview

This guide shows how to add Synap to a Haystack application to build pipelines that:
  • Retrieve user-scoped memories as standard Document objects in a RAG flow
  • Persist each conversation turn back to Synap so future runs benefit from it
  • Compose freely with any other Haystack component (rerankers, prompt builders, generators)
The Synap Haystack integration ships two drop-in pipeline components. Both follow Haystack’s component contract so you can wire them into pipelines exactly like any built-in component. Four more pieces cover chat pipelines and agents:

How the integration connects

You do not open a connection yourself. The first turn the writer or the store records opens Synap’s gRPC stream, and from then on turns, tool calls, tool results and reasoning go over it. Opening gets up to 10 seconds; if it fails, the integration tries once more a few seconds later, then no more than once a minute, and writes each turn over REST in the meantime, never both.
  • A stream your own code opened with listen() is used as is.
  • To keep the integration on REST, set SYNAP_AUTO_LISTEN=false in the environment, or call set_auto_listen(sdk, False) from synap_integrations_common.
  • Tool calls, tool results and reasoning exist only on the stream. Without one they are not reported, and the turns still are.
  • A store built with only a customer_id has no user to attribute a turn to, so it writes over REST.
  • Initialize the SDK first (below). Until it is initialized the integration cannot open the stream.
See gRPC vs REST for what the stream carries.

Setup

Install the package alongside Haystack:
The pip package is maximem-synap-haystack, but the import drops the maximem- prefix and uses underscores: from synap_haystack import ....
Configure your API key. Generate one from the Synap Dashboard.
.env
Initialize the SDK once at application startup:
See SDK Initialization for the full lifecycle and configuration options.

Basic integration

The smallest useful integration plugs SynapRetriever into a pipeline and routes its documents output to a prompt builder:
Retrieval failures degrade gracefully. SynapRetriever emits an empty documents list and logs an error, so the rest of the pipeline keeps running. To close the loop and persist new turns for future retrievals, add SynapMemoryWriter after the generator.

Core concepts

Retriever

SynapRetriever is a Haystack component that takes a query input and emits a documents output. Each returned Document has:
  • content: the memory text
  • meta["type"]: memory type (e.g. "fact", "preference")
  • meta["confidence"]: relevance score of a fact; other types carry their own score field, such as strength for a preference
The two retrieval modes trade latency against comprehensiveness: fast is lower-latency and suited to the hot path; accurate adds LLM-driven query decomposition and reranking for relationship-aware queries at a higher latency cost. Because the output shape matches Haystack’s standard Document, you can route it through any reranker, prompt builder, or filter that accepts documents.

Memory writer

SynapMemoryWriter is the write side. Place it at the end of a pipeline so each LLM reply is captured as a memory for future retrievals:
It accepts a documents input: Documents whose content is the message text and whose meta["role"] is "user" or "assistant". A generator’s replies are ChatMessages, not Documents, so a pipeline needs a small converter between them (see the example below); a document with any other role is skipped. It also accepts a messages input of ChatMessages. Connect an agent’s or generator’s messages or replies there and its tool calls, tool results and reasoning reach Synap with the turns, which a Document cannot carry. It emits written_count, failed_count, skipped_count and first_error. Write failures surface explicitly. SynapMemoryWriter raises SynapIntegrationError when every message fails, so the pipeline knows persistence broke.

Short-term history for an agent

A plain pipeline sends whatever messages you give it, so there is nothing to replace. A Haystack Agent keeps its own message history, and Synap’s compaction can stand in for the turns it covers. Once Synap has compacted the conversation, the model sees the compaction as a system message, then the turns Synap holds after it, then the current turn. Until then, and whenever Synap cannot answer, the agent’s messages are left alone. The wiring depends on your Haystack version. haystack-ai 3.x takes hooks. SynapHistoryHook runs before each model call and does the replacing. SynapRecordHook runs after the run and records its turns, tool calls and results. Both take the store and the Synap conversation id:
haystack-ai 2.x has no hooks. Install haystack-experimental and pass the store as the experimental agent’s chat_message_store. It reads history from Synap and writes the run back through the same store:
The hooks need haystack-ai 3.x: they import on 2.x, but only an agent that takes hooks runs them. The store marks each message it has recorded or retrieved, so passing it to an agent in more than one slot does not record a turn twice. SynapShortTermContext(sdk, conversation_id) is for a pipeline you wire by hand. It emits Synap’s compaction, followed by the optional system text you give it, on its synap_st output for a prompt builder template. Before the first compaction the output is your system text alone, empty by default.

Complete example: full RAG pipeline with memory loop

The following pipeline retrieves user-scoped memories, builds a prompt, generates a response, and writes both the user’s question and the assistant’s reply back to Synap, in one pass:
The generator’s answer is in result["llm"]["replies"][0].text. Haystack only returns the outputs of the last components in the graph, so ask for the generator explicitly with include_outputs_from, and read the write summary from result["writer"]. Three things to notice in this pattern:
  1. Memory is another retriever. SynapRetriever emits standard Document objects, so you can mix it with any document store retriever via a DocumentJoiner if you want corpus context too.
  2. The write loop closes itself. Each pipeline run ends by persisting the question and the reply, so every subsequent run benefits from accumulating context.
  3. Scope is bound at construction. The retriever and writer carry the user/customer scope; the pipeline graph never needs to know about user identity.

Advanced patterns

Multi-tenant scoping

Both components accept the standard scoping triple: user_id (required), customer_id (B2B instances only), optional conversation_id. customer_id is required on B2B Synap instances and not accepted on B2C (single-tenant) ones, where a call carrying it is rejected with HTTP 400. See Memory Scopes.
For multi-tenant services, build the pipeline (or at least the retriever/writer components) per request so each invocation has the correct scope baked in.

Combining with document retrieval

SynapRetriever’s output is a standard Document list, so it slots into a DocumentJoiner next to your existing document store retriever:
User-specific facts and corpus chunks come back as a single ranked list to the prompt builder.

Failure semantics

The integration follows the Synap-wide contract:
  • SynapRetriever degrades gracefully: emits an empty documents list and logs an error if Synap is unreachable.
  • History reads degrade gracefully: SynapHistoryHook leaves the agent’s messages untouched, and SynapMemoryStore.retrieve_messages returns an empty list.
  • SynapMemoryWriter surfaces failures: raises SynapIntegrationError so the pipeline (and caller) know persistence failed.
This is by design: read failures shouldn’t break a user-facing turn, but silent write failures would let the memory drift away from reality.

Going further


Next steps

LangChain

Memory and retriever for LangChain.

LlamaIndex

BaseMemory and retriever for LlamaIndex.

Context Fetch

The retrieval API that powers SynapRetriever: modes, scopes, and response shapes.

Memory Scopes

How user_id, customer_id, and conversation_id interact across reads.