ResponsesAgentThread Class

A specialized thread class for "Responses" style chat. Manages in-memory ChatHistory for partial or ephemeral sessions, or storing them in the service if store_enabled is True.

Constructor

ResponsesAgentThread(client: AsyncOpenAI, chat_history: ChatHistory | None = None, previous_response_id: str | None = None, enable_store: bool | None = True)

Parameters

Name Description
client
Required

The openai.AsyncOpenAI client for making "Responses" requests.

chat_history

Optional ChatHistory to seed the conversation.

previous_response_id

The prior response ID if continuing from a previous run.

enable_store

If true, the thread can store conversation server-side.

Methods

_create

Creates a new thread if needed. For ephemeral usage, the thread ID might remain blank until the first server call.

_delete

Deletes the thread, clearing chat_history in memory and marking it as deleted.

_on_new_message

Called when a new user or system message is appended; adds to in-memory history or defers to remote if storing is enabled.

get_messages

Retrieve messages from memory (if ephemeral) or from the remote store if a response_id is known and store is enabled.

reduce

Attempt to reduce the chat history if it is an instance of ChatHistoryReducer; returns a smaller or truncated conversation.

_create

Creates a new thread if needed. For ephemeral usage, the thread ID might remain blank until the first server call.

async _create() -> str

_delete

Deletes the thread, clearing chat_history in memory and marking it as deleted.

async _delete() -> None

_on_new_message

Called when a new user or system message is appended; adds to in-memory history or defers to remote if storing is enabled.

async _on_new_message(new_message: str | ChatMessageContent) -> None

get_messages

Retrieve messages from memory (if ephemeral) or from the remote store if a response_id is known and store is enabled.

async get_messages(limit: int | None = None, sort_order: Literal["asc","desc"] | None = "desc") -> AsyncIterable[ChatMessageContent]

Parameters

Name Description
limit

Optional max number of messages to retrieve.

Default value: None
sort_order

Sort ascending or descending. Defaults to desc.

Default value: desc

Returns

Type Description

An async stream of ChatMessageContent messages from the conversation.

reduce

Attempt to reduce the chat history if it is an instance of ChatHistoryReducer; returns a smaller or truncated conversation.

async reduce() -> ChatHistory | None

Returns

Type Description

A reduced ChatHistory object, or None if reduction is unavailable.

Attributes

_client

The openai.AsyncOpenAI client interface used for "Responses" operations.

_client: AsyncOpenAI

_chat_history

Local ChatHistory instance, used if store_enabled is false or not yet connected.

_chat_history: ChatHistory

_response_id

Tracks the remote response ID, if any.

_response_id: str | None

_enable_store

If True, writes the conversation to the server. Otherwise ephemeral.

_enable_store: bool