Class GeminiUtil

java.lang.Object
com.google.adk.models.GeminiUtil

public final class GeminiUtil extends Object
Request / Response utilities for Gemini.
  • Field Details

  • Method Details

    • prepareGenenerateContentRequest

      public static LlmRequest prepareGenenerateContentRequest(LlmRequest llmRequest, boolean sanitize)
      Prepares an LlmRequest for the GenerateContent API.

      This method can optionally sanitize the request and ensures that the last content part is from the user to prompt a model response. It also strips out any parts marked as "thoughts".

      Parameters:
      llmRequest - The original LlmRequest.
      sanitize - Whether to sanitize the request to be compatible with the Gemini API backend.
      Returns:
      The prepared LlmRequest.
    • sanitizeRequestForGeminiApi

      public static LlmRequest sanitizeRequestForGeminiApi(LlmRequest llmRequest)
      Sanitizes the request to ensure it is compatible with the Gemini API backend. Required as there are some parameters that if included in the request will raise a runtime error if sent to the wrong backend (e.g. image names only work on Vertex AI).
      Parameters:
      llmRequest - The request to sanitize.
      Returns:
      The sanitized request.
    • getTextFromLlmResponse

      public static String getTextFromLlmResponse(LlmResponse llmResponse)
      Extracts text content from the first part of an LlmResponse, if available.
      Parameters:
      llmResponse - The LlmResponse to extract text from.
      Returns:
      The text content, or an empty string if not found.
    • shouldEmitAccumulatedText

      public static boolean shouldEmitAccumulatedText(LlmResponse currentLlmResponse)
      Determines if accumulated text should be emitted based on the current LlmResponse. We flush if current response is not a text continuation (e.g., no content, no parts, or the first part is not inline_data, meaning it's something else or just empty, thereby warranting a flush of preceding text).
      Parameters:
      currentLlmResponse - The current LlmResponse being processed.
      Returns:
      True if accumulated text should be emitted, false otherwise.
    • stripThoughts

      public static List<com.google.genai.types.Content> stripThoughts(List<com.google.genai.types.Content> originalContents)
      Removes any `Part` that contains only a `thought` from the content list.