Skip to content

Python: [Bug]: RedisContextProvider saves the whole conversation again on every AG-UI run, so memory search returns copies and misses facts #10127

Open

Description

Observed Behavior

When an agent with RedisContextProvider is served through the AG-UI endpoint (add_agent_framework_fastapi_endpoint), every run saves the whole conversation into Redis again, not only the new messages. The AG-UI TypeScript client (@ag-ui/client HttpAgent) sends all earlier messages on every run, and the provider stores everything it receives as new documents.

In my test, a chat with 15 short team facts (30 messages) left 240 memory documents in Redis. "We deploy to westus3." was stored 14 times. Earlier messages pile up the most, so they fill the memory search results. In my runs, all 10 search results for "Where do we deploy, and who approves rollbacks?" were the same message, "Rollbacks need approval from Mira.", and the westus3 fact never reached the model. In a new chat the agent answered "I don't have the deployment location. Mira approves rollbacks."

The same chat sent one message at a time stored 30 documents and answered "We deploy to westus3. Mira approves rollbacks."

Storage grows with the square of the conversation length. Without an embeddings cache every copy is also a paid embedding call (in my runs without a cache: 242 texts embedded against 32). With the cache from the redis_basics sample, repeated texts mostly come from the cache, but the copies and the crowded search results are the same.

Expected Behavior

A message that is already stored for the same scope should not be stored again. The memory should hold one document per message, so search results stay distinct, no matter whether the client sends only new messages or the whole conversation.

Steps to Reproduce

  1. Start Redis with the command from the package README: docker run --rm --name agent-framework-redis -p 127.0.0.1:6379:6379 redis:8.0.3
  2. pip install --pre agent-framework-redis agent-framework-ag-ui agent-framework-openai and set OPENAI_API_KEY.
  3. Run python repro.py full. The client sends the whole conversation on every run, like @ag-ui/client does.
  4. Run python repro.py new. The client sends only the latest message.
  5. Compare the document counts and the answers in the new chat.

Minimal Reproduction

import asyncio
import json
import sys
import uuid

import httpx
from agent_framework import Agent
from agent_framework.openai import OpenAIChatCompletionClient
from agent_framework.redis import RedisContextProvider
from agent_framework_ag_ui import add_agent_framework_fastapi_endpoint
from fastapi import FastAPI
from redis import Redis
from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.utils.vectorize import OpenAITextVectorizer

REDIS_URL = "redis://localhost:6379"
FACTS = [
    "Our release day is Thursday.", "We deploy to westus3.", "Rollbacks need approval from Mira.",
    "The on-call engineer this week is Priya.", "Our database is Postgres 16.", "The payments API runs on port 8443.",
    "Incident reviews happen every Monday at 10am.", "Feature flags live in LaunchDarkly.",
    "Our staging cluster is called blue-staging.", "Secrets are stored in Azure Key Vault named pay-kv.",
    "The mobile team lead is Omar.", "Load tests run nightly at 2am.", "The SLA for refunds is 48 hours.",
    "Logs go to Grafana Loki.", "Code freeze starts the 20th of each month.",
]
# "full" sends the whole conversation on every run, like @ag-ui/client (HttpAgent) does.
# "new" sends only the latest message.
MODE = sys.argv[1] if len(sys.argv) > 1 else "full"


async def ask(client: httpx.AsyncClient, thread_id: str, messages: list[dict[str, str]]) -> tuple[str, str]:
    body = {"threadId": thread_id, "runId": uuid.uuid4().hex, "messages": messages,
            "tools": [], "context": [], "state": {}, "forwardedProps": {}}
    reply, message_id = "", uuid.uuid4().hex
    async with client.stream("POST", "http://test/agent", json=body) as response:
        async for line in response.aiter_lines():
            if line.startswith("data:"):
                event = json.loads(line[5:])
                if event["type"] == "TEXT_MESSAGE_START":
                    message_id = event["messageId"]
                elif event["type"] == "TEXT_MESSAGE_CONTENT":
                    reply += event["delta"]
                elif event["type"] == "RUN_ERROR":
                    raise RuntimeError(event)
    return reply, message_id


