█

AI/TLDR

मुझे पिलाएं कॉफ़ी
मुझे एक कॉफ़ी पिलाएँ

JSON Mode vs Structured Outputs vs Function Calling: कौन सा चुनें?

तीनों JSON extraction तरीकों को समझें, हर एक असल में क्या गारंटी देता है, और सही तरीका चुनने का एक सरल नियम।

मध्यम13 मिनट पढ़ेंअपडेट 12 जून 2026

सीधी भाषा में

आपको चाहिए कि LLM ऐसा JSON दे जिसे आपका कोड भरोसे के साथ parse कर सके। ऐसा करने के लिए तीन अलग तरीके हैं, और वे आपस में बदले नहीं जा सकते। हर एक अलग स्तर की गारंटी, अलग API सतह और अलग उद्देश्य के साथ आता है। LLM-आधारित ऐप्स में इन्हें आपस में मिला देना production की सबसे आम छिपी हुई गड़बड़ियों में से एक है।

JSON Mode vs Structured Outputs vs Function Calling — डायग्राम
JSON Mode, Structured Outputs और Function Calling की तुलना — anyscale.com

JSON mode सबसे पुराना विकल्प है। आप मॉडल से कहते हैं कि "JSON में जवाब दो", और वह सिंटैक्टिक रूप से वैध JSON देगा: curly braces बराबर, strings quoted, कोई trailing comma नहीं। लेकिन इसमें कोई गारंटी नहीं कि कौन सी keys आएँगी या उनके कौन से types होंगे। Schema पूरी तरह प्रॉम्प्ट से मॉडल के अंदाज़े पर निर्भर रहता है।

Structured outputs आधुनिक अपग्रेड है। आप पूरी JSON Schema देते हैं और API गारंटी देता है कि जवाब उसी से मेल खाएगा: हर required field मौजूद, हर type सही, कोई extra key नहीं, कोई गढ़ी हुई enum वैल्यू नहीं। अंदर से inference engine आपकी schema को एक grammar में compile करता है और sampling के समय अमान्य tokens को mask कर देता है, इसलिए उल्लंघन construction से ही असंभव है।

Function calling (जिसे tool use भी कहते हैं) एक अलग काम के लिए बना है: मॉडल को आपके कोड में actions trigger करने देना। आप ऐसे functions का सेट बताते हैं जिन्हें मॉडल call कर सकता है, और जब वह किसी एक को invoke करने का फैसला करता है, तो API एक structured argument payload लौटाता है, अंतिम जवाब नहीं। इसकी मुख्य गारंटी action चुनने और argument के shape के बारे में है, न कि उपयोगकर्ता को डेटा लौटाने के बारे में।

एक उपयोगी उदाहरण: JSON mode ऐसा है जैसे किसी waiter से कहना कि "मेरा ऑर्डर किसी तरह की list format में लिख दो"। शायद वह लिख देगा, लेकिन आप अपना order-processing system इस बात पर दांव पर नहीं लगा सकते कि उसका सटीक shape क्या होगा। Structured outputs ऐसा है जैसे उसे लेबल वाले बक्सों वाला पहले से छपा फॉर्म थमा देना: waiter किसी required field को छोड़ नहीं सकता। Function calling ऐसा है जैसे किचन स्टाफ को एक बटन पैनल देना। जब waiter सही बटन दबाता है, तो किचन उस slip पर लिखे parameters के साथ एक खास action शुरू कर देता है।

यह क्यों मायने रखता है

गलत mechanism चुनने से production में असली समस्याएँ आती हैं। JSON mode के साथ शिप करने वाली टीमें production में पाती हैं कि मॉडल कभी-कभी ऐसी "notes" key जोड़ देता है जो उन्होंने माँगी ही नहीं थी, context लंबा होने पर कोई required field छोड़ देता है, या ऐसी enum वैल्यू लौटाता है जो उनकी allowlist में नहीं है। इनमें से हर स्थिति का मतलब है try/catch, retry और अतिरिक्त latency, या उससे भी बुरा, चुपचाप होने वाली data corruption।

जब आपको सिर्फ़ structured data चाहिए, तब function calling चुनने से बिना किसी फ़ायदे के जटिलता बढ़ती है: आपको एक dummy tool define करना पड़ता है, अपने message loop में tool-call turn संभालना पड़ता है, और फिर अंतिम जवाब जोड़ना पड़ता है। जबकि schema वाले response_format से यही काम एक round trip में हो जाता।

उल्टी तरफ़ जाएँ, यानी जब मॉडल को तय करना हो कि कई में से कौन सा action लेना है, तब response_format इस्तेमाल करना। इसका मतलब है कि आप मॉडल की सही tool तक routing करने और typed arguments पास करने की क्षमता खो देते हैं। फिर आपको JSON blob से action का नाम parse करके खुद dispatch करना पड़ता है, जो ठीक वही काम है जो function calling पहले से आपके लिए करता है।

