CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-google-ai-gemini

LangChain4j integration for Google AI Gemini models providing chat, streaming, embeddings, image generation, and batch processing capabilities

Overview
Eval results
Files

configuration.mddocs/

Configuration

Configuration classes and enums for controlling Google AI Gemini model behavior including function calling, thinking mode, safety settings, media resolution, and response modalities. These configuration options allow fine-grained control over model behavior and outputs.

Capabilities

GeminiFunctionCallingConfig

Configuration for function calling behavior controlling when and which functions the model can invoke.

/**
 * Configuration for function calling behavior.
 * Controls function calling mode and allowed function names.
 */
public class GeminiFunctionCallingConfig {
    /**
     * Creates a function calling config with mode and allowed functions.
     * @param mode Function calling mode
     * @param allowedFunctionNames List of function names that can be called (null for all)
     */
    public GeminiFunctionCallingConfig(GeminiMode mode, List<String> allowedFunctionNames);

    /**
     * Creates a function calling config from builder.
     * @param builder Builder instance
     */
    public GeminiFunctionCallingConfig(GeminiFunctionCallingConfigBuilder builder);

    /**
     * Creates a new builder for function calling config.
     * @return GeminiFunctionCallingConfigBuilder instance
     */
    public static GeminiFunctionCallingConfigBuilder builder();

    /**
     * Gets the function calling mode.
     * @return GeminiMode enum value
     */
    public GeminiMode getMode();

    /**
     * Gets the list of allowed function names.
     * @return List of function names (null if all allowed)
     */
    public List<String> getAllowedFunctionNames();

    /**
     * Sets the function calling mode.
     * @param mode Function calling mode
     */
    public void setMode(GeminiMode mode);

    /**
     * Sets the allowed function names.
     * @param allowedFunctionNames List of function names
     */
    public void setAllowedFunctionNames(List<String> allowedFunctionNames);
}

GeminiFunctionCallingConfigBuilder

Builder for constructing GeminiFunctionCallingConfig instances.

/**
 * Builder for GeminiFunctionCallingConfig.
 */
public static class GeminiFunctionCallingConfigBuilder {
    /**
     * Sets the function calling mode.
     * @param mode GeminiMode enum value
     * @return Builder instance for chaining
     */
    public GeminiFunctionCallingConfigBuilder mode(GeminiMode mode);

    /**
     * Sets the allowed function names.
     * @param allowedFunctionNames List of function names
     * @return Builder instance for chaining
     */
    public GeminiFunctionCallingConfigBuilder allowedFunctionNames(List<String> allowedFunctionNames);

    /**
     * Builds the GeminiFunctionCallingConfig instance.
     * @return Configured GeminiFunctionCallingConfig
     */
    public GeminiFunctionCallingConfig build();
}

GeminiMode Enum

Enum defining function calling modes.

/**
 * Function calling modes for Gemini models.
 */
public enum GeminiMode {
    /**
     * Model automatically decides whether to call functions.
     * Uses functions when appropriate for the query.
     */
    AUTO,

    /**
     * Model must call at least one function.
     * Forces function calling in the response.
     */
    ANY,

    /**
     * Model cannot call functions.
     * Disables function calling completely.
     */
    NONE
}

GeminiThinkingConfig

Configuration for thinking mode enabling extended reasoning with configurable depth and output.

/**
 * Configuration for thinking mode (extended reasoning).
 * Controls whether thinking process is included and reasoning depth.
 */
public record GeminiThinkingConfig(
    /**
     * Whether to include thinking process in response.
     * If true, model's reasoning steps are returned.
     */
    Boolean includeThoughts,

    /**
     * Token budget for thinking (optional).
     * Limits the number of tokens used for reasoning.
     */
    Integer thinkingBudget,

    /**
     * Thinking level as string (optional).
     * Can be "minimal", "low", "medium", or "high".
     */
    String thinkingLevel
) {
    /**
     * Creates a new builder for thinking config.
     * @return Builder instance
     */
    public static Builder builder();

    /**
     * Returns whether thoughts are included.
     * @return Boolean indicating thought inclusion
     */
    public Boolean includeThoughts();

    /**
     * Returns the thinking token budget.
     * @return Integer token budget (null if not set)
     */
    public Integer thinkingBudget();

    /**
     * Returns the thinking level string.
     * @return String thinking level (null if not set)
     */
    public String thinkingLevel();
}

GeminiThinkingConfig.Builder

