Skip to content

examples: add persistent memory cookbook with Dakera integration (31 tests) - #2405

Open
ferhimedamine wants to merge 1 commit into
567-labs:mainfrom
ferhimedamine:feat/dakera-persistent-memory
Open

examples: add persistent memory cookbook with Dakera integration (31 tests)#2405
ferhimedamine wants to merge 1 commit into
567-labs:mainfrom
ferhimedamine:feat/dakera-persistent-memory

Conversation

@ferhimedamine

Copy link
Copy Markdown

Closes #2404

Summary

Adds a complete cookbook recipe for persistent, cross-session memory using Dakera — a self-hosted, decay-weighted vector memory server.

Instructor currently has no memory example. This PR adds one using Instructor's native hook system, matching the pattern already established by examples/tracing_with_langfuse.md.

Files added

examples/persistent_memory_dakera/
├── dakera_memory.py         # DakeraMemory, AsyncDakeraMemory, DakeraMemoryHook, build_context_messages
├── run.py                   # 4 runnable scenarios
└── test_dakera_memory.py    # 31 tests, all passing (mocked)

docs/examples/persistent_memory_dakera.md  # Cookbook page
mkdocs.yml                                 # 1 line added under Cookbook:

How DakeraMemoryHook works

Uses Instructor's client.on() hook system — same mechanism as the Langfuse tracing integration:

import instructor
from examples.persistent_memory_dakera.dakera_memory import DakeraMemory, DakeraMemoryHook

client = instructor.from_provider("openai/gpt-4o-mini")
mem = DakeraMemory(base_url="http://localhost:3300", api_key="demo", agent_id="user-123")
hook = DakeraMemoryHook(mem)
hook.attach(client)   # 3 lines — that's the integration
  • completion:kwargs — searches Dakera for the top-K relevant memories for the current prompt and prepends them as a system message block
  • completion:response — stores the user prompt + model reply in Dakera after the call

Errors from Dakera are swallowed (non-fatal) — the hook never crashes an LLM call.

Four runnable examples

1. Manual store + recall

mem = DakeraMemory(base_url="http://localhost:3300", agent_id="user-123")
mem.store("I prefer concise technical answers.")

messages = [{"role": "user", "content": "How should you respond to me?"}]
messages = build_context_messages(messages, mem.search("answer style preference"))
reply = client.create(messages=messages, response_model=Reply)

2. Zero-boilerplate hook (auto-recall + auto-store)

hook.attach(client)
# All subsequent client.create() calls automatically recall and store

3. Structured extraction pipeline

class UserProfile(BaseModel):
    name: str
    preferences: list[str]

profile = client.create(
    messages=[{"role": "user", "content": "I'm Alex, I love Python and Rust."}],
    response_model=UserProfile,
)
# profile.name, profile.preferences are structured — and the exchange is persisted

4. Multi-turn CLI chat loop (persists across restarts)

python examples/persistent_memory_dakera/run.py

Tests

31 tests using unittest.mock — no live server or LLM credentials required:

test_dakera_memory.py
  test_store_success ✓
  test_store_sends_auth_header ✓
  test_store_http_error_raises ✓
  test_search_returns_hits ✓
  test_search_empty_result ✓
  test_search_http_error_raises ✓
  test_forget_by_ids ✓
  test_forget_all ✓
  test_forget_http_error_raises ✓
  test_build_context_messages_no_hits ✓
  test_build_context_messages_with_hits ✓
  test_hook_attach_registers_handlers ✓
  test_hook_completion_kwargs_injects_memories ✓
  test_hook_completion_kwargs_no_recall_when_top_k_zero ✓
  test_hook_completion_response_stores_exchange ✓
  test_hook_errors_are_swallowed ✓
  ... (31 total)

Prerequisites

# Start Dakera (Docker)
docker run -p 3300:3300 -e DAKERA_API_KEY=demo ghcr.io/dakera-ai/dakera:latest

# Install
pip install instructor openai httpx

No new dependencies added to pyproject.tomlhttpx is already a transitive dependency of instructor.

