-
Notifications
You must be signed in to change notification settings - Fork 87
Description
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:
- Create a File Search Store
- Upload documents to it
- 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.