हर mechanism कैसे काम करता है

तीनों mechanisms एक ही अंतर्निहित token-generation pipeline पर चलते हैं। फ़र्क इस बात में है कि हर position पर कौन से tokens valid हैं, इस पर आप inference engine को कितना नियंत्रण सौंपते हैं।

JSON mode: सिर्फ़ syntactic validity

JSON mode (OpenAI के API में response_format: {type: "json_object"}) system prompt और एक हल्की decode-time constraint के ज़रिए मॉडल को संकेत देता है कि आउटपुट parse होने वाला JSON होना चाहिए। यह constraint सतही है: यह सुनिश्चित करता है कि पूरा structure एक JSON value हो, लेकिन keys का कोई खास सेट, value types या nesting depth लागू नहीं करता। पुराने या कम सक्षम मॉडलों पर मॉडल अब भी keys छोड़ सकता है या ऐसे fields गढ़ सकता है जो प्रॉम्प्ट ने माँगे ही नहीं थे।

Structured outputs: grammar-आधारित schema enforcement

Structured outputs (response_format: {type: "json_schema", json_schema: {...}, strict: true}) आपकी JSON Schema को token-level grammar में compile करता है। हर sampling step पर inference engine उन tokens को mask कर देता है जो schema का उल्लंघन करेंगे। अगर वर्तमान position पर boolean अपेक्षित है, तो सिर्फ़ true और false tokens अनुमति पाते हैं। नतीजा यह कि schema उल्लंघन गणितीय रूप से असंभव हैं: मॉडल ऐसा जवाब कभी नहीं दे सकता जो parse न हो या schema से मेल न खाए। जब OpenAI ने structured outputs पेश किए, तो उसने बताया कि यह फ़ीचर चालू होने पर schema-adherence दर लगभग परफ़ेक्ट रही, जबकि grammar-level enforcement न होने वाले मॉडलों पर सिर्फ़ प्रॉम्प्टिंग से यह दर कहीं कम थी।

Function calling: typed arguments के साथ action routing

Function calling (tools: [...]) अलग तरीके से काम करता है। आप एक या अधिक function definitions देते हैं (नाम, विवरण, parameter schema)। मॉडल बातचीत पढ़कर तय करता है कि सामान्य रूप से जवाब दे या आपके किसी function को call करे। अगर वह function call करता है, तो API एक खास message लौटाता है जिसमें finish_reason: "tool_calls" होता है और function का नाम व JSON argument payload होता है। आपके कोड को यह turn संभालना होता है: असली function call करें, नतीजा message history में जोड़ें, और बातचीत को वापस मॉडल के पास भेजें ताकि अंतिम जवाब मिले। Function definition के अंदर strict: true सेट करने से वही grammar-आधारित enforcement चालू होता है जो structured outputs में है, यानी arguments ठीक parameter schema से मेल खाएँगे।

कोड में साथ-साथ तुलना

तीनों mechanisms कोड में एक जैसे दिखते हैं, लेकिन उनके मतलब बहुत अलग हैं। नीचे दिए उदाहरण OpenAI Python SDK का उपयोग करके call site पर मुख्य अंतर दिखाते हैं।

JSON mode

pythonpython
response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Return info about Paris as JSON."}],
    response_format={"type": "json_object"}   # valid JSON guaranteed, schema is not
)
# Still need to check that the keys you expect are actually present
import json
data = json.loads(response.choices[0].message.content)

Structured outputs

pythonpython
from pydantic import BaseModel

class CityInfo(BaseModel):
    name: str
    country: str
    population: int
    capital: bool

# .parse() sends the Pydantic schema and returns a typed object — no json.loads needed
response = client.beta.chat.completions.parse(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Return info about Paris."}],
    response_format=CityInfo,
)
city: CityInfo = response.choices[0].message.parsed
print(city.population)   # int, guaranteed

Function calling

pythonpython
import json

tools = [{
    "type": "function",
    "function": {
        "name": "get_city_info",
        "description": "Look up current data for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city_name": {"type": "string"}
            },
            "required": ["city_name"],
            "additionalProperties": False
        },
        "strict": True   # arguments will exactly match the schema
    }
}]

# First turn: model decides to call the tool
response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "What's the population of Paris?"}],
    tools=tools,
    tool_choice="auto"
)

tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)   # {"city_name": "Paris"}

# Your code runs the actual lookup here:
result = fetch_city_from_database(args["city_name"])

