Session
Session
Bases: Protocol
Protocol for session implementations.
Session stores conversation history for a specific session, allowing agents to maintain context without requiring explicit manual memory management.
ソースコード位置: src/agents/memory/session.py
get_items
async
get_items(
limit: int | None = None,
) -> list[TResponseInputItem]
Retrieve the conversation history for this session.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
limit
|
int | None
|
Maximum number of items to retrieve. If None, retrieves all items. When specified, returns the latest N items in chronological order. |
None
|
戻り値:
| タイプ | デスクリプション |
|---|---|
list[TResponseInputItem]
|
List of input items representing the conversation history |
ソースコード位置: src/agents/memory/session.py
add_items
async
add_items(items: list[TResponseInputItem]) -> None
Add new items to the conversation history.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
items
|
list[TResponseInputItem]
|
List of input items to add to the history |
必須 |
pop_item
async
pop_item() -> TResponseInputItem | None
Remove and return the most recent item from the session.
戻り値:
| タイプ | デスクリプション |
|---|---|
TResponseInputItem | None
|
The most recent item if it exists, None if the session is empty |
SessionABC
Bases: ABC
Abstract base class for session implementations.
Session stores conversation history for a specific session, allowing agents to maintain context without requiring explicit manual memory management.
This ABC is intended for internal use and as a base class for concrete implementations. Third-party libraries should implement the Session protocol instead.
ソースコード位置: src/agents/memory/session.py
get_items
abstractmethod
async
get_items(
limit: int | None = None,
) -> list[TResponseInputItem]
Retrieve the conversation history for this session.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
limit
|
int | None
|
Maximum number of items to retrieve. If None, retrieves all items. When specified, returns the latest N items in chronological order. |
None
|
戻り値:
| タイプ | デスクリプション |
|---|---|
list[TResponseInputItem]
|
List of input items representing the conversation history |
ソースコード位置: src/agents/memory/session.py
add_items
abstractmethod
async
add_items(items: list[TResponseInputItem]) -> None
Add new items to the conversation history.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
items
|
list[TResponseInputItem]
|
List of input items to add to the history |
必須 |
pop_item
abstractmethod
async
pop_item() -> TResponseInputItem | None
Remove and return the most recent item from the session.
戻り値:
| タイプ | デスクリプション |
|---|---|
TResponseInputItem | None
|
The most recent item if it exists, None if the session is empty |
OpenAIResponsesCompactionArgs
Bases: TypedDict
Arguments for the run_compaction method.
ソースコード位置: src/agents/memory/session.py
compaction_mode
instance-attribute
How to provide history for compaction.
- "auto": Use input when the last response was not stored or no response ID is available.
- "previous_response_id": Use server-managed response history.
- "input": Send locally stored session items as input.
store
instance-attribute
Whether the last model response was stored on the server.
When set to False, compaction should avoid "previous_response_id" unless explicitly requested.
OpenAIResponsesCompactionAwareSession
Bases: Session, Protocol
Protocol for session implementations that support responses compaction.
ソースコード位置: src/agents/memory/session.py
run_compaction
async
run_compaction(
args: OpenAIResponsesCompactionArgs | None = None,
) -> None
get_items
async
get_items(
limit: int | None = None,
) -> list[TResponseInputItem]
Retrieve the conversation history for this session.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
limit
|
int | None
|
Maximum number of items to retrieve. If None, retrieves all items. When specified, returns the latest N items in chronological order. |
None
|
戻り値:
| タイプ | デスクリプション |
|---|---|
list[TResponseInputItem]
|
List of input items representing the conversation history |
ソースコード位置: src/agents/memory/session.py
add_items
async
add_items(items: list[TResponseInputItem]) -> None
Add new items to the conversation history.
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
items
|
list[TResponseInputItem]
|
List of input items to add to the history |
必須 |
pop_item
async
pop_item() -> TResponseInputItem | None
Remove and return the most recent item from the session.
戻り値:
| タイプ | デスクリプション |
|---|---|
TResponseInputItem | None
|
The most recent item if it exists, None if the session is empty |
await_session_mutation
async
Wait for a session mutation to settle before propagating caller cancellation.
Once this coroutine starts, cancellation of its caller does not cancel the supplied awaitable. Repeated cancellation requests are deferred until the operation finishes. Without caller cancellation, return the result or raise the operation's exception. With caller cancellation, retrieve the operation's outcome and raise the first caller cancellation instead, even if the operation failed. The caller must not interpret that cancellation as a rolled-back write.
This helper does not provide a timeout, transaction, retry, or mutual exclusion. A stuck operation can delay cancellation indefinitely; configure timeouts on the underlying client. Independently cancelling the supplied operation can still interrupt it. Cancellation before this coroutine starts is not covered.
ソースコード位置: src/agents/memory/session.py
is_openai_responses_compaction_aware_session
is_openai_responses_compaction_aware_session(
session: Session | None,
) -> TypeGuard[OpenAIResponsesCompactionAwareSession]
Check if a session supports responses compaction.