Skip to content

Fix session memory injection logic and prevent duplicate memory entries#34

Open
yishangupenn wants to merge 3 commits into
mainfrom
upstream-pr-2387
Open

Fix session memory injection logic and prevent duplicate memory entries#34
yishangupenn wants to merge 3 commits into
mainfrom
upstream-pr-2387

Conversation

@yishangupenn

@yishangupenn yishangupenn commented Mar 10, 2026

Copy link
Copy Markdown

Copied from upstream: openai/openai-cookbook#2387
Original author: @panchaldhruv27223
Originally opened: 2026-01-17


Summary

This PR fixes and simplifies session memory handling in the 'examples/agents_sdk/context_personalization.ipynb' notebook.

It addresses two related issues:

  1. Context Loss: Session memory was only injected in a trim-gated, one-shot manner, which could cause recent conversational context to be unavailable in subsequent turns.
  2. Redundancy: Repeated calls to save session memory could create duplicate entries, leading to unnecessary memory growth and noise.

The changes ensure that session memory is injected consistently when available, and that identical session notes are updated instead of duplicated.

Motivation

In tool-heavy and iterative conversations, recent session context is often required across multiple turns, not only after a trim event. The previous trim-gated injection could lead to loss of short-term context in these cases.

Additionally, session memory notes were previously append-only, allowing identical notes to accumulate over time. This PR introduces a simple deduplication mechanism to keep session memory concise and up to date.

These changes improve:

  • Conversational continuity
  • Memory signal quality
  • Deterministic agent behavior
  • Long-session stability

Changes Included

  • Inject session memory eagerly when notes are present, instead of relying solely on trim-triggered injection.
  • Reset deferred injection flags to avoid duplicate or delayed memory usage.
  • Update existing session memory notes when the same text is saved again, rather than appending duplicates.
  • Refresh timestamps and keywords for updated session memory entries.

No changes were made to global or long-term memory behavior.

Notes

  • This PR intentionally changes session memory from trim-gated to data-driven injection.
  • Token usage may increase slightly due to more consistent memory inclusion.
  • Behavior is otherwise backward compatible outside of session memory injection timing.
  • This behavior can be gated behind a configuration flag if a trim-gated default is preferred.

For new content

When contributing new content, read through our contribution guidelines, and mark the following action items as completed:

  • I have added a new entry in [registry.yaml](not applicable - no new content added)
    • I have conducted a self-review of my content based on the contribution guidelines:
    • Relevance: This content is related to building with OpenAI technologies and is useful to others.
    • Uniqueness: I have searched for related examples in the OpenAI Cookbook, and verified that my content offers new insights or unique information compared to existing documentation.
    • Spelling and Grammar: I have checked for spelling or grammatical mistakes.
    • Clarity: I have done a final read-through and verified that my submission is well-organized and easy to understand.
    • Correctness: The information I include is correct and all of my code executes successfully.
    • Completeness: I have explained everything fully, including all necessary references and citations.

We will rate each of these areas on a scale from 1 to 4, and will only accept contributions that score 3 or higher on all areas. Refer to our contribution guidelines for more details.

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.

2 participants