async def main() -> None:
    prefix = f"memory_dup_{MODE}_{uuid.uuid4().hex[:6]}"
    agent = Agent(
        client=OpenAIChatCompletionClient(model="gpt-6-luna"),
        instructions="Be very brief.",
        context_providers=[
            RedisContextProvider(
                redis_url=REDIS_URL,
                index_name=prefix,
                prefix=prefix,
                application_id="repro",
                user_id="user-1",
                # Same vectorizer and embeddings cache as the redis_basics sample.
                redis_vectorizer=OpenAITextVectorizer(
                    model="text-embedding-3-small",
                    cache=EmbeddingsCache(name=f"{prefix}_cache", redis_url=REDIS_URL),
                ),
                vector_field_name="vector",
                vector_algorithm="hnsw",
                vector_distance_metric="cosine",
            )
        ],
    )
    app = FastAPI()
    add_agent_framework_fastapi_endpoint(app, agent, "/agent", single_user=True)
    redis = Redis.from_url(REDIS_URL)

    async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), timeout=180) as client:
        thread_id, messages = uuid.uuid4().hex, []
        for fact in FACTS:
            messages.append({"id": uuid.uuid4().hex, "role": "user", "content": fact})
            reply, reply_id = await ask(client, thread_id, messages if MODE == "full" else messages[-1:])
            messages.append({"id": reply_id, "role": "assistant", "content": reply})

        stored = [redis.hget(key, "content").decode() for key in redis.scan_iter(match=f"{prefix}:*")]
        print(f"mode={MODE}: {len(messages)} messages in the chat, {len(stored)} memory documents in Redis")
        print(f'  "We deploy to westus3." stored {stored.count("We deploy to westus3.")} times')

        question = {"id": uuid.uuid4().hex, "role": "user", "content": "Where do we deploy, and who approves rollbacks?"}
        answer, _ = await ask(client, uuid.uuid4().hex, [question])
        print(f"  new chat: {question['content']!r} -> {answer!r}")


asyncio.run(main())

Output on the released packages:

mode=full: 30 messages in the chat, 240 memory documents in Redis
  "We deploy to westus3." stored 14 times
  new chat: 'Where do we deploy, and who approves rollbacks?' -> 'I don’t have the deployment location. Mira approves rollbacks.'
mode=new: 30 messages in the chat, 30 memory documents in Redis
  "We deploy to westus3." stored 1 times
  new chat: 'Where do we deploy, and who approves rollbacks?' -> 'We deploy to westus3. Mira approves rollbacks.'

Current main (3af8584) gives the same counts.

Error Messages and Stack Traces


Package Versions

agent-framework-core: 1.24.0, agent-framework-redis: 1.0.0b261030, agent-framework-ag-ui: 1.7.1, agent-framework-openai: 1.18.0 (also reproduced on main at 3af8584)

Python Version

Python 3.12

Operating System

macOS

Regression

Unknown

Additional Context

