Skip to content

feature/session memory #1040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 59 commits into
base: main
Choose a base branch
from
Draft

feature/session memory #1040

wants to merge 59 commits into from

Conversation

rm-openai
Copy link
Collaborator

  • Add session memory functionality to the Agents SDK
  • Enhance session memory validation in Runner class
  • Add custom session memory implementation examples to README
  • Implement session memory instance reuse in Runner class
  • Refactor session memory usage in README and examples
  • Add session memory documentation and examples
  • Add memory documentation and update references
  • revert changes to Agent. memory is a concern of the runner
  • Enhance SQLiteSessionMemory to support customizable table names for sessions and messages
  • Add pop_message functionality to SQLiteSessionMemory
  • Refactor SQLiteSessionMemory methods to support asynchronous execution
  • Add validation for session_id when memory is disabled in Runner class
  • Refactor session memory handling in documentation and examples
  • Refactor database initialization in SQLiteSessionMemory
  • Remove redundant database initialization method in SQLiteSessionMemory
  • Enhance SQLiteSessionMemory for thread safety and connection management
  • Initialize database schema for file databases in SQLiteSessionMemory
  • Refactor Runner class to improve error handling and input preparation
  • Refactor test_session_memory.py to simplify imports
  • Refactor session memory implementation to simplify usage and improve clarity
  • Update session memory documentation and refactor references
  • Update documentation to reflect renaming of Session Memory to Sessions
  • Update documentation and references for Sessions
  • Enhance session message retrieval functionality
  • Update AGENTS.md to include note on executing CLI commands in virtual environment
  • Update README to reflect changes from "Memory options" to "Session options" and clarify session implementation details. Adjusted section headers and descriptions for consistency with recent documentation updates.
  • Add .claude/settings.local.json to .gitignore
  • Refactor message retrieval parameter from 'amount' to 'count'
  • Remove original non-parametrized tests from test_session.py for session memory functionality. This cleanup improves code maintainability and focuses on the current testing approach.
  • Implement validation to prevent simultaneous session and list input in Runner class
  • Refactor session management by introducing an abstract base class
  • Refactor message retrieval parameter from 'count' to 'limit'
  • Update get_messages method to include optional limit parameter
  • Update test_mcp_tracing.py from upstream/main
  • Refactor session methods to use 'items' instead of 'messages'
  • Minimized diff with upstream/main branch in run.py
  • Refactor AgentRunner methods for consistency and readability
  • Refactor RunOptions and AgentRunner methods for clarity and consistency
  • Prevent hanging session test and refactor error handling and input preparation in AgentRunner
  • Remove unnecessary comment regarding parameter handling in AgentRunner to improve code clarity.
  • Enhance RunOptions and AgentRunner to support additional parameters
  • Remove unnecessary asterisk from method signatures in AgentRunner for cleaner code.
  • Refactor error handling and input preparation in AgentRunner to improve clarity and maintainability
  • Refactor SQLiteSession class for improved readability and maintainability
  • Remove .claude/settings.local.json from .gitignore to clean up ignored files.
  • move current_span variable declaration back to where it was
  • Refactor SQLiteSession.pop_item() to use atomic DELETE with RETURNING
  • fix lint
  • fix typechecks

## Summary
- Introduced `SessionMemory` and `SQLiteSessionMemory` classes for automatic conversation history management.
- Updated `Agent` class to support session memory configuration.
- Enhanced `Runner` class to handle input preparation and result saving with session memory.
- Added example demonstrating session memory usage.
- Implemented tests for session memory functionality.

## Testing
- `make format`
- `make lint`
- `make mypy`
- `make tests`
- Added a check to raise a ValueError if `session_id` is not provided when session memory is enabled.
- Updated the `SessionMemory` class to use a Protocol instead of an abstract base class, simplifying the implementation.
- Modified tests to ensure an exception is raised when attempting to run with memory enabled but no session_id is provided.
- Introduced a section on creating custom memory implementations following the `SessionMemory` protocol.
- Added code examples demonstrating how to implement and use a custom memory class.
- Highlighted the requirement for `session_id` when session memory is enabled, with examples illustrating correct usage.
- Updated the Runner class to ensure that when memory=True, a single instance of SQLiteSessionMemory is created and reused across runs.
- Added a test to verify that the same memory instance is returned for multiple calls when memory is enabled.
- Ensured the agent stores the memory instance for consistency.
- Updated README and example scripts to utilize `SQLiteSessionMemory` explicitly instead of using a boolean flag for memory.
- Modified `RunConfig` to accept a memory instance directly, enhancing clarity and flexibility in session management.
- Adjusted tests to reflect the new memory handling approach, ensuring consistent behavior across different configurations.
- Updated `mkdocs.yml` to include `session_memory.md` in the documentation.
- Enhanced `index.md` to highlight the new **Session Memory** feature for automatic conversation history management.
- Modified `running_agents.md` to include details about the `memory` and `session_id` parameters in `RunConfig`.
- Added comprehensive documentation for session memory functionality in the new `session_memory.md` file, including usage examples and best practices.
- Included `memory.md` in the documentation by updating `mkdocs.yml`.
- Corrected links in `session_memory.md` to point to the appropriate memory classes.
- Created a new `memory.md` file detailing the `SessionMemory` and `SQLiteSessionMemory` classes.
…essions and messages

- Updated the constructor to accept `sessions_table` and `messages_table` parameters, allowing users to specify custom table names.
- Modified SQL queries to utilize the provided table names, ensuring flexibility in database schema.
- Adjusted index creation and deletion queries to reflect the new table name parameters.
- Implemented the `pop_message` method to remove and return the most recent message from a session.
- Updated the `SessionMemory` protocol to include the new method signature.
- Enhanced documentation in `session_memory.md` with examples demonstrating the usage of `pop_message`.
- Added tests to verify the functionality of `pop_message`, including edge cases for empty sessions and multiple sessions.
- Converted synchronous database operations in `get_messages`, `add_messages`, `pop_message`, and `clear_session` methods to asynchronous using `asyncio.to_thread`.
- Improved performance and responsiveness of the session memory handling by allowing non-blocking database interactions.
- Implemented a check in the Runner class to raise a ValueError if a session_id is provided without enabling memory in the RunConfig.
- Updated tests to verify that the appropriate exception is raised when session_id is used without memory.
- Updated README, session_memory.md, and example scripts to remove the use of RunConfig for session memory configuration, directly passing memory and session_id parameters to the Runner.run method.
- Enhanced clarity in documentation regarding the requirement of session_id when memory is enabled.
- Adjusted tests to reflect the new approach, ensuring consistent behavior across different configurations.
- Introduced a new method `_init_db_for_connection` to handle database schema initialization for a specific connection.
- Updated the `_init_db` method to call the new method, improving clarity and separation of concerns.
- Added a comment to indicate the initialization of the database schema for the connection.
- Deleted the `_init_db` method as database schema initialization is now handled in `_init_db_for_connection`.
- This change simplifies the class structure and improves clarity in the database connection management.
- Introduced a shared connection for in-memory databases to avoid thread isolation, improving concurrency.
- Implemented a locking mechanism for database operations to ensure thread safety, regardless of the database type.
- Updated the `_get_connection`, `_add_messages_sync`, `_pop_message_sync`, and `_clear_session_sync` methods to utilize the new locking and connection management logic.
- Added logic to initialize the database schema for file databases during connection setup.
- Ensured that the schema is only initialized once, improving efficiency and clarity in connection management.
- Enhanced the error handling mechanism in the Runner class to ensure that exceptions during setup result in a completion sentinel being placed in the event queue.
- Streamlined the input preparation process by consolidating the logic for handling session memory and updating the streamed result.
- Improved clarity and maintainability of the code by restructuring the try-except blocks and ensuring proper resource management for spans and traces.
- Removed unused imports and streamlined the import statements for clarity and maintainability.
- This change enhances the readability of the test file by focusing on the necessary components.
…clarity

- Replaced `SQLiteSessionMemory` with `SQLiteSession` in the codebase, streamlining session management.
- Updated documentation and examples to reflect the new session handling approach, removing the need for `session_id` when using sessions.
- Enhanced the `Session` protocol to better define session behavior and improve consistency across implementations.
- Adjusted tests to ensure compatibility with the new session structure, maintaining functionality across various scenarios.
- Renamed `session_memory.md` to `session.md` for clarity and consistency.
- Updated links in `running_agents.md` to reflect the new documentation filename.
- Added comprehensive documentation for session memory functionality, including usage examples and API reference.
- Removed references to `SessionMemory` and `SQLiteSessionMemory` from the codebase to streamline session management.
- Changed all references from "Session Memory" to "Sessions" in README, documentation, and example files for consistency.
- Updated descriptions to clarify the functionality of Sessions in managing conversation history across agent runs.
- Renamed instances of "session.md" to "sessions.md" in mkdocs.yml and running_agents.md for consistency.
- Added new sessions.md file detailing the functionality and usage of session memory in the Agents SDK, including examples and API reference.
- Updated the `get_messages` method in the `Session` and `SQLiteSession` classes to accept an optional `amount` parameter, allowing retrieval of the latest N messages or all messages if not specified.
- Added a demonstration in `session_example.py` to showcase the new functionality for fetching the latest messages.
- Implemented tests in `test_session.py` to verify the behavior of the `get_messages` method with various amounts, ensuring correct message retrieval.
…tions" and clarify session implementation details. Adjusted section headers and descriptions for consistency with recent documentation updates.
- Updated the `get_messages` method in the `Session` and `SQLiteSession` classes to use 'count' instead of 'amount' for clarity.
- Adjusted the corresponding example in `session_example.py` to reflect this change.
- Modified tests in `test_session.py` to ensure they reference the new 'count' parameter.
…on memory functionality. This cleanup improves code maintainability and focuses on the current testing approach.
…n Runner class

