Class InteractionsClient
- Namespace
- GenerativeAI.Clients
- Assembly
- GenerativeAI.dll
A client for the Gemini Interactions API (/interactions): stateful model and agent interactions
(e.g. Deep Research, Antigravity) with server-side state, background execution, Server-Sent Event streaming,
stream resumption and continuation of long decodes. Works with the Gemini Developer API and Vertex AI.
public class InteractionsClient : BaseClient
- Inheritance
-
InteractionsClient
- Inherited Members
- Extension Methods
Constructors
InteractionsClient(IPlatformAdapter, HttpClient?, ILogger?)
Initializes a new instance of the InteractionsClient class.
public InteractionsClient(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)
Parameters
platformIPlatformAdapterThe platform adapter (Google AI or Vertex AI).
httpClientHttpClientOptional HTTP client.
loggerILoggerOptional logger.
Properties
EnableAutomaticContinuation
Whether CreateAsync(CreateInteractionRequest, CancellationToken) automatically resumes an interaction whose status is RequiresContinuation (default true, as in python-genai). The returned interaction is the last hop, with the steps of all hops.
public bool EnableAutomaticContinuation { get; set; }
Property Value
MaxContinuations
The maximum number of continuation requests per create call (default 10).
public int MaxContinuations { get; set; }
Property Value
MaxStreamReconnects
How many times StreamAsync(CreateInteractionRequest, CancellationToken) resumes a stream that fails after the interaction was created, using the last received event ID (default 3; 0 disables it).
public int MaxStreamReconnects { get; set; }
Property Value
Methods
CancelAsync(string, CancellationToken)
Cancels a running (background) interaction.
public Task<Interaction> CancelAsync(string id, CancellationToken cancellationToken = default)
Parameters
idstringThe interaction ID.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The interaction after cancellation.
ContinueAsync(Interaction, CreateInteractionRequest?, CancellationToken)
Continues an interaction whose status is RequiresContinuation (see CreateContinuationRequest(Interaction, CreateInteractionRequest?)). Each continuation re-prefills and bills input tokens; background interactions are continued by the server.
public Task<Interaction> ContinueAsync(Interaction interaction, CreateInteractionRequest? originalRequest = null, CancellationToken cancellationToken = default)
Parameters
interactionInteractionThe interaction to continue.
originalRequestCreateInteractionRequestThe request that created the interaction, if available.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The new interaction produced by the continuation hop.
CreateAsync(CreateInteractionRequest, CancellationToken)
Creates an interaction and returns it once the request completes (or, with Background, as soon as it has been queued).
public Task<Interaction> CreateAsync(CreateInteractionRequest request, CancellationToken cancellationToken = default)
Parameters
requestCreateInteractionRequestThe request. Stream is ignored; use StreamAsync(CreateInteractionRequest, CancellationToken).
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The interaction.
- See Also
CreateAsync(string, InteractionInput, CancellationToken)
Creates a model interaction from a model ID and an input.
public Task<Interaction> CreateAsync(string model, InteractionInput input, CancellationToken cancellationToken = default)
Parameters
modelstringThe model ID (see InteractionModels).
inputInteractionInputThe input: a string, content blocks or steps.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The interaction.
CreateContinuationRequest(Interaction, CreateInteractionRequest?)
Builds the request that continues an interaction whose status is
RequiresContinuation, following python-genai: the
ContinuationToken is passed back unchanged; when the original request was stored
(the default) the interaction is referenced by previous_interaction_id and no input is sent,
otherwise (store=false) the original input is sent again.
public static CreateInteractionRequest CreateContinuationRequest(Interaction interaction, CreateInteractionRequest? originalRequest = null)
Parameters
interactionInteractionThe interaction to continue.
originalRequestCreateInteractionRequestThe request that created the interaction. When omitted, the model/agent of
interactionare used and the interaction is assumed to be stored.
Returns
- CreateInteractionRequest
The continuation request.
DeleteAsync(string, CancellationToken)
Deletes an interaction.
public Task DeleteAsync(string id, CancellationToken cancellationToken = default)
Parameters
idstringThe interaction ID.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task
A task that completes when the interaction has been deleted.
GetAsync(string, bool?, CancellationToken)
Retrieves an interaction by ID.
public Task<Interaction> GetAsync(string id, bool? includeInput = null, CancellationToken cancellationToken = default)
Parameters
idstringThe interaction ID (an
interactions/prefix is accepted).includeInputbool?If
true, includes the input in the response.cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The interaction.
GetInteractionsUrl()
Gets the base URL of the interactions collection, e.g.
https://generativelanguage.googleapis.com/v1beta/interactions or
https://{location}-aiplatform.googleapis.com/{version}/projects/{project}/locations/{location}/interactions.
protected string GetInteractionsUrl()
Returns
- string
The collection URL.
ResumeStreamAsync(string, string?, bool?, CancellationToken)
Streams the events of an existing interaction (GET /interactions/{id}?stream=true), e.g. a
background interaction, or resumes a dropped stream after lastEventId.
public IAsyncEnumerable<InteractionSseEvent> ResumeStreamAsync(string id, string? lastEventId = null, bool? includeInput = null, CancellationToken cancellationToken = default)
Parameters
idstringThe interaction ID.
lastEventIdstringIf set, resumes the stream from the event after this event ID.
includeInputbool?If
true, includes the input in the interaction.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<InteractionSseEvent>
The stream of events.
StreamAsync(CreateInteractionRequest, CancellationToken)
Creates an interaction and streams its Server-Sent Events (interaction.created,
step.start, step.delta, step.stop, interaction.status_update,
interaction.completed, error). Keep the last EventId to
resume a dropped stream with ResumeStreamAsync(string, string?, bool?, CancellationToken).
public IAsyncEnumerable<InteractionSseEvent> StreamAsync(CreateInteractionRequest request, CancellationToken cancellationToken = default)
Parameters
requestCreateInteractionRequestThe request; Stream is set to
true.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<InteractionSseEvent>
The stream of events.
WaitForCompletionAsync(string, TimeSpan?, TimeSpan?, CancellationToken)
Polls an interaction (typically one created with Background) until it is no longer InProgress or Queued.
public Task<Interaction> WaitForCompletionAsync(string id, TimeSpan? pollInterval = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
Parameters
idstringThe interaction ID.
pollIntervalTimeSpan?Delay between polls (default 10 seconds).
timeoutTimeSpan?Optional overall timeout; a TimeoutException is thrown when exceeded.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<Interaction>
The interaction in its final (or action/continuation-required) state.