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.
  • Sanitize model responses by ensuring all function calls have stable IDs.

  • 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
    sanitize(com.google.genai.types.Content originalContent, AtomicInteger idCounter)
    Inspects a Content object from the model and ensures every FunctionCall part has a stable ID.

    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.
    • sanitize

      public static com.google.genai.types.Content sanitize(com.google.genai.types.Content originalContent, AtomicInteger idCounter)
      Inspects a Content object from the model and ensures every FunctionCall part has a stable ID.

      If a FunctionCall is missing an ID, this method generates one and reconstructs the entire Content object to include it, preserving all other metadata.

      Parameters:
      originalContent - The raw content received from the model.
      idCounter - The atomic counter to use for generating new IDs.
      Returns:
      The original content if no changes were needed, or a new, patched Content object.