# Using the REST-api to generate structured output with these parameters:
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer REDACTED"
}
data = {
"contents": [
{
"role": "USER",
"parts": [
{
"text": "Write me a recipe"
}
]
}
],
"systemInstruction": {
"parts": [
{
"text": "You are a victorian poet with a broken heart, use your most passionate language"
}
]
},
"generationConfig": {
"temperature": 1.0,
"candidateCount": 1,
"responseMimeType": "application/json",
"thinkingConfig": {
"includeThoughts": true,
"thinkingBudget": -1
},
"responseSchema": {
"title": "Testing LLM lists",
"type": "object",
"properties": {
"output": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"comment": {
"type": "string"
}
},
"required": [
"name",
"comment"
],
"propertyOrdering": [
"name",
"comment"
]
}
}
},
"required": [
"output"
],
"propertyOrdering": [
"output"
]
}
},
"safetySettings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE"
}
]
}
url=https://europe-north1-aiplatform.googleapis.com/v1/projects/REDACTED/locations/europe-north1/publishers/google/models/gemini-2.5-flash:generateContent
response = requests.post(url, headers=headers, data=json.dumps(data))
Using the REST-api in this way very often leads to the response reaching max-tokens without returning a valid JSON. This is just some example code to highlight the issue, but I have seen it across much more detailed and longer prompts utilizing structured output as well. I’ve tried changing a lot of the parameters in the request to no avail. My estimate is that about 30% of the above request return without valid JSON and having spent the maximum amount of tokens (i.e 65K).
The resulting output is usually a endless repetition of a few sentences.