Skip to content

Conversation

@nuheajiohc
Copy link

@nuheajiohc nuheajiohc commented Nov 27, 2025

Summary

This PR adds an option to enable the Gemini URL Context tool in GoogleGenAiChatModel via GoogleGenAiChatOptions.

  • Introduce urlContextEnabled flag in GoogleGenAiChatOptions (default: false).
  • When the flag is true, the generated Gemini request includes the UrlContext tool.
  • Update builder/copy/equals/hashCode/toString to handle the new option.
  • Add tests to verify option copying/equality and request generation with the URL Context tool.

Background

Gemini provides a URL Context tool that allows the model to ground its answers in the content of a given URL (e.g. blog posts, documentation pages, articles, etc.).
Gemini API - Tools [Url Context]

The Google Java SDK already allows this:

Client client = new Client();

String prompt = "'https://www.example-article.com', Who is the author of this article?";

GenerateContentConfig config = GenerateContentConfig.builder()
    .tools(List.of(
        Tool.builder()
            .urlContext(UrlContext.builder().build())
            .build()
    ))
    .build();

GenerateContentResponse response =
    client.models.generateContent(
        "gemini-2.5-flash",
        prompt,
        config
    );

System.out.println(response.text());

However, Spring AI’s GoogleGenAiChatModel does not currently expose a simple way to enable this feature through GoogleGenAiChatOptions.

Changes

  • GoogleGenAiChatOptions

    • Add private Boolean urlContextEnabled
    • Extend builder with urlContextEnabled(Boolean urlContextEnabled)
    • Include field in copy/from, equals, hashCode, toString
  • GoogleGenAiChatModel

    • Add UrlContext tool when urlContextEnabled == Boolean.TRUE
    • Keep default behavior unchanged when disabled
  • Tests

    • Validate option propagation
    • Validate equals / hashCode
    • Validate request contains or excludes UrlContext

Example usage

String apiKey = System.getenv("GEMINI_API_KEY"); // Set your API key as an environment variable

GoogleGenAiChatModel chatModel = GoogleGenAiChatModel.builder()
			.genAiClient(Client.builder().apiKey(apiKey).build())
			.defaultOptions(GoogleGenAiChatOptions.builder().model(GoogleGenAiChatModel.ChatModel.GEMINI_2_5_FLASH).build())
			.build();

ChatClient chatClient = ChatClient.builder(chatModel).build();

GoogleGenAiChatOptions options = GoogleGenAiChatOptions.builder()
    .urlContextEnabled(true)
    .build();

String prompt = "'https://www.example-article.com', Who is the author of this article?";

String answer = chatClient
    .prompt(prompt)
    .options(options)
    .call()
    .content();

Signed-off-by: Jaehun Choi <jaehun341012@gmail.com>
@nuheajiohc nuheajiohc force-pushed the feature/genai-url-context branch from 5830502 to 594d45c Compare November 28, 2025 00:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant