Gemini 1.5 Pro model — JSON format responses

Andres Sandoval
2 min readJun 1, 2024

The new Gemini models 1.5 Pro and 1.5 Flash support JSON responses. Previous version don’t support JSON response.

While developing an Android application using Gemini. I was able to add a prompt to get the JSON response and use Kotlin to parse the response string.

Gemini model configuration

val config = generationConfig {
responseMimeType = "application/json"
temperature = 0.9f
topK = 16
topP = 0.1f
//maxOutputTokens = 200
//stopSequences = listOf("red")
}

val generativeModel = GenerativeModel(
modelName = "gemini-1.5-flash",
apiKey = BuildConfig.GEM_API_KEY,
generationConfig = config
)

Prompt view model. We need to add in the prompt we want a to use a JSON schema and give example on our expected response

       val prompt = "Tell me information about the city $inputTravelCity"
"Using this JSON schema:" +
"Information = {\"city\": str,\"country\": str}" +
" Return a `list[Information]`"
        viewModelScope.launch {
try {…

--

--