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 theresponse_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-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
Expected Result
After running any of the examples above, youroutput
variable will contain a clean, parsed JSON object, regardless of which model responded: