Class GeminiAdapter

java.lang.Object
uno.anahata.ai.gemini.GeminiAdapter

public class GeminiAdapter extends Object
A utility class for adapting Java types and objects to the Gemini API's data structures.

This class provides methods to:

  • Generate Schema objects from Java Types.
  • Extract tool call IDs from Part objects.
  • Prepare content for the API (purifying POJOs).

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.google.genai.types.Schema
    Generates a Gemini Schema for a given Java class.
    static com.google.genai.types.Schema
    Generates a Gemini Schema for a given Java type.
    static com.google.genai.types.Schema
    getGeminiSchema(Type type, boolean includeJsonSchemaId)
    Generates a Gemini Schema for a given Java type, with an option to include a JSON schema ID.
    getToolCallId(com.google.genai.types.Part part)
    Extracts the tool call ID from a Part, checking both FunctionCall and FunctionResponse.
    static com.google.genai.types.Content
    prepareForApi(com.google.genai.types.Content content)
    Prepares a Content object for the Gemini API by ensuring all FunctionCall arguments and FunctionResponse results are converted to pure JSON primitives (Maps, Lists, Primitives).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GeminiAdapter

      public GeminiAdapter()
  • Method Details

    • getGeminiSchema

      public static com.google.genai.types.Schema getGeminiSchema(Type type) throws Exception
      Generates a Gemini Schema for a given Java type.
      Parameters:
      type - The Java type to convert.
      Returns:
      The corresponding Gemini Schema.
      Throws:
      Exception - if schema generation fails.
    • getGeminiSchema

      public static com.google.genai.types.Schema getGeminiSchema(Type type, boolean includeJsonSchemaId) throws Exception
      Generates a Gemini Schema for a given Java type, with an option to include a JSON schema ID.
      Parameters:
      type - The Java type to convert.
      includeJsonSchemaId - Whether to include a unique ID in the schema.
      Returns:
      The corresponding Gemini Schema.
      Throws:
      Exception - if schema generation fails.
    • getGeminiSchema

      public static com.google.genai.types.Schema getGeminiSchema(Class<?> clazz) throws Exception
      Generates a Gemini Schema for a given Java class.
      Parameters:
      clazz - The Java class to convert.
      Returns:
      The corresponding Gemini Schema.
      Throws:
      Exception - if schema generation fails.
    • getToolCallId

      public static Optional<String> getToolCallId(com.google.genai.types.Part part)
      Extracts the tool call ID from a Part, checking both FunctionCall and FunctionResponse.
      Parameters:
      part - The part to inspect.
      Returns:
      An Optional containing the ID if found, otherwise empty.
    • prepareForApi

      public static com.google.genai.types.Content prepareForApi(com.google.genai.types.Content content)
      Prepares a Content object for the Gemini API by ensuring all FunctionCall arguments and FunctionResponse results are converted to pure JSON primitives (Maps, Lists, Primitives). This prevents "greedy" serialization crashes in external mappers.
      Parameters:
      content - The content to prepare.
      Returns:
      A new Content object with purified parts, or the original if no changes were needed.