Builder for constructing GeminiThinkingConfig instances.

/**
 * Builder for GeminiThinkingConfig.
 */
public static class Builder {
    /**
     * Sets whether to include thoughts in response.
     * @param includeThoughts True to include thinking process
     * @return Builder instance for chaining
     */
    public Builder includeThoughts(Boolean includeThoughts);

    /**
     * Sets the thinking token budget.
     * @param thinkingBudget Maximum tokens for reasoning
     * @return Builder instance for chaining
     */
    public Builder thinkingBudget(Integer thinkingBudget);

    /**
     * Sets the thinking level as string.
     * @param thinkingLevel Level string ("minimal", "low", "medium", "high")
     * @return Builder instance for chaining
     */
    public Builder thinkingLevel(String thinkingLevel);

    /**
     * Sets the thinking level using enum.
     * @param thinkingLevel GeminiThinkingLevel enum value
     * @return Builder instance for chaining
     */
    public Builder thinkingLevel(GeminiThinkingLevel thinkingLevel);

    /**
     * Builds the GeminiThinkingConfig instance.
     * @return Configured GeminiThinkingConfig
     */
    public GeminiThinkingConfig build();
}

GeminiThinkingLevel Enum

Enum defining thinking depth levels.

/**
 * Thinking depth levels for extended reasoning.
 */
public enum GeminiThinkingLevel {
    /**
     * Minimal reasoning depth.
     * Fast but less thorough.
     */
    MINIMAL,

    /**
     * Low reasoning depth.
     * Basic reasoning steps.
     */
    LOW,

    /**
     * Medium reasoning depth.
     * Balanced thoroughness and speed.
     */
    MEDIUM,

    /**
     * High reasoning depth.
     * Most thorough reasoning, slower.
     */
    HIGH
}

GeminiSafetySetting

Configuration for content safety filtering by category and threshold.

/**
 * Safety setting for content filtering.
 * Configures blocking behavior for specific harm categories.
 */
public class GeminiSafetySetting {
    /**
     * Creates a safety setting.
     * @param category Harm category to configure
     * @param threshold Block threshold for this category
     */
    public GeminiSafetySetting(GeminiHarmCategory category, GeminiHarmBlockThreshold threshold);

    /**
     * Gets the harm category.
     * @return GeminiHarmCategory enum value
     */
    public GeminiHarmCategory getCategory();

    /**
     * Gets the block threshold.
     * @return GeminiHarmBlockThreshold enum value
     */
    public GeminiHarmBlockThreshold getThreshold();

    /**
     * Sets the harm category.
     * @param category Harm category
     */
    public void setCategory(GeminiHarmCategory category);

    /**
     * Sets the block threshold.
     * @param threshold Block threshold
     */
    public void setThreshold(GeminiHarmBlockThreshold threshold);
}

GeminiHarmCategory Enum

Enum defining categories of potentially harmful content.

/**
 * Categories of harmful content for safety filtering.
 */
public enum GeminiHarmCategory {
    /**
     * Hate speech content.
     * Content promoting hatred against groups based on protected attributes.
     */
    HARM_CATEGORY_HATE_SPEECH,

    /**
     * Sexually explicit content.
     * Content containing sexual or adult material.
     */
    HARM_CATEGORY_SEXUALLY_EXPLICIT,

    /**
     * Dangerous content.
     * Content promoting dangerous acts, violence, or self-harm.
     */
    HARM_CATEGORY_DANGEROUS_CONTENT,

    /**
     * Harassment content.
     * Content that harasses, bullies, or threatens individuals.
     */
    HARM_CATEGORY_HARASSMENT,

    /**
     * Civic integrity violations.
     * Content that may interfere with civic processes or spread misinformation.
     */
    HARM_CATEGORY_CIVIC_INTEGRITY
}

GeminiHarmBlockThreshold Enum

Enum defining thresholds for blocking harmful content.

/**
 * Thresholds for blocking harmful content.
 * Higher thresholds block less content.
 */
public enum GeminiHarmBlockThreshold {
    /**
     * Unspecified threshold.
     * Uses default threshold.
     */
    HARM_BLOCK_THRESHOLD_UNSPECIFIED,

    /**
     * Block low probability and above.
     * Most restrictive, blocks the most content.
     */
    BLOCK_LOW_AND_ABOVE,

    /**
     * Block medium probability and above.
     * Moderate restriction.
     */
    BLOCK_MEDIUM_AND_ABOVE,

