Groq Structured Output Fails to Return JSON When Result Is Empty

I’m currently working on a project that uses the Groq API with structured output (JSON). Everything works fine as long as the model returns actual data, the response is formatted correctly based on the schema.

However, I’ve run into an issue:
When the model has no result to return (e.g., nothing matches, or the model decides the output should be empty), Groq does NOT return JSON. Instead, it returns an empty string or a plain-text response that doesn’t match the JSON schema at all.

This causes my code to throw parsing errors because I’m always expecting a JSON object, even if it’s empty.

It would be much better if Groq always returned a valid JSON structure (like {} or values set to null) when structured output is enabled. That way, downstream code wouldn’t break just because the model had no content to return.

Has anyone else encountered this, and is there a known workaround or best practice?

"error": {
        "message": "Failed to validate JSON. Please adjust your prompt. See 'failed_generation' for more details.",
        "type": "invalid_request_error",
        "code": "json_validate_failed",
        "failed_generation": ""
      }

Could you paste a curl to reproduce this? This is somewhat model-dependent, I’m using Kimi K2 and it does output nullified values in a proper json object

my request:

{
    "model": "moonshotai/kimi-k2-instruct-0905",
    "messages": [
        {
            "role": "system",
            "content": "Extract product review information from the text for the Edifier M60 Speakers."
        },
        {
            "role": "user",
            "content": "once upon a time"
        }
    ],
    "response_format": {
        "type": "json_schema",
        "json_schema": {
            "name": "product_review",
            "schema": {
                "type": "object",
                "properties": {
                    "product_name": {
                        "type": "string"
                    },
                    "rating": {
                        "type": "number"
                    },
                    "sentiment": {
                        "type": "string",
                        "enum": [
                            "positive",
                            "negative",
                            "neutral"
                        ]
                    },
                    "key_features": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "required": [
                    "product_name",
                    "rating",
                    "sentiment",
                    "key_features"
                ],
                "additionalProperties": false
            }
        }
    }
}

my response:

{
    "id": "chatcmpl-ab56c2d6-62bb-4a07-8ad8-37b4c08071dc",
    "object": "chat.completion",
    "created": 1765403378,
    "model": "moonshotai/kimi-k2-instruct-0905",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "{\"product_name\":\"Edifier M60 Speakers\",\"rating\":3,\"sentiment\":\"neutral\",\"key_features\":[]}"
            },
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "queue_time": 0.02443171,
        "prompt_tokens": 137,
        "prompt_time": 0.020237796,
        "completion_tokens": 24,
        "completion_time": 0.07095364,
        "total_tokens": 161,
        "total_time": 0.091191436
    },
    "usage_breakdown": null,
    "service_tier": "on_demand"
}