Skip to content

FileSearch tool not working with generateContent - missing tool_type field #685

@yarivMobitti

Description

@yarivMobitti

What I'm trying to do:

I'm trying to use the File Search feature with the Gemini API via the Java SDK. Specifically, I want to:

  1. Create a File Search Store
  2. Upload documents to it
  3. Query the store using generateContent with a FileSearch tool

The store creation and file upload work perfectly, but querying fails when using the FileSearch tool.

Code I've tried:

import com.google.genai.Client;
import com.google.genai.types.*;

public class FileSearchExample {
public static void main(String[] args) throws Exception {
String apiKey = "YOUR_API_KEY";
Client client = Client.builder().apiKey(apiKey).build();

      // This works fine - store and file upload
      FileSearchStore store = client.fileSearchStores.create(
          CreateFileSearchStoreConfig.builder()
              .displayName("Test Store")
              .build()
      );
      String storeId = store.name().get();

      // Upload and import file (this also works)
      File uploadedFile = client.files.upload(
          new java.io.File("document.pdf"),
          UploadFileConfig.builder().displayName("Test Doc").build()
      );
      client.fileSearchStores.importFile(
          storeId,
          uploadedFile.name().get(),
          ImportFileConfig.builder().build()
      );

      // THIS FAILS - Query with FileSearch tool
      Tool tool = Tool.builder()
          .fileSearch(FileSearch.builder()
              .fileSearchStoreNames(storeId)
              .build())
          .build();

      GenerateContentResponse response = client.models.generateContent(
          "gemini-2.0-flash-exp",
          "What is this document about?",
          GenerateContentConfig.builder()
              .tools(List.of(tool))
              .build()
      );
  }

}

Error message:

Exception in thread "main" com.google.genai.errors.ClientException: 400 . The GenerateContentRequest proto is invalid:
* tools[0].tool_type: required one_of 'tool_type' must have one initialized field
at com.google.genai.errors.ApiException.throwFromResponse(ApiException.java:94)
at com.google.genai.HttpApiResponse.getBody(HttpApiResponse.java:37)
at com.google.genai.Models.processResponseForPrivateGenerateContent(Models.java:4708)
at com.google.genai.Models.privateGenerateContent(Models.java:4762)
at com.google.genai.Models.generateContent(Models.java:6090)
at com.google.genai.Models.generateContent(Models.java:6164)

Expected behavior:

The SDK should properly serialize the FileSearch tool and send it to the API, similar to how the REST API works with this JSON structure:

{
"contents": [{"parts": [{"text": "What is this document about?"}]}],
"tools": [{
"retrieval": {
"fileSearch": {
"fileSearchStoreNames": ["fileSearchStores/abc123"]
}
}
}]
}

Environment:

  • SDK Version: com.google.genai:google-genai:1.26.0
  • Java Version: 21
  • OS: macOS

Additional context:

The REST API documentation shows that File Search should work with the retrieval tool type containing a fileSearch configuration: https://ai.google.dev/api/file-search/file-search-stores

The error message suggests that the Tool object isn't properly setting the tool_type field when using fileSearch(). It seems like the SDK might be missing the proper mapping between the FileSearch builder and the underlying proto field.

Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.status:awaiting user responsetype: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions