Skip to content

marvin.beta.assistants.threads

Tip

All async methods that have an _async suffix have sync equivalents that can be called with out the suffix e.g. run() and await run_async().

Thread

The Thread class represents a conversation thread with an assistant.

Attributes:

Name Type Description
id Optional[str]

The unique identifier of the thread. None if the thread hasn't been created yet.

metadata dict

Additional data about the thread.

add_async async

Add a user message to the thread.

chat

Starts an interactive chat session with the provided assistant.

create_async async

Creates a thread.

get_messages_async async

Asynchronously retrieves messages from the thread.

Parameters:

Name Type Description Default
limit int

The maximum number of messages to return.

None
before_message str

The ID of the message to start the list from, retrieving messages sent before this one.

None
after_message str

The ID of the message to start the list from, retrieving messages sent after this one.

None
json_compatible bool

If True, returns messages as dictionaries. If False, returns messages as ThreadMessage objects. Default is False.

False

Returns:

Type Description
list[Union[ThreadMessage, dict]]

list[Union[ThreadMessage, dict]]: A list of messages from the thread, either as dictionaries or ThreadMessage objects, depending on the value of json_compatible.

run_async async

Creates and returns a Run of this thread with the provided assistant.

Parameters:

Name Type Description Default
assistant Assistant

The assistant to run the thread with.

required
run_kwargs

Additional keyword arguments to pass to the Run constructor.

{}

ThreadMonitor

The ThreadMonitor class represents a monitor for a specific thread.

Attributes:

Name Type Description
thread_id str

The unique identifier of the thread being monitored.

last_message_id Optional[str]

The ID of the last message received in the thread.

on_new_message Callable

A callback function that is called when a new message is received in the thread.

run_async async

Run the thread monitor in a loop, checking for new messages every interval_seconds.

Parameters:

Name Type Description Default
interval_seconds int

The number of seconds to wait between checking for new messages. Default is 1.

None