Skip to main content

Introduction

By default, language models generate plain text. This is great for applications like chatbots, but it becomes difficult to work with when you need to programmatically extract specific details from the response. Several models on the Nusantara AI platform have the capability to respond in a structured JSON format. This feature allows you to work directly with the model’s output data in your application code without complex text parsing. To enable this feature, you simply need to add the response_format parameter to your API request.

Supported Models

Nearly all major text-generation models on the Nusantara AI platform support JSON Mode, including:
  • nusantara-base
  • archipelago-70b
  • garda-beta-mini
Vision models like vision-emas-2045 also support JSON extraction from images.

Handling and Parsing the Response

Different models may return JSON in slightly different formats. To ensure your application is robust, it’s best to parse the response with a helper function that can handle these variations. Some models wrap the JSON in markdown fences (e.g., ```json ... ```), while others might nest the JSON string inside another JSON object. The following examples include a robust parsing function.
Important: You must instruct the model to respond only in JSON format within your system prompt or user message, in addition to including the response_format parameter.
shell
curl https://api.neosantara.xyz/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <YOUR_API_KEY>" \
  -d '{
    "model": "nusantara-base",
    "messages": [
      {
        "role": "user",
        "content": "I need a fried rice recipe. I have rice, an egg, shallots, garlic, and soy sauce. It should take about 15 minutes to cook."
      }
    ],
    "response_format": {
      "type": "json_object"
    }
  }'

Expected Result

After running any of the examples above, your output variable will contain a clean, parsed JSON object, regardless of which model responded:
{
  "recipe_name": "Simple Fried Rice",
  "cooking_time": "15 minutes",
  "ingredients": [
    "Rice",
    "Egg",
    "Shallots",
    "Garlic",
    "Soy Sauce"
  ]
}
By using a robust parsing function on the client-side, you can reliably handle structured data from any model on the Nusantara AI platform.