Skip to content

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

Open
panchaldhruv27223 wants to merge 3 commits intoopenai:mainfrom
panchaldhruv27223:fix-context-personalization
Open

Fix session memory injection logic and prevent duplicate memory entries#2387
panchaldhruv27223 wants to merge 3 commits intoopenai:mainfrom
panchaldhruv27223:fix-context-personalization

Conversation

@panchaldhruv27223
Copy link

@panchaldhruv27223 panchaldhruv27223 commented Jan 17, 2026

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.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa4943e5d9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@panchaldhruv27223
Copy link
Author

@codex

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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.

1 participant