# Second turn: send the result back so the model can answer
messages = [
    {"role": "user", "content": "What's the population of Paris?"},
    response.choices[0].message,   # the tool_call message
    {"role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result)}
]
final = client.chat.completions.create(model="gpt-5.5", messages=messages)
print(final.choices[0].message.content)

आपको कौन सा इस्तेमाल करना चाहिए?

सबसे सरल नियम: अगर आप अपने कोड के उपभोग के लिए डेटा निकाल रहे हैं या बना रहे हैं, तो structured outputs इस्तेमाल करें। अगर मॉडल को तय करना है कि कौन सा action लेना है और उस action को typed arguments देने हैं, तो function calling इस्तेमाल करें। JSON mode पर तभी लौटें जब आप किसी पुराने मॉडल पर हों जो structured outputs सपोर्ट नहीं करता।

स्थितिसबसे अच्छा mechanism
किसी document से fields निकालें (नाम, तारीख, राशि)Structured outputs
Support ticket को पहले से तय categories में classify करेंStructured outputs
उपयोगकर्ता के विवरण से form value set बनाएँStructured outputs
मॉडल को आपके API से मौजूदा मौसम लाने देंFunction calling
ऐसा agent बनाएँ जो search, read और write files कर सकेFunction calling
उपयोगकर्ता के इरादे को कई backend actions में से एक तक route करेंFunction calling
पुराने मॉडल endpoint पर जल्दी prototype बनाएँ, जहाँ structured outputs उपलब्ध नहीं हैंJSON mode
डेटा निकालना है और मॉडल को tool भी call करना पड़ सकता हैStructured outputs + strict: true वाले tools

Anthropic और Google एक ही पैटर्न पर आ गए हैं। Claude ने नवंबर 2025 में अपने Opus और Sonnet मॉडलों पर structured outputs लॉन्च किए (JSON response के लिए output_config.format और tool parameters के लिए strict: true), उसी grammar-compilation तरीके से। Gemini API controlled generation के लिए JSON Schema object के साथ response_schema इस्तेमाल करता है और Google GenAI SDK के ज़रिए Pydantic/Zod schemas सपोर्ट करता है। तीनों ecosystems में मूल गारंटी एक ही है: schema को grammar में compile किया जाता है, और decode के समय अमान्य tokens mask किए जाते हैं।

Parallel tool calls में अतिरिक्त सावधानी चाहिए। जब parallel_tool_calls चालू होता है (ज़्यादातर OpenAI मॉडलों पर डिफ़ॉल्ट), तो मॉडल एक turn में कई tools call कर सकता है और उन सभी पर strict mode को हमेशा सही तरीके से नहीं मानता। अगर strict argument schemas बेहद ज़रूरी हैं और आप ऐसा व्यवहार दिखाने वाला मॉडल इस्तेमाल कर रहे हैं, तो OpenAI parallel_tool_calls: false सेट करने की सलाह देता है।

और गहराई से

तीनों mechanisms से सहज होने के बाद, अगली परत की बारीकियाँ schema design, provider के बीच अंतर और streaming से जुड़ी हैं।

Structured outputs के लिए schema design की सीमाएँ

JSON Schema की सभी सुविधाएँ strict mode में समर्थित नहीं हैं। OpenAI की grammar-आधारित enforcement के लिए ज़रूरी है कि हर object के लिए additionalProperties को false सेट किया जाए और सभी fields required में हों। असमर्थित keywords (जैसे if/then, not, contains) या तो चुपचाप अनदेखे कर दिए जाते हैं या request के समय validation error देते हैं, generation के समय नहीं। Production में भेजने से पहले हमेशा अपनी schema को provider की समर्थित keywords list के सामने जाँचें।

Structured outputs के साथ streaming

Structured outputs को सामान्य completions की तरह token-by-token stream किया जा सकता है। Schema enforcement server पर sampling के समय होता है, इसलिए हर streamed chunk पहले से schema-legal होता है। लेकिन JSON को parse करने के लिए पूरा होना ज़रूरी है, इसलिए आप बीच के chunks पर JSON.parse को सार्थक तरीके से नहीं चला सकते। आम पैटर्न ये हैं: पूरा stream buffer करें और अंत में एक बार parse करें, या ऐसा streaming JSON parser इस्तेमाल करें (जैसे Node.js में jsonstream) जो fields पूरे होते ही आंशिक objects emit कर सके। आधिकारिक SDKs में Pydantic/Zod के .stream() helpers यह अपने-आप संभाल लेते हैं।

Structured output arguments के साथ function calling

जब आप strict: true को function definition में जोड़ते हैं, तो structured-output enforcement tool calls के arguments payload पर लागू होता है। Generation के समय argument JSON पर वही grammar-आधारित masking चलता है। इसका मतलब है कि दोनों फ़ायदे मिलते हैं: मॉडल सही tool तक route करता है (function calling का काम) और arguments schema-exact होते हैं (structured outputs की गारंटी)। किसी भी production agent के लिए, समर्थित मॉडलों पर function definitions में हमेशा strict: true सेट करें।

Constrained decoding गुणवत्ता को कब नुकसान पहुँचा सकता है

Grammar-आधारित masking compute के हिसाब से लगभग मुफ़्त है, लेकिन बहुत कसी हुई schemas कभी-कभी मॉडल से ऐसी values निकलवा सकती हैं जो valid तो हैं पर गलत हैं। अगर कोई field enum तक सीमित है और कोई भी enum value संदर्भ में स्वाभाविक रूप से फिट नहीं बैठती, तो मॉडल को एक चुनना ही पड़ता है, वह "मुझे नहीं पता" नहीं कह सकता। उपाय: जहाँ लागू हो वहाँ enums में "unknown" या "other" member जोड़ें, वैकल्पिक डेटा के लिए nullable fields इस्तेमाल करें, और schemas को उतना ही ढीला रखें जितना ज़रूरी हो, फिर भी उतना सख़्त रखें जितना उपयोगी हो।

अक्सर पूछे जाने वाले प्रश्न

क्या JSON mode गारंटी देता है कि मॉडल प्रॉम्प्ट में माँगी गई keys लौटाएगा?

नहीं। JSON mode सिर्फ़ syntactically valid JSON की गारंटी देता है: संतुलित braces, quoted strings, कोई trailing comma नहीं। अगर आप प्रॉम्प्ट में name, date और amount माँगते हैं, तो मॉडल आमतौर पर इन्हें शामिल कर देगा, लेकिन वह इन्हें छोड़ सकता है, नाम बदल सकता है, या अतिरिक्त keys जोड़ सकता है। सिर्फ़ structured outputs (type: "json_schema" और strict: true के साथ) गारंटी देते हैं कि हर required key मौजूद हो और हर type सही हो।

क्या structured outputs और function calling एक साथ इस्तेमाल किए जा सकते हैं?

हाँ। अगर आप चाहते हैं कि मॉडल tools call कर सके और उसका अंतिम जवाब schema से बँधा हो, तो एक ही request में tools और response_format दोनों पास कर सकते हैं। ज़्यादा आम तरीका यह है कि function definitions में strict: true जोड़ें, ताकि tool-call arguments schema-exact हों। ये दोनों फ़ीचर एक-दूसरे के पूरक हैं, एक-दूसरे को रोकते नहीं।

क्या strict: true के साथ function calling वही गारंटी देता है जो structured outputs देते हैं?

Tool call के argument payload के लिए, हाँ। strict: true argument JSON पर वही grammar-आधारित constrained decoding चालू करता है। फ़र्क अर्थ का है: function calling action routing के बारे में है, और schema चुने गए function को पास होने वाले arguments पर लागू होता है। response_format के ज़रिए structured outputs मॉडल के अंतिम text जवाब पर लागू होते हैं।

आप 2025 या 2026 में भी JSON mode क्यों इस्तेमाल करेंगे?

JSON mode पुराने मॉडल वर्ज़न पर उपयोगी है जो structured outputs से पहले के हैं (उदाहरण के लिए gpt-4-turbo, gpt-3.5-turbo, या provider-hosted open-source मॉडल)। यह तब भी सरल है जब आपको कोई भी valid JSON चाहिए और सटीक shape की परवाह नहीं, जैसे मॉडल के सादा string जवाब लौटाने के खिलाफ़ एक त्वरित guard के रूप में। मौजूदा मॉडलों पर नए प्रोजेक्ट के लिए structured outputs को प्राथमिकता दें।

क्या Anthropic और Google structured outputs को OpenAI की तरह ही सपोर्ट करते हैं?

अवधारणा एक ही है: JSON Schema दें, grammar-आधारित decoding से schema-exact आउटपुट पाएँ। लेकिन API सतह अलग है। OpenAI response_format: {type: "json_schema", json_schema: {...}, strict: true} इस्तेमाल करता है। Anthropic (नवंबर 2025 तक) beta header के साथ output_config.format इस्तेमाल करता है। Google Gemini generation_config में response_schema field इस्तेमाल करता है। तीनों schema को grammar में compile करते हैं और decode के समय अमान्य tokens mask करते हैं।

अगर मेरी JSON Schema ऐसे फ़ीचर इस्तेमाल करती है जो provider strict mode में सपोर्ट नहीं करता, तो क्या होगा?

व्यवहार provider के हिसाब से अलग होता है। OpenAI का strict mode हर object पर additionalProperties: false और सभी properties का required में होना माँगता है। ये नियम तोड़ने वाली schema भेजने पर request के समय validation error मिलता है, generation के समय error नहीं, जो एक उपयोगी fail-fast है। अन्य असमर्थित keywords चुपचाप अनदेखे हो सकते हैं, जिससे गारंटी best-effort रह जाती है। समर्थित JSON Schema keywords की सटीक सूची के लिए हमेशा provider का structured-outputs documentation देखें।

आगे पढ़ें