Requires Python 3.11+.
Overview
This guide shows how to add Synap to an OpenAI Agents SDK application to build agents that:- Recall user-specific facts, preferences, and past conversations
- Persist new information surfaced during a conversation for future runs
- Stay fully in control of when memory is queried or written
- Send the model Synap’s compaction of a long conversation instead of every earlier turn
function_tool(...) helper from agents (the name_override argument lives on function_tool, not on the FunctionTool class). It also ships run hooks.
How the integration connects
The tools call Synap over the SDK when the model invokes them. The run hooks use the gRPC stream, and you do not open it yourself. The first report opens it, and from then on the user’s turn, tool calls, tool results, reasoning and the reply go over it. Opening gets up to 10 seconds; if it fails, the hooks try once more a few seconds later, then no more than once a minute, and write each turn over REST in the meantime, never both. The hooks never raise inside your run.- A stream your own code opened with
listen()is used as is. - To keep the hooks on REST, set
SYNAP_AUTO_LISTEN=falsein the environment, or callset_auto_listen(sdk, False)fromsynap_integrations_common. - Tool calls, tool results and reasoning exist only on the stream. Without one they are not reported, and the turns still are.
- The REST fallback needs a
conversation_id, so pass one toSynapRunHooks. - Initialize the SDK first (below). Until it is initialized the hooks cannot open the stream.
Setup
Install the package alongside the OpenAI Agents SDK. It requiresopenai-agents 0.2.9 or later, the first release with both the on_llm_start hook and call_model_input_filter that the run hooks use, and it installs maximem-synap and maximem-synap-integrations-common as dependencies.
The pip package is
maximem-synap-openai-agents, but the import drops the maximem- prefix and uses underscores: from synap_openai_agents import .....env
Basic integration
The smallest useful integration registers both tools on an agent and runs a query. The agent reads its tool descriptions, decides whether to search or store, and calls the matching function:user_id, optional customer_id) is bound when you construct the tool: the agent only ever sees the query and content parameters, never the user identity. This keeps the model from leaking or spoofing user IDs.
Core concepts
Search tool
create_search_tool returns an async callable that takes a natural-language query and returns a list of memory objects.
accurate mode and returns Synap’s formatted context as one string, or "No relevant memories found." when there is none. The agent reads the text and can quote or reason over it directly.
Search failures surface: the tool logs the error and raises SynapIntegrationError. Like any function tool failure, the Agents SDK hands the model a generic error message, and the model can carry on without recall.
Store tool
create_store_tool returns a companion callable that ingests a new memory.
"Memory stored (ingestion_id: ...)" on success. Synap extracts memories from the content asynchronously, so a memory stored now may take a moment to become searchable. Store failures surface explicitly: the tool raises SynapIntegrationError so the agent (and you) know if persistence failed.
Run hooks and compacted history
SynapRunHooks records what the agent does and, through the run config, sends the model Synap’s compaction of the conversation. Pass both to Runner.run with run_kwargs():
report_tool_results=False to keep them out.
Compaction is not on by default. A RunHooks object cannot change what the model is sent. With hooks=hooks alone the run is recorded, but the model still receives the Agents SDK’s own history. run_kwargs() returns hooks= plus a run_config= whose call_model_input_filter does the replacing. Once Synap has compacted the conversation, that filter replaces every item before the current turn with Synap’s compaction and the turns after it. The current turn, with its tool calls and outputs, is left as it is. Until then, and whenever Synap cannot answer, the SDK’s items go through untouched. It works the same with a Session or with an input list you manage.
If you already build a RunConfig, pass its arguments to run_kwargs(), for example hooks.run_kwargs(workflow_name="support"). A call_model_input_filter of your own runs after Synap’s.
Compaction does not apply when the history lives on OpenAI’s side (previous_response_id or conversation_id on the run): there the input holds only the new items, and a filter cannot replace what is not in it.
synap_st_instructions(sdk, conversation_id, system="...") is the lighter option. It returns an instructions callable that adds the compaction above your own instructions and does not replace any turns. Use it when you cannot change the run config. If you use both, Synap’s compaction is sent once.
Build one SynapRunHooks per conversation. It pairs each tool call with its result by tool name, which assumes the calls to one tool run one after another.
Complete example: assistant with explicit memory control
The following agent is told to consult its memory before answering and store anything the user shares. The Synap tool calls only happen when the model elects to invoke the tools. The hooks record every run and send the model Synap’s compaction once there is one:- The agent owns the memory loop. Search and store calls are model-driven, not framework-driven; the prompt steers the behavior.
- Scope is bound at construction. The model never sees
user_idorcustomer_id, so even a prompt-injection attempt cannot make it query memories for someone else. - The hooks own the record. Every run is reported whether or not the model called a tool, and
run_kwargs()is what puts Synap’s compaction in front of the model.
Advanced patterns
Multi-tenant scoping
Both factories andSynapRunHooks accept the same scoping pair: user_id (required), and customer_id on B2B instances only. 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.
Per-request scoping
If your service handles many users in one process, build a fresh pair of tools and a freshSynapRunHooks per request rather than caching them; each agent run should have its scope baked in to prevent cross-user leakage:
Failure semantics
The integration follows the Synap-wide contract:- Tool failures surface explicitly:
synap_searchandsynap_storelog the error and raiseSynapIntegrationError, which the Agents SDK turns into a generic error message for the model. - Hooks never raise: a failed report or a failed history read is logged, and the run carries on with the SDK’s own input.
Going further
- Patterns overview: reusable memory patterns across frameworks.
- Cookbook overview: end-to-end worked examples.
Next steps
Pydantic AI
Type-safe deps and tools for Pydantic AI agents.
AutoGen
BaseTool implementations for AutoGen agents.Context Fetch
The retrieval API that powers
synap_search: modes, scopes, and response shapes.Memory Scopes
How
user_id and customer_id interact across reads and writes.