Cause: RedisContextProvider.after_run stores every message in context.input_messages plus the response (_context_provider.py#L214), and _add calls self.redis_index.load(prepared) without keys (#L391), so RedisVL gives every document a new random key. Nothing checks whether the message is already stored.

The AG-UI endpoint passes the whole incoming conversation to agent.run, and it creates a new session for each request, so a session-state check cannot catch it either. #7802 describes the AG-UI snapshot messages as the conversation-history authority, so the full conversation reaching the agent looks like current intended behavior. History providers were already fixed for this same replay in #7242 and #8269. Memory providers were not.

Other providers:

  • CosmosMemoryContextProvider has the same pattern. Behind the AG-UI endpoint, 6 runs sent 42 turn writes for 12 unique messages (I used a recording memory_client, not a real Cosmos account). The toolkit gives each write a new id and counts it toward its extraction trigger.
  • The experimental MemoryContextProvider already deduplicates its transcript, but its extraction prompt gets the whole resent conversation every run (145 tokens on run 1, 524 on run 15).
  • Mem0ContextProvider was not meaningfully affected in my test (2026-10-30), because Mem0 deduplicates extracted facts itself.

Suggested fix: give each memory document a key derived from its scope fields, role and text. _add then checks which keys already exist and embeds and loads only the new ones. I did not use the message id, because the assistant reply comes back with a different id than the one stored (the stored id is the chatcmpl-... id, while the AG-UI stream uses its own id). Two side effects: the same text from the same role in one conversation is stored once, and documents saved before the change stay as they are, so an existing conversation gets one more copy of each message and then stays stable. I tested it on main with the script above (30 documents and the right answer in both modes), with three concurrent runs of the same conversation (22 documents, against 84 without the fix) and with 2,000 resent messages. I also added unit tests and a live Redis test. The Redis test suite and the CI lint and type checks pass.

AI tools assisted with the investigation, the reproduction script and the drafting of this issue. I ran the reproduction and verified the results myself.

I have a fix ready and can open a PR once the approach is confirmed.

Acknowledgements

  • I searched existing issues and did not find a duplicate.
  • I personally verified this behavior and the reproduction details are authentic.
  • I will wait for explicit maintainer agreement before starting implementation of a non-trivial change.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Nov 5, 2026
  2. added
    reproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
    on Nov 5, 2026
  3. github-actions commented on Nov 5, 2026

    @github-actions
    Contributor

    🤖 Automated triage reproduction notes (agent-authored — trust but verify)

    Agent analysis

    The bug occurs in python/packages/redis/agent_framework_redis/_context_provider.py::RedisContextProvider.after_run at lines 214-230 and _add at lines 344-391 when successive runs contain overlapping message histories. Minimal repro: call after_run for one session with ["First fact"], then with ["First fact", "Second fact"]; the RedisVL hash writer is invoked three times instead of twice.

    • Failing test: python/packages/redis/tests/test_context_provider_sdk.py::test_after_run_does_not_store_replayed_input_messages
    • Files examined: python/packages/redis/agent_framework_redis/_context_provider.py, python/packages/redis/tests/test_context_provider_sdk.py, python/packages/ag-ui/agent_framework_ag_ui/_agent_run.py, python/packages/redis/pyproject.toml
    • Tests run: python/packages/redis/tests/test_context_provider_sdk.py, test_after_run_does_not_store_replayed_input_messages
    • Reported version: 1.0.0b261030
    • Current version: 1.0.0b261030
  4. added
    memoryUsage: [Issues, PRs], Target: memory related features
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Nov 5, 2026
  5. eavanvalkenburg commented on Nov 9, 2026

    @eavanvalkenburg
    Member

    /plan Investigate the independently confirmed Redis memory replay defect: current-main after_run/_add submit three unkeyed documents for two logical messages when inputs progress from [first] to [first, second]. A no-service index-load probe confirms this path; the unrelated text-search escaping fix does not address it. Establish the smallest correct fix before implementation readiness. Distinguish replayed transcript prefixes from legitimate repeated identical messages, edits, and repeated turns; do not treat content hashing or whitespace stripping as an agreed identity policy. Account for AG-UI assistant wire IDs differing from model message IDs, full snapshots versus delta inputs, stable scope isolation, fresh request sessions, concurrent runs, embedding work, borrowed index prefixes/separators, and existing random-key data/migration. Compare persistent-provider idempotency with existing core/AG-UI reconciliation primitives and review the repeated-turn preservation contract from #8800 as well as history replay fixes #7242/#8269. PR #10179 proposes one approach and is not accepted by this invocation; its author differs from the issue author, so do not infer author-priority ownership or close it. Check whether Cosmos memory shares the defect without silently expanding implementation scope. Produce focused tests and a production-scope estimate; keep assignment/readiness deferred while policy and scope remain unresolved.

  6. github-actions commented on Nov 9, 2026

    @github-actions
    Contributor

    Summary

    RedisContextProvider.after_run stores every input message plus the response, and _add calls redis_index.load(prepared) with no keys, so each call creates new random-key documents (contextprovider.py). I checked this by reading the code; I did not run anything. Any caller that resends a transcript prefix, such as the AG-UI endpoint, therefore gets [first] then [first, second] stored as three documents. The issue's wire/ID claims are as reported and I have not checked them against the AG-UI source. The stored message_id field is written but never read back.

    Recommendation

    A code change is warranted. This is a framework defect, not a sample or docs matter. Search results are crowded by copies, and the cost grows quadratically with embedding calls. The only workaround is to send deltas from the client, which the AG-UI client does not do. The exact identity policy is not agreed yet, so I recommend deferring assignment and "ready" status.

    Findings on the points you asked about

    • Core primitives don't transfer directly. filter_new_messages in _sessions.py (from Python: fix: prevent superlinear history growth by deduplicating messages in save_messages #7242/Python: Prevent replayed history from being re-persisted by CosmosHistoryProvider #8269, with the repeated-turn preservation from Python: fix(core): preserve repeated turns in history persistence #8800) compares the incoming list against the ordered stored history. The Redis memory index is an unordered vector store with no ordered read-back per session. Its two fallbacks (content identity when there is no ID, and keeping a lone ID-less user message) are exactly the ambiguity Python: fix(core): preserve repeated turns in history persistence #8800 chose to resolve toward keeping.
    • IDs. User messages carry client IDs, so ID matching works for them. Assistant messages do not: the stored ID is the model's (chatcmpl-...), while AG-UI replays its own wire ID. AG-UI already handles this in _snapshot_messages_match in runcommon.py: when assistant IDs differ it falls back to canonical content. That precedent exists, but it is for snapshot-vs-stored-thread comparison, not persistent-memory writes.
    • Hashing is a policy choice, not a neutral one. A hash of scope+role+text would treat a legitimate repeat ("yes" in turn 3 and turn 9) as a replay, and would not treat an edited message as new-and-old. That may be acceptable for a memory (search) store, which is not a transcript, but it must be decided explicitly. I would not add whitespace stripping either (it changes what is stored).
    • Scope isolation. Dedup must be keyed within the same application/agent/user/conversation scope tags, and must use the provider's configured scope, so two users saying the same thing are never merged. AG-UI creates a fresh session per request, so session state cannot hold the "already stored" record; it must come from Redis.
    • Borrowed indexes. With a caller-supplied redis_index, the key prefix and separator come from the index schema (prefix, key_separator are read in the schema check). Derived keys must be built from the index's prefix/separator, not assumed ones, or they may fall outside the index.
    • Concurrency. Check-then-write races across concurrent runs. A deterministic key plus a write that does not overwrite or duplicate (e.g. an existence check, or an atomic set-if-absent) is needed, rather than a read-then-write only.
    • Embedding cost. Dedupe before embedding so replays do not cost embedding calls.
    • Existing data. Old documents have random keys. They will not be recognised, so each conversation gets one extra copy and then stays stable. A migration is optional and a design decision.
    • Cosmos. CosmosMemoryContextProvider.after_run loops over context.input_messages and calls the toolkit's write method for each one, with no replay check. So it has the same pattern at the provider level; whether the toolkit dedupes internally is not visible in this repo and I could not check it. I recommend a separate issue for it, not silently folding it in.

    Questions before a plan (policy decisions)

    1. Identity: Should memory dedup treat the same role+text in the same conversation as one memory (accepting that legitimate repeated messages collapse), or should it only skip a replayed prefix and keep real repeats? The first is simpler and fits a search index; the second needs order knowledge the index lacks.
    2. Assistant messages: Are we fine matching assistant messages by content (as AG-UI already does) because wire IDs differ?
    3. Migration: Is "no migration, one extra copy for existing conversations" acceptable?
    4. Scope: Is Redis-only the intended scope for this issue, with Cosmos tracked separately?

    Tentative plan (only once the answers above are settled; nothing has been changed)

    1. In after_run/_add, compute a deterministic per-document key from the scope tags, role, and the agreed identity, honouring the index prefix and separator.

    2. Check which keys already exist, then embed and load only the missing ones, using a write that is safe under concurrency.

    3. Tests, in python/packages/redis/tests/:

      • [first] then [first, second] stores two documents and embeds the replayed message only once.
      • Delta inputs are unchanged.
      • Different users, agents, or conversations do not collide.
      • A new session with the same replay does not duplicate.
      • Assistant response ID versus replayed wire ID.
      • Concurrent runs, plus a borrowed index with a custom prefix or separator.
      • Behavior for legitimate repeated and edited messages matches the agreed policy.
      • Existing random-key documents are left alone.
    4. Estimate: a small production change (roughly one file, a key helper plus an existence check in _add), with most of the effort in the tests above. The estimate grows if the replayed-prefix policy (question 1, second option) is chosen.

    I did not inspect or rely on the open PR #10179. This comment does not endorse it or any other proposed approach.

    Generated by Issue Planning for #10127 · copilot · auto · ~25 AIC · ⌖ ~1 AIC · ⊞ 4.8K · ◷

  7. LUCKYREDDY31 commented on Nov 9, 2026

    @LUCKYREDDY31
    Author

    Hi Eduard van Valkenburg (@eavanvalkenburg), here are results for some of the points the plan could not check. Run on main 13956ad with the real AG-UI endpoint (add_agent_framework_fastapi_endpoint), a client that resends the full transcript like @ag-ui/client, gpt-6-luna, text-embedding-3-small and Redis 8.0.3, 15 messages.

    Message ids. User messages keep their id: all 120 stored copies have the id the client sent. Assistant messages do not: each reply is stored once with the model's id (chatcmpl-...) and every resent copy with the id the client received on the stream (a UUID). So matching on message_id would still leave two copies of every assistant reply.

    Embedding calls.

    Full transcript Only the new message
    Documents stored 240 30
    Texts sent to the embeddings API, no cache 255 45
    Texts sent to the embeddings API, with EmbeddingsCache as in redis_basics 44 30

    The API counts include the search query embedding of each run. With the sample's cache most of the extra embedding cost goes away, but the 240 documents are still stored.

    Cosmos. CosmosMemoryContextProvider behind the same endpoint (with a recording memory_client, not a real Cosmos account) made 42 upsert_memory calls for 12 unique messages over 6 turns (2, 4, 6, 8, 10, 12 per run), and 12 when only the new message is sent. In azure-cosmos-agent-memory 0.3.0b2, a turn write gets a new random id (MemoryRecord.id defaults to uuid4, and there is no content check for turns), and each call counts toward the auto-trigger for fact extraction. I did not run it against a real Cosmos account, so I don't know how much of this the toolkit's later fact reconciliation hides.

    In case it helps with questions 1 and 2: in a local test I keyed each document on the scope fields + role + content (no whitespace stripping) and checked for existing keys before embedding. On main 13956ad, the same 15-message chat then stored 32 documents instead of 242 (including the new-thread question and answer), embedded 32 texts instead of 242, and the new thread answered "We deploy to westus3, and Mira approves rollbacks." The same text in a different session is still stored separately. It does not handle concurrent runs (it checks, then writes), and I did not test existing random-key data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

memoryUsage: [Issues, PRs], Target: memory related featurespythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions