Skip to content

Conversation

@ruskaruma
Copy link
Contributor

Summary

Implements OpenAIResponsesCompactionSession to add seamless support for the newly added responses.compact API, matching the TypeScript SDK implementation (openai-agents-js#760).

Core Implementation

  • OpenAIResponsesCompactionSession: Session decorator that automatically compacts conversation history when it grows too large
  • CompactionItem: New item type to handle encrypted compaction content
  • OpenAIResponsesCompactionAwareSession: Protocol extension for compaction-aware sessions
  • is_openai_responses_compaction_aware_session(): Type guard for runtime checks

Key Features

  • Configurable should_trigger_compaction hook for custom compaction logic
  • Default threshold of 10 candidate items (excludes user messages and existing compaction items)
  • Model validation to ensure only OpenAI Responses API–compatible models are used
  • Automatic runner integration, compaction runs after each turn if threshold is met
  • Chat completions guardrail to prevent unsupported usage

Usage Example

from agents import Agent, MemorySession, OpenAIResponsesCompactionSession, Runner

session = OpenAIResponsesCompactionSession(
    session_id="my-session",
    underlying_session=MemorySession(),
)

result = await Runner.run(agent, "Hello!", session=session)

Test plan

  • Added 17 new tests in tests/memory/test_openai_responses_compaction_session.py

Issue number

Closes #2206

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

@ruskaruma ruskaruma force-pushed the feature/compaction-support branch from c6d62d4 to cb496bf Compare December 23, 2025 11:16
@ruskaruma
Copy link
Contributor Author

Greetings @seratch and @ihower, this PR implements the core responses.compact support, aligned with the TypeScript SDK implementation (#760).

Following the TS SDK approach, a few related enhancements could either be handled in follow-up PRs or incorporated here, depending on your preference:

  • Usage tracking integration (similar to #785)
  • Documentation updates (similar to #784)

I’d appreciate your feedback on whether this aligns with the intended design for the issue, and I’m happy to adjust the implementation as needed.

@seratch seratch added this to the 0.7.x milestone Dec 23, 2025
@seratch
Copy link
Member

seratch commented Dec 23, 2025

Thanks for sending this. This looks like a good port of the TS implementation I did. I will review the details early next year.

@seratch seratch changed the title Add responses.compact: auto-compact long conversations (#2206) feat: #2206 Add responses.compact: auto-compact long conversations Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add responses.compact-wired session

2 participants