Table of Contents

Class ThreadExtensions

Namespace
OpenAI.Threads
Assembly
OpenAI-DotNet.dll
public static class ThreadExtensions
Inheritance
ThreadExtensions
Inherited Members

Methods

CancelAsync(RunResponse, CancellationToken)

Cancels a run that is InProgress.

public static Task<bool> CancelAsync(this RunResponse run, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to cancel.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<bool>

RunResponse.

CreateMessageAsync(ThreadResponse, Message, CancellationToken)

Create a new message for this thread.

public static Task<MessageResponse> CreateMessageAsync(this ThreadResponse thread, Message request, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

request Message

Message.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<MessageResponse>

MessageResponse.

CreateRunAsync(ThreadResponse, AssistantResponse, Action<IServerSentEvent>, CancellationToken)

[Obsolete("use new overload with Func<IServerSentEvent, Task> instead.")]
public static Task<RunResponse> CreateRunAsync(this ThreadResponse thread, AssistantResponse assistant, Action<IServerSentEvent> streamEventHandler, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse
assistant AssistantResponse
streamEventHandler Action<IServerSentEvent>
cancellationToken CancellationToken

Returns

Task<RunResponse>

CreateRunAsync(ThreadResponse, AssistantResponse, Func<IServerSentEvent, Task>, CancellationToken)

Create a run.

public static Task<RunResponse> CreateRunAsync(this ThreadResponse thread, AssistantResponse assistant, Func<IServerSentEvent, Task> streamEventHandler = null, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

assistant AssistantResponse

The AssistantResponse to use for the run.

streamEventHandler Func<IServerSentEvent, Task>

Optional, Func<T, TResult> stream callback handler.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

CreateRunAsync(ThreadResponse, CreateRunRequest, Action<IServerSentEvent>, CancellationToken)

[Obsolete("use new overload with Func<IServerSentEvent, Task> instead.")]
public static Task<RunResponse> CreateRunAsync(this ThreadResponse thread, CreateRunRequest request, Action<IServerSentEvent> streamEventHandler, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse
request CreateRunRequest
streamEventHandler Action<IServerSentEvent>
cancellationToken CancellationToken

Returns

Task<RunResponse>

CreateRunAsync(ThreadResponse, CreateRunRequest, Func<IServerSentEvent, Task>, CancellationToken)

Create a run.

public static Task<RunResponse> CreateRunAsync(this ThreadResponse thread, CreateRunRequest request = null, Func<IServerSentEvent, Task> streamEventHandler = null, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

request CreateRunRequest

CreateRunRequest.

streamEventHandler Func<IServerSentEvent, Task>

Optional, Func<T, TResult> stream callback handler.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

DeleteAsync(ThreadResponse, bool, CancellationToken)

Deletes the thread.

public static Task<bool> DeleteAsync(this ThreadResponse thread, bool deleteToolResources = false, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

deleteToolResources bool

Optional, should tool resources, such as vector stores be deleted when this thread is deleted?

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<bool>

True, if the thread was successfully deleted.

GetThreadAsync(RunResponse, CancellationToken)

Gets the thread associated to the RunResponse.

public static Task<ThreadResponse> GetThreadAsync(this RunResponse run, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ThreadResponse>

ThreadResponse.

ListMessagesAsync(RunResponse, ListQuery, CancellationToken)

Returns a list of messages for a given thread that the run belongs to.

public static Task<ListResponse<MessageResponse>> ListMessagesAsync(this RunResponse run, ListQuery query = null, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse.

query ListQuery

ListQuery.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ListResponse<MessageResponse>>

ListResponse<TObject>.

ListMessagesAsync(ThreadResponse, ListQuery, CancellationToken)

List the messages associated to this thread.

public static Task<ListResponse<MessageResponse>> ListMessagesAsync(this ThreadResponse thread, ListQuery query = null, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

query ListQuery

Optional, ListQuery.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ListResponse<MessageResponse>>

ListResponse<TObject>.

ListRunStepsAsync(RunResponse, ListQuery, CancellationToken)

Returns a list of run steps belonging to a run.

public static Task<ListResponse<RunStepResponse>> ListRunStepsAsync(this RunResponse run, ListQuery query = null, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to list run steps for.

query ListQuery

Optional, ListQuery.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ListResponse<RunStepResponse>>

ListResponse<TObject>.

ListRunsAsync(ThreadResponse, ListQuery, CancellationToken)

Lists all the runs associated to a thread.

public static Task<ListResponse<RunResponse>> ListRunsAsync(this ThreadResponse thread, ListQuery query = null, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

query ListQuery

ListQuery.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ListResponse<RunResponse>>

ListResponse<TObject>.

ModifyAsync(MessageResponse, IReadOnlyDictionary<string, string>, CancellationToken)

Modify a message.

public static Task<MessageResponse> ModifyAsync(this MessageResponse message, IReadOnlyDictionary<string, string> metadata, CancellationToken cancellationToken = default)

Parameters

message MessageResponse

MessageResponse.

metadata IReadOnlyDictionary<string, string>

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<MessageResponse>

MessageResponse.

Remarks

Only the Metadata can be modified.

ModifyAsync(RunResponse, IReadOnlyDictionary<string, string>, CancellationToken)

Modifies a run.

public static Task<RunResponse> ModifyAsync(this RunResponse run, IReadOnlyDictionary<string, string> metadata, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to modify.

metadata IReadOnlyDictionary<string, string>

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

Remarks

Only the Metadata can be modified.

ModifyAsync(ThreadResponse, IReadOnlyDictionary<string, string>, CancellationToken)

Modify the thread.

public static Task<ThreadResponse> ModifyAsync(this ThreadResponse thread, IReadOnlyDictionary<string, string> metadata, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

metadata IReadOnlyDictionary<string, string>

The metadata to set on the thread.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ThreadResponse>

ThreadResponse.

Remarks

Only the Metadata can be modified.

ModifyMessageAsync(ThreadResponse, string, IReadOnlyDictionary<string, string>, CancellationToken)

Modifies a message.

public static Task<MessageResponse> ModifyMessageAsync(this ThreadResponse thread, string messageId, IReadOnlyDictionary<string, string> metadata, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

messageId string

The id of the message to modify.

metadata IReadOnlyDictionary<string, string>

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<MessageResponse>

MessageResponse.

Remarks

Only the Metadata can be modified.

RetrieveMessageAsync(ThreadResponse, string, CancellationToken)

Retrieve a message.

public static Task<MessageResponse> RetrieveMessageAsync(this ThreadResponse thread, string messageId, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

messageId string

The id of the message to get.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<MessageResponse>

MessageResponse.

RetrieveRunAsync(ThreadResponse, string, CancellationToken)

Retrieves a run.

public static Task<RunResponse> RetrieveRunAsync(this ThreadResponse thread, string runId, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

The thread that was run.

runId string

The id of the run to retrieve.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

RetrieveRunStepAsync(RunResponse, string, CancellationToken)

Retrieves a run step.

public static Task<RunStepResponse> RetrieveRunStepAsync(this RunResponse run, string runStepId, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to retrieve step for.

runStepId string

Id of the run step.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunStepResponse>

RunStepResponse.

SubmitToolOutputsAsync(RunResponse, SubmitToolOutputsRequest, Action<IServerSentEvent>, CancellationToken)

[Obsolete("use new overload with Func<IServerSentEvent, Task> instead.")]
public static Task<RunResponse> SubmitToolOutputsAsync(this RunResponse run, SubmitToolOutputsRequest request, Action<IServerSentEvent> streamEventHandler, CancellationToken cancellationToken = default)

Parameters

run RunResponse
request SubmitToolOutputsRequest
streamEventHandler Action<IServerSentEvent>
cancellationToken CancellationToken

Returns

Task<RunResponse>

SubmitToolOutputsAsync(RunResponse, SubmitToolOutputsRequest, Func<IServerSentEvent, Task>, CancellationToken)

When a run has the status: "requires_action" and required_action.type is submit_tool_outputs, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.

public static Task<RunResponse> SubmitToolOutputsAsync(this RunResponse run, SubmitToolOutputsRequest request, Func<IServerSentEvent, Task> streamEventHandler = null, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to submit outputs for.

request SubmitToolOutputsRequest

SubmitToolOutputsRequest.

streamEventHandler Func<IServerSentEvent, Task>

Optional, Func<T, TResult> stream callback handler.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

SubmitToolOutputsAsync(RunResponse, IEnumerable<ToolOutput>, Action<IServerSentEvent>, CancellationToken)

[Obsolete("use new overload with Func<IServerSentEvent, Task> instead.")]
public static Task<RunResponse> SubmitToolOutputsAsync(this RunResponse run, IEnumerable<ToolOutput> outputs, Action<IServerSentEvent> streamEventHandler, CancellationToken cancellationToken = default)

Parameters

run RunResponse
outputs IEnumerable<ToolOutput>
streamEventHandler Action<IServerSentEvent>
cancellationToken CancellationToken

Returns

Task<RunResponse>

SubmitToolOutputsAsync(RunResponse, IEnumerable<ToolOutput>, Func<IServerSentEvent, Task>, CancellationToken)

When a run has the status: "requires_action" and required_action.type is submit_tool_outputs, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.

public static Task<RunResponse> SubmitToolOutputsAsync(this RunResponse run, IEnumerable<ToolOutput> outputs, Func<IServerSentEvent, Task> streamEventHandler = null, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse to submit outputs for.

outputs IEnumerable<ToolOutput>

ToolOutputs

streamEventHandler Func<IServerSentEvent, Task>

Optional, Func<T, TResult> stream callback handler.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

UpdateAsync(MessageResponse, CancellationToken)

Retrieve a message.

public static Task<MessageResponse> UpdateAsync(this MessageResponse message, CancellationToken cancellationToken = default)

Parameters

message MessageResponse

MessageResponse.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<MessageResponse>

MessageResponse.

UpdateAsync(RunResponse, CancellationToken)

Get the latest status of the RunResponse.

public static Task<RunResponse> UpdateAsync(this RunResponse run, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.

UpdateAsync(RunStepResponse, CancellationToken)

Retrieves a run step.

public static Task<RunStepResponse> UpdateAsync(this RunStepResponse runStep, CancellationToken cancellationToken = default)

Parameters

runStep RunStepResponse

RunStepResponse to retrieve.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunStepResponse>

RunStepResponse.

UpdateAsync(ThreadResponse, CancellationToken)

Updates this thread with the latest snapshot from OpenAI.

public static Task<ThreadResponse> UpdateAsync(this ThreadResponse thread, CancellationToken cancellationToken = default)

Parameters

thread ThreadResponse

ThreadResponse.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<ThreadResponse>

ThreadResponse.

WaitForStatusChangeAsync(RunResponse, int?, int?, CancellationToken)

Waits for Status to change.

public static Task<RunResponse> WaitForStatusChangeAsync(this RunResponse run, int? pollingInterval = null, int? timeout = null, CancellationToken cancellationToken = default)

Parameters

run RunResponse

RunResponse.

pollingInterval int?

Optional, time in milliseconds to wait before polling status.

timeout int?

Optional, timeout in seconds to cancel polling.
Defaults to 30 seconds.
Set to -1 for indefinite.

cancellationToken CancellationToken

Optional, CancellationToken.

Returns

Task<RunResponse>

RunResponse.