mhawksey / GeminiApp

GeminiApp is a library that allows integration to Google's Gemini API in your Google Apps Script projects. It allows for mutli-modal prompts, structured conversations and function calling
Apache License 2.0
67 stars 4 forks source link

FEATURE REQUEST: Support for structured response #8

Open BJG87 opened 1 week ago

BJG87 commented 1 week ago

I was just wondering if you had any plans to integrate the option to request a structured response in the form of JSON from Gemini by providing schema?

I have used schemas in the past using the node js library and they can be really effective when you need to force a specific response as sometimes Gemini can waffle on even when you ask it to be specific. Link to Gemini Structured Response

I could possibly look into adding it myself but I didn't want to waste my time if it's already been tried and abandoned for some reason I have not yet realised.

mhawksey commented 1 day ago

Hi Brett - a useful request. I've pushed an update to a branch for you to test it hasn't broken any of your existing uses of the GeminiApp library. Here is the updated library code and a test you can try

BJG87 commented 12 hours ago

Thanks very much. I've tested the new library code and it doesn't seem to have affected any old usage. I am getting an error though when trying to run the test code your provided.

Request to Gemini failed with response code 400 - { "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"responseMimeType\" at 'generation_config': Cannot find field.\nInvalid JSON payload received. Unknown name \"responseSchema\" at 'generation_config': Cannot find field.", "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "field": "generation_config", "description": "Invalid JSON payload received. Unknown name \"responseMimeType\" at 'generation_config': Cannot find field." }, { "field": "generation_config", "description": "Invalid JSON payload received. Unknown name \"responseSchema\" at 'generation_config': Cannot find field." } ] } ] } }

I'm not sure if it has something to do with the generationConfig object being in camel case compared to the generation_config object mentioned in the error message being in snake case.

The documentation seems a little unclear about this and when I tried other generationConfig options like "maxOutputTokens" it worked fine. It just doesn't like "responseMimeType" and "responseSchema".

I also tried different Gemini models but I got the same error message.