    /**
     * Block only high probability.
     * Least restrictive, only blocks obvious harmful content.
     */
    BLOCK_ONLY_HIGH,

    /**
     * Don't block any content.
     * Disables safety filtering for this category.
     */
    BLOCK_NONE
}

GeminiMediaResolutionLevel Enum

Enum defining resolution levels for processing images and videos.

/**
 * Resolution levels for media processing.
 * Controls quality vs speed tradeoff for image/video analysis.
 */
public enum GeminiMediaResolutionLevel {
    /**
     * Unspecified resolution.
     * Uses default resolution.
     */
    MEDIA_RESOLUTION_UNSPECIFIED,

    /**
     * Low resolution.
     * Fastest processing, lower detail.
     */
    MEDIA_RESOLUTION_LOW,

    /**
     * Medium resolution.
     * Balanced speed and detail.
     */
    MEDIA_RESOLUTION_MEDIUM,

    /**
     * High resolution.
     * High detail, slower processing.
     */
    MEDIA_RESOLUTION_HIGH,

    /**
     * Ultra high resolution.
     * Maximum detail, slowest processing.
     */
    MEDIA_RESOLUTION_ULTRA_HIGH
}

GeminiResponseModality Enum

Enum defining response modality types.

/**
 * Response modality types.
 * Specifies the type of response content.
 */
public enum GeminiResponseModality {
    /**
     * Text response.
     * Model returns text content.
     */
    TEXT,

    /**
     * Image response.
     * Model returns image content.
     */
    IMAGE;

    /**
     * Gets the string value of the modality.
     * @return String representation
     */
    public String getValue();
}

Usage Examples

Function Calling Configuration

import dev.langchain4j.model.googleai.GeminiFunctionCallingConfig;
import dev.langchain4j.model.googleai.GeminiMode;
import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel;

// Auto mode - model decides when to call functions
GeminiFunctionCallingConfig autoConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.AUTO)
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .toolConfig(autoConfig)
    .build();

Restricting Function Access

// Only allow specific functions
GeminiFunctionCallingConfig restrictedConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.AUTO)
    .allowedFunctionNames(List.of("get_weather", "search_web"))
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .toolConfig(restrictedConfig)
    .build();

// Model can only call get_weather and search_web functions

Forcing Function Calls

// ANY mode - model must call at least one function
GeminiFunctionCallingConfig forcedConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.ANY)
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .toolConfig(forcedConfig)
    .build();

// Model will always call a function in its response

Disabling Function Calling

// NONE mode - disable function calling
GeminiFunctionCallingConfig noFunctionsConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.NONE)
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .toolConfig(noFunctionsConfig)
    .build();

// Model will not call any functions

Thinking Configuration

import dev.langchain4j.model.googleai.GeminiThinkingConfig;
import dev.langchain4j.model.googleai.GeminiThinkingConfig.GeminiThinkingLevel;

// Enable thinking with high depth
GeminiThinkingConfig thinkingConfig = GeminiThinkingConfig.builder()
    .includeThoughts(true)
    .thinkingLevel(GeminiThinkingLevel.HIGH)
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.0-flash-thinking-exp")
    .thinkingConfig(thinkingConfig)
    .build();

// Model will include detailed reasoning in response

Thinking with Token Budget

// Limit thinking tokens
GeminiThinkingConfig budgetedThinking = GeminiThinkingConfig.builder()
    .includeThoughts(true)
    .thinkingBudget(1000) // Max 1000 tokens for reasoning
    .thinkingLevel(GeminiThinkingLevel.MEDIUM)
    .build();

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.0-flash-thinking-exp")
    .thinkingConfig(budgetedThinking)
    .build();

Safety Settings

import dev.langchain4j.model.googleai.GeminiSafetySetting;
import dev.langchain4j.model.googleai.GeminiHarmCategory;
import dev.langchain4j.model.googleai.GeminiHarmBlockThreshold;

// Configure safety settings
List<GeminiSafetySetting> safetySettings = List.of(
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HATE_SPEECH,
        GeminiHarmBlockThreshold.BLOCK_LOW_AND_ABOVE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HARASSMENT,
        GeminiHarmBlockThreshold.BLOCK_LOW_AND_ABOVE
    )
);

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .safetySettings(safetySettings)
    .build();

Lenient Safety Settings

