Agree, the google dev docs is a maze to navigate through.
You can set json mode by simply passing response_mime_type = "application/json"
in generationConfig
.
Here’s the sample code I wrote:
import google.generativeai as genai
import json
model = genai.GenerativeModel("gemini-1.5-pro-latest")
json_schema = {
"type": "object",
"properties": {
"characters": {
"type": "array",
"items": {"type": "string"},
"minItems": 1,
"description": "List of character names in the story.",
},
"story": {
"type": "string",
"description": "The story in which all the characters are involved",
},
},
"required":["characters", "story"]
}
response = model.generate_content(
f"Write a short story and list its characters in the following JSON schema: {json.dumps(json_schema)}",
generation_config=genai.types.GenerationConfig(
# Only one candidate for now.
candidate_count=1,
stop_sequences=["x"],
max_output_tokens=700,
temperature=1.0,
response_mime_type="application/json",
),
)
print(response)
and its output:
response:
GenerateContentResponse(
done=True,
iterator=None,
result=glm.GenerateContentResponse({'candidates': [{'content': {'parts': [{'text': '{"characters": ["Amelia", "Oliver", "Jasper"], "story": "Amelia nervously paced the cobblestone streets, her eyes searching the crowd for Oliver. He was late, as usual, but today it felt different. A shadow fell over her and she turned to see Jasper, a man she knew only through Oliver\'s stories. \\"He won\'t be coming,\\" Jasper said, his voice low. \\"He\'s gone.\\""}\n\n'}], 'role': 'model'}, 'finish_reason': 1, 'index': 0, 'safety_ratings': [{'category': 9, 'probability': 1, 'blocked': False}, {'category': 8, 'probability': 1, 'blocked': False}, {'category': 7, 'probability': 1, 'blocked': False}, {'category': 10, 'probability': 1, 'blocked': False}], 'token_count': 0, 'grounding_attributions': []}]}),
)