- Added a check in the Runner class to raise a UserError when both a session and a list input are provided, clarifying the expected input format.
- Introduced a new test in test_session.py to ensure that this validation works correctly across different runner methods.
knowsuchagency and others added 28 commits May 30, 2025 22:51
Resolved conflicts in src/agents/run.py:
- Kept both UserError and RunErrorDetails imports
- Preserved session functionality (_prepare_input_with_session and _save_result_to_session)
- Integrated upstream's RunErrorDetails exception handling
- Maintained max turns check logic while incorporating upstream changes
- Updated the `get_messages` method in the `Session`, `SQLiteSession`, and related files to use 'limit' instead of 'count' for consistency and clarity.
- Adjusted the example in `session_example.py` to reflect this change.
- Modified tests in `test_session.py` to ensure they reference the new 'limit' parameter.
- Modified the `get_messages` method in both `README.md` and `docs/sessions.md` to accept an optional `limit` parameter, enhancing flexibility in retrieving conversation history.
Resolve conflicts with upstream changes to _get_all_tools method which now
accepts a context_wrapper parameter. This change enables dynamic tool
enabling/disabling based on context.

Updated all calls to _get_all_tools to pass the context_wrapper parameter.
Add noqa comment to suppress line-too-long linting error
Resolved merge conflict in mkdocs.yml by including both memory.md and repl.md references
### Changes
- Renamed methods in `MyCustomSession`, `SQLiteSession`, and related classes from `get_messages`, `add_messages`, `pop_message` to `get_items`, `add_items`, `pop_item`.
- Updated documentation and examples to reflect the new method names and terminology.
- Adjusted tests to ensure they validate the new method names and functionality.

This change enhances clarity by standardizing the terminology used for session items.
### Changes
- Simplified method calls in `AgentRunner` by using class methods directly.
- Consolidated input preparation and event queue updates for clarity.
- Improved formatting and reduced line breaks for better readability.

This refactor enhances the maintainability of the code by standardizing method usage and improving overall structure.
…stency

### Changes
- Removed the `session` field from `RunOptions` and its related comments to simplify the interface.
- Improved method calls by using class methods directly for better maintainability.

This refactor streamlines the code structure and improves the overall clarity of the `run.py` file.
…eparation in `AgentRunner`

### Changes
- Consolidated error handling to ensure `streamed_result.is_complete` is set even if an exception occurs.
- Improved the structure of input preparation and event queue updates for better readability and maintainability.

This refactor enhances the robustness of the `AgentRunner` class by ensuring proper completion signaling and clearer code flow.
### Changes
- Added `session` field to `RunOptions` for session management.
- Refactored `AgentRunner` methods to accept parameters via `kwargs`, improving flexibility and maintainability.

This update streamlines parameter handling and enhances the functionality of the `AgentRunner` class.
…rove clarity and maintainability

### Changes
- Moved the initialization of variables closer to their usage for better readability.
- Removed redundant exception handling to streamline the flow of the method.
- Ensured that the completion signaling for `streamed_result` is handled more clearly.

This update enhances the robustness and clarity of the `AgentRunner` class.
…bility

### Changes
- Adjusted formatting of docstrings and SQL queries for better clarity.
- Simplified SQL query structure by removing unnecessary line breaks.
- Enhanced the logic for fetching messages to ensure chronological order when using descending order.

This update improves the overall structure and readability of the `SQLiteSession` class.
Replace separate SELECT and DELETE queries with a single atomic DELETE...RETURNING
operation as suggested by slyapustin. This improves performance and eliminates
potential race conditions while simplifying the error handling logic.
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