// More permissive safety settings
List<GeminiSafetySetting> lenientSettings = List.of(
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HATE_SPEECH,
        GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HARASSMENT,
        GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH
    )
);

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .safetySettings(lenientSettings)
    .build();

Disabling Safety Filters

// Disable all safety filtering (use with caution)
List<GeminiSafetySetting> noFiltering = List.of(
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HATE_SPEECH,
        GeminiHarmBlockThreshold.BLOCK_NONE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        GeminiHarmBlockThreshold.BLOCK_NONE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        GeminiHarmBlockThreshold.BLOCK_NONE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HARASSMENT,
        GeminiHarmBlockThreshold.BLOCK_NONE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
        GeminiHarmBlockThreshold.BLOCK_NONE
    )
);

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .safetySettings(noFiltering)
    .build();

Media Resolution Configuration

import dev.langchain4j.model.googleai.GeminiMediaResolutionLevel;

// High resolution for detailed image analysis
GoogleAiGeminiChatModel highResModel = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_HIGH)
    .build();

// Low resolution for faster processing
GoogleAiGeminiChatModel lowResModel = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-flash")
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_LOW)
    .build();

// Ultra high for maximum detail
GoogleAiGeminiChatModel ultraResModel = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_ULTRA_HIGH)
    .build();

Combined Configuration

// Complex configuration with multiple settings
GeminiFunctionCallingConfig functionConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.AUTO)
    .allowedFunctionNames(List.of("search", "calculate"))
    .build();

GeminiThinkingConfig thinkingConfig = GeminiThinkingConfig.builder()
    .includeThoughts(true)
    .thinkingLevel(GeminiThinkingLevel.HIGH)
    .thinkingBudget(2000)
    .build();

List<GeminiSafetySetting> safetySettings = List.of(
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_HATE_SPEECH,
        GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
    ),
    new GeminiSafetySetting(
        GeminiHarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
    )
);

GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.0-flash-thinking-exp")
    .toolConfig(functionConfig)
    .thinkingConfig(thinkingConfig)
    .safetySettings(safetySettings)
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_HIGH)
    .build();

Dynamic Safety Configuration

public GoogleAiGeminiChatModel createModelWithSafetyLevel(String safetyLevel) {
    GeminiHarmBlockThreshold threshold = switch (safetyLevel.toLowerCase()) {
        case "strict" -> GeminiHarmBlockThreshold.BLOCK_LOW_AND_ABOVE;
        case "moderate" -> GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE;
        case "lenient" -> GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH;
        case "none" -> GeminiHarmBlockThreshold.BLOCK_NONE;
        default -> GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE;
    };

    List<GeminiSafetySetting> settings = Arrays.stream(GeminiHarmCategory.values())
        .map(category -> new GeminiSafetySetting(category, threshold))
        .toList();

    return GoogleAiGeminiChatModel.builder()
        .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
        .modelName("gemini-2.5-pro")
        .safetySettings(settings)
        .build();
}

// Usage
GoogleAiGeminiChatModel strictModel = createModelWithSafetyLevel("strict");
GoogleAiGeminiChatModel moderateModel = createModelWithSafetyLevel("moderate");

Per-Part Media Resolution

// Enable per-part media resolution for mixed content
GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_MEDIUM)
    .mediaResolutionPerPartEnabled(true)
    .build();

// Each media part can have its own resolution

Function Config Helper

public class FunctionConfigHelper {
    public static GeminiFunctionCallingConfig createAutoConfig() {
        return GeminiFunctionCallingConfig.builder()
            .mode(GeminiMode.AUTO)
            .build();
    }

    public static GeminiFunctionCallingConfig createRestrictedConfig(String... functionNames) {
        return GeminiFunctionCallingConfig.builder()
            .mode(GeminiMode.AUTO)
            .allowedFunctionNames(List.of(functionNames))
            .build();
    }

    public static GeminiFunctionCallingConfig createForcedConfig() {
        return GeminiFunctionCallingConfig.builder()
            .mode(GeminiMode.ANY)
            .build();
    }

    public static GeminiFunctionCallingConfig createDisabledConfig() {
        return GeminiFunctionCallingConfig.builder()
            .mode(GeminiMode.NONE)
            .build();
    }
}

// Usage
var autoConfig = FunctionConfigHelper.createAutoConfig();
var restrictedConfig = FunctionConfigHelper.createRestrictedConfig("search", "calculate");

Safety Settings Builder