Adds a new cookbook entry showing how to give any Instructor application
persistent, decay-weighted long-term memory using Dakera
(https://dakera.ai) — a self-hosted vector memory server.

## What's added

### `examples/persistent_memory_dakera/`

- `dakera_memory.py` — integration helper:
  - `DakeraMemory`: sync httpx client for `/v1/memory/store`, `/v1/memory/search`,
    `/v1/memory/forget`
  - `AsyncDakeraMemory`: async variant using `httpx.AsyncClient`
  - `DakeraMemoryHook`: attaches to any instructor client via `client.on()`; auto-recalls
    before each call (`completion:kwargs`), auto-stores after (`completion:response`);
    Dakera errors are silently swallowed so they never crash an LLM call
  - `build_context_messages(messages, hits)`: inject recalled memories as a leading
    system message

- `run.py` — four runnable examples:
  1. Manual store + recall with `build_context_messages`
  2. Zero-boilerplate hook-based approach (`hook.attach(client)`)
  3. Structured extraction pipeline: `UserProfile` Pydantic model
  4. Multi-turn CLI chat loop that persists across restarts

- `test_dakera_memory.py` — 31 tests, all passing (fully mocked, no live server needed)

### `docs/examples/persistent_memory_dakera.md`

Cookbook page with usage examples, API reference, and Quick Start.

### `mkdocs.yml`

Entry added under `Cookbook:` alongside `Tracing with Langfuse`.

## Quick start

```bash
docker run -p 3300:3300 -e DAKERA_API_KEY=demo ghcr.io/dakera-ai/dakera:latest
pip install instructor openai httpx

python examples/persistent_memory_dakera/run.py
```

Co-Authored-By: Paperclip <noreply@paperclip.ing>
jxnl added a commit that referenced this pull request Aug 3, 2026
## Summary

- accumulate declared, nested, and unknown numeric OpenAI/Anthropic
usage fields across retries while preserving non-numeric metadata
- add corrective feedback when a Responses API retry receives no tool
call
- preserve raw iterable type hints through sync and async v2
parallel-tool wrappers
- strengthen API-key-free coverage for current and future SDK usage
counters

## Consolidated and superseded items

- closes #2493
- consolidates contributor work from #2498, #2500, and #2501 with
original commit authorship preserved
- supersedes #2497 because it drops unknown `model_extra` counters
- supersedes #2499 because its hand-maintained provider field lists
would drift as SDKs evolve

## Validation

- focused changed-surface suite: `110 passed`
- broad offline v2/coverage suite: `2368 passed, 91 skipped, 73
deselected`
- Ruff check and format check: passed
- scoped `ty check`: passed
- `uv lock --check`: passed
- pre-commit hooks and `git diff --check`: passed

The 73 deselected tests require live provider credentials. An unfiltered
local run confirmed its 22 failures were provider network connections in
the restricted environment; GitHub provider jobs remain the
authoritative validation for those paths.

## Intentionally skipped

- provider additions or expansions: #2436, #2435, #2423, #2409, #2384,
#2322, #2306, #2298, #2283, #2168, #2086; issues #2408, #2383, #2365,
#2260, #2084, #2076
- broad architecture, product, security, or streaming decisions: #2394,
#2392, #2357, #2356, #2355, #2351, #2321, #2307, #2287, #2263; issues
#2479, #2403, #2393, #2391, #2316, #2272, #2056
- dependency batch: #2433
- nontrivial examples and editorial/resource additions: #2468, #2405,
#2401, #2354, #2346, #2311, #2305; issue #2404

These remain open because they need dedicated product, architecture,
provider, security, dependency, or editorial review and are not required
for the `1.15.5` patch release.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes retry usage totals and reask message content on failure paths;
scope is limited and heavily covered by tests, with no auth or
data-store changes.
> 
> **Overview**
> Bundles three v2 retry and wrapper fixes for a patch release.
> 
> **Retry usage accounting** replaces hand-maintained token field sums
with generic `_accumulate_models` on Pydantic usage objects. Numeric
fields (including nested models and `model_extra` counters) add across
retries; booleans and other non-numeric metadata are not treated as
billable. OpenAI and Anthropic paths share this logic.
> 
> **OpenAI Responses reask** appends a user correction when
`RESPONSES_TOOLS` validation fails but the output has no tool calls
(e.g. reasoning-only), so retries include feedback instead of repeating
the same request.
> 
> **Parallel tools** in `patch_v2` skips `prepare_response_model` and
does not replace `response_model` with the handler’s prepared wrapper
for parallel modes, keeping raw `Iterable[...]` hints so schemas and
parsed results include every member type.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
bbddca1. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add persistent memory cookbook with Dakera integration

1 participant