Gemini.Types.Operation (GeminiEx v0.8.2)

View Source

Type definitions for long-running operations.

Long-running operations are used for tasks that may take significant time to complete, such as video generation, file imports, model tuning, and batch processing.

Operation Lifecycle

  1. Initiated - Operation is created, done: false
  2. Running - Operation is processing, done: false
  3. Completed - Operation finished successfully, done: true, response populated
  4. Failed - Operation failed, done: true, error populated

Polling Pattern

{:ok, operation} = some_long_running_call()

# Poll until complete
{:ok, completed} = Gemini.APIs.Operations.wait(operation.name,
  poll_interval: 5000,
  timeout: 300_000
)

case completed do
  %{done: true, error: nil, response: response} ->
    IO.puts("Success: #{inspect(response)}")
  %{done: true, error: error} ->
    IO.puts("Failed: #{error.message}")
end

Example

# Video generation returns an operation
{:ok, op} = Gemini.generate_video("A cat playing piano")

# Wait for completion
{:ok, completed} = Gemini.APIs.Operations.wait(op.name)

# Get the result
video_uri = completed.response["generatedVideos"]

Summary

Types

Operation error details.

t()

Represents a long-running operation.

Functions

Checks if the operation is complete (successfully or failed).

Checks if the operation failed.

Creates an Operation from API response.

Extracts the operation ID from the full name.

Gets the progress percentage from metadata, if available.

Checks if the operation is still running.

Checks if the operation completed successfully.

Types

operation_error()

@type operation_error() :: %{
  optional(:code) => integer(),
  optional(:message) => String.t(),
  optional(:details) => [map()]
}

Operation error details.

t()

@type t() :: %Gemini.Types.Operation{
  done: boolean(),
  error: operation_error() | nil,
  metadata: map() | nil,
  name: String.t() | nil,
  response: map() | nil
}

Represents a long-running operation.

Fields

  • name - Server-assigned unique identifier (e.g., "operations/abc123")
  • metadata - Service-specific metadata about the operation progress
  • done - Whether the operation is complete (true = finished, false = in progress)
  • error - Error result if the operation failed (mutually exclusive with response)
  • response - Success result if the operation completed (mutually exclusive with error)

Functions

complete?(arg1)

@spec complete?(t()) :: boolean()

Checks if the operation is complete (successfully or failed).

failed?(arg1)

@spec failed?(t()) :: boolean()

Checks if the operation failed.

from_api_response(response)

@spec from_api_response(map()) :: t()

Creates an Operation from API response.

Parameters

  • response - Map from API response with string keys

Examples

response = %{
  "name" => "operations/abc123",
  "done" => false,
  "metadata" => %{"@type" => "...", "progress" => 50}
}
op = Gemini.Types.Operation.from_api_response(response)

get_id(operation)

@spec get_id(t()) :: String.t() | nil

Extracts the operation ID from the full name.

Examples

op = %Operation{name: "operations/abc123"}
Operation.get_id(op)
# => "abc123"

get_progress(operation)

@spec get_progress(t()) :: float() | nil

Gets the progress percentage from metadata, if available.

Returns nil if progress information is not available in metadata.

running?(arg1)

@spec running?(t()) :: boolean()

Checks if the operation is still running.

succeeded?(arg1)

@spec succeeded?(t()) :: boolean()

Checks if the operation completed successfully.