When using AI in your application, you often need outputs that your code can parse, not just free-form text. Whether you’re generating data for a user interface, automating business logic, or orchestrating multi-step reasoning, predictable, machine-readable responses are essential.
That’s why 4D 21’s 4D AIKit introduces the new response_format attribute, letting you define the exact structure of the model’s output to ensure consistency, validation, and smooth integration into your app logic.
WHY STRUCTURED OUTPUTS MATTER
Structured outputs are useful every time you need to parse or interpret what an AI model produces.
For example:
- Data generation: Automatically create structured content such as product records, CRM entries, or configuration data that your app can use immediately.
- Reasoning & Planning: You can ask the model to propose next steps or workflow options that your app can display or execute automatically.
- Multi-step agents (ReAct pattern): Combine reasoning and acting, where the AI follows specific formats to guide its next action.
- Chain-of-thought control (visible part only): Capture the reasoning steps that you decide to expose to users for transparency or debugging.
- Debugging / Monitoring AI reasoning: Enforce structure so that the AI’s internal logic can be logged and analyzed.
Here’s a simple example of how an AI model could return structured reasoning data you can instantly parse in 4D:
{
"thought": "The customer seems satisfied overall but mentioned slow delivery.",
"next_step": "offer_discount"
}
Use case: The AI summarizes the reasoning and suggests a next action that your code can apply automatically.
Structured Outputs with response_format
When interacting with an AI model, developers often need results in a specific format, whether plain text, JSON objects, or data validated against a schema. With the new response_format attribute, you can now instruct the model to return its response in the exact structure you require.
Here’s another case where structured output ensures consistent, validated data, a financial summary returned as strict JSON:
var $client:=cs.AIKit.OpenAI.new($openAIKey)
var $chatHelper:=$ai.chat.create("You are a financial data analyst.")
// Define the expected response format for the AI
var $response_format:={type: "json_schema"; json_schema: {}}
$response_format.json_schema.name:="sales_summary"
$response_format.json_schema.schema:={type: "object"; properties: {}}
$response_format.json_schema.schema.properties.total_revenue:={type: "number"}
$response_format.json_schema.schema.properties.gross_margin:={type: "number"}
$response_format.json_schema.schema.properties.top_products:={Type: "array"; items: {Type: "String"}}
$response_format.json_schema.schema.required:=["total_revenue"; "gross_margin"; "top_products"]
$response_format.json_schema.schema.additionalProperties:=False
// Attach the JSON Schema format to the chat helper
$chatHelper.parameters.response_format:=$jsonFormat
// Compose the message to send to the AI:
var $message:="Here are the sales figures for Q3:\n"
$message+="- Product A: $120,000 in sales, 40% margin\n"
$message+="- Product B: $85,000 in sales, 35% margin\n"
$message+="- Product C: $60,000 in sales, 50% margin\n"
$message+="Please calculate the total revenue, the overall gross margin, "
$message+="and list the top 2 products by revenue."
var $result:=$chatHelper.prompt($message)
//$result.choice.message.text:="{
// "total_revenue": 265000,
// "gross_margin": 0,406603773,
// "top_products": ["Product A","Product B"]
// ]
//}"
The AI responds with a validated JSON object, ready to use in your database or UI, no post-processing required.
MODEL SUPPORT LANDSCAPE
Support for structured outputs varies depending on the model you’re using:
-
OpenAI GPT-4o and GPT-4-Turbo natively support response_format.
-
Local or other cloud models (e.g., Claude, Gemini, or Ollama models) may support structured outputs differently, sometimes via strict function calling, sometimes via text-based schema enforcement.
And remember: response_format isn’t limited to JSON. Depending on the model, other formats such as plain text, XML, or domain-specific syntaxes may also be supported.
Conclusion
With the response_format in 4D AIKit, you can ensure that your AI outputs are exactly as you need them, consistent, reliable, and ready to plug straight into your UI, database, or any other system. This feature helps developers parse AI completions effortlessly and integrate intelligence deeply into their 4D applications. It transforms AI from a simple chat partner into a powerful, structured reasoning engine, enabling smarter, more dynamic features than users ever imagined.
