AICredits logo
API

Structured Outputs

Get LLM responses as valid JSON using JSON mode or strict JSON Schema. Works with all supported providers.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

Force LLM responses to follow a specific JSON format. Use JSON mode for flexible extraction, or strict JSON Schema for guaranteed schema conformance.

JSON Mode

Set response_format to {"type": "json_object"} to instruct the model to output valid JSON. Always include a system prompt describing the expected structure.

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    response_format={"type": "json_object"},
    messages=[
        {
            "role": "system",
            "content": """Extract invoice details and return as JSON:
{"vendor": string, "amount": number, "currency": string, "date": string}""",
        },
        {
            "role": "user",
            "content": "TechCorp invoice, ₹13,500, 15 Jan 2025",
        },
    ],
)

data = json.loads(response.choices[0].message.content)
print(data["vendor"])   # TechCorp
print(data["amount"])   # 13500
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.aicredits.in/v1",
  apiKey: "sk-your-key-here",
});

const response = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  response_format: { type: "json_object" },
  messages: [
    {
      role: "system",
      content: `Extract invoice details as JSON:
{"vendor": string, "amount": number, "currency": string}`,
    },
    { role: "user", content: "TechCorp invoice, ₹13,500, 15 Jan 2025" },
  ],
});

const data = JSON.parse(response.choices[0].message.content!);
console.log(data.vendor);  // TechCorp
console.log(data.amount);  // 13500

Structured Outputs (JSON Schema)

For strict schema enforcement, use "type": "json_schema" with a full JSON Schema definition. The model only outputs JSON that matches the schema exactly (supported on OpenAI GPT-4o and later):

structured_outputs.py
from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "invoice",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "vendor": {"type": "string"},
                    "amount": {"type": "number"},
                    "currency": {"type": "string", "enum": ["INR", "USD", "EUR"]},
                    "date": {"type": "string", "description": "ISO 8601 date"},
                    "items": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "description": {"type": "string"},
                                "quantity": {"type": "integer"},
                                "unit_price": {"type": "number"},
                            },
                            "required": ["description", "quantity", "unit_price"],
                            "additionalProperties": False,
                        },
                    },
                },
                "required": ["vendor", "amount", "currency", "date", "items"],
                "additionalProperties": False,
            },
        },
    },
    messages=[
        {"role": "user", "content": "Parse: TechCorp invoice, 3x server licenses at ₹4500 each, 15 Jan 2025"},
    ],
)

invoice = json.loads(response.choices[0].message.content)
print(invoice)

Response Healing

When JSON mode is enabled and a provider returns malformed JSON, AICredits automatically repairs the response before returning it. This handles:

  • Truncated JSON (missing closing } or ])
  • Markdown code fences wrapping the JSON (```json ... ```)
  • Extra text before or after the JSON object
  • Trailing commas and minor syntax errors
# Provider returned (truncated):
'{"vendor": "TechCorp", "amount": 13500, "items": [{"description": "Server'

# After response healing:
'{"vendor": "TechCorp", "amount": 13500, "items": [{"description": "Server"}]}'

Response healing is transparent — you receive valid JSON even when the provider's raw output was slightly malformed. When healing is not possible, the raw response is returned and you must handle the parsing error in your code.

Prompting Tips

  • Show an example in the system prompt — include a sample JSON object so the model has a concrete target format
  • Use snake_case keys — models handle them consistently across languages
  • Enumerate allowed values — for fields with a fixed set, list them: "status must be one of: pending, approved, rejected"
  • Set temperature: 0 for extraction tasks — reduces hallucinated field values when the answer is in the input

Provider Support

ProviderJSON ModeStrict JSON Schema
OpenAI GPT-4o / GPT-4o-miniYesYes
OpenAI o1, o3YesYes
Anthropic ClaudeYes (via prompt)Via tool calling
Google GeminiYesPartial
DeepSeekYesNo
MistralYesNo

For maximum portability, use JSON mode with a clear prompt schema. Strict JSON Schema mode is most reliable on OpenAI models; for other providers, combine JSON mode with precise system prompt instructions.

On this page