public class SafetySettingsBuilder {
    private final Map<GeminiHarmCategory, GeminiHarmBlockThreshold> settings = new HashMap<>();

    public SafetySettingsBuilder strict() {
        for (GeminiHarmCategory category : GeminiHarmCategory.values()) {
            settings.put(category, GeminiHarmBlockThreshold.BLOCK_LOW_AND_ABOVE);
        }
        return this;
    }

    public SafetySettingsBuilder moderate() {
        for (GeminiHarmCategory category : GeminiHarmCategory.values()) {
            settings.put(category, GeminiHarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE);
        }
        return this;
    }

    public SafetySettingsBuilder lenient() {
        for (GeminiHarmCategory category : GeminiHarmCategory.values()) {
            settings.put(category, GeminiHarmBlockThreshold.BLOCK_ONLY_HIGH);
        }
        return this;
    }

    public SafetySettingsBuilder customize(GeminiHarmCategory category, GeminiHarmBlockThreshold threshold) {
        settings.put(category, threshold);
        return this;
    }

    public List<GeminiSafetySetting> build() {
        return settings.entrySet().stream()
            .map(e -> new GeminiSafetySetting(e.getKey(), e.getValue()))
            .toList();
    }
}

// Usage
List<GeminiSafetySetting> customSettings = new SafetySettingsBuilder()
    .moderate()
    .customize(GeminiHarmCategory.HARM_CATEGORY_HATE_SPEECH, GeminiHarmBlockThreshold.BLOCK_LOW_AND_ABOVE)
    .build();

Configuration Recommendations

Function Calling Modes

  • AUTO: Best for most use cases; model decides when functions are needed
  • ANY: Use when function results are required for every response
  • NONE: Use for pure conversational interactions without tools

Thinking Levels

  • MINIMAL: Quick responses, basic reasoning
  • LOW: Simple problems, faster processing
  • MEDIUM: Balanced depth for most tasks
  • HIGH: Complex problems requiring deep analysis

Safety Thresholds

  • BLOCK_LOW_AND_ABOVE: Most restrictive, for sensitive applications
  • BLOCK_MEDIUM_AND_ABOVE: Balanced approach, recommended default
  • BLOCK_ONLY_HIGH: Lenient filtering for content analysis
  • BLOCK_NONE: No filtering, use only when necessary

Media Resolution

  • LOW: Fast processing, simple image understanding
  • MEDIUM: Balanced quality and speed
  • HIGH: Detailed analysis, medical imaging, OCR
  • ULTRA_HIGH: Maximum detail, slow but comprehensive

Best Practices

  1. Function calling: Start with AUTO mode and restrict functions only if needed
  2. Thinking budget: Set thinking budget to control costs while enabling reasoning
  3. Safety settings: Use BLOCK_MEDIUM_AND_ABOVE as default for most applications
  4. Media resolution: Choose based on use case; higher isn't always better
  5. Configuration reuse: Create configuration objects once and reuse across models
  6. Testing: Test safety settings with edge cases to ensure appropriate filtering
  7. Documentation: Document why specific configuration choices were made

Integration Patterns

Builder Pattern

// All configuration classes use builder pattern
var functionConfig = GeminiFunctionCallingConfig.builder()
    .mode(GeminiMode.AUTO)
    .build();

var thinkingConfig = GeminiThinkingConfig.builder()
    .includeThoughts(true)
    .thinkingLevel(GeminiThinkingLevel.HIGH)
    .build();

With Chat Models

// Configuration integrates seamlessly with chat models
GoogleAiGeminiChatModel model = GoogleAiGeminiChatModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-pro")
    .toolConfig(functionConfig)
    .thinkingConfig(thinkingConfig)
    .safetySettings(safetySettings)
    .mediaResolution(GeminiMediaResolutionLevel.MEDIA_RESOLUTION_HIGH)
    .build();

With Image Models

// Safety settings also apply to image generation
GoogleAiGeminiImageModel imageModel = GoogleAiGeminiImageModel.builder()
    .apiKey(System.getenv("GOOGLE_AI_API_KEY"))
    .modelName("gemini-2.5-flash")
    .safetySettings(safetySettings)
    .build();

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-google-ai-gemini

docs

advanced-configuration.md

batch-processing.md

chat-streaming.md

chat-synchronous.md

configuration.md

embeddings.md

file-management.md

images.md

index.md

model-catalog.md

response-metadata.md

token-counting.md

tile.json