Comparing function calling vs JSON mode for structured output - which is better?
I need to extract structured data from unstructured text using GPT-4. I've tried both approaches:
Function Calling:
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": text}],
functions=[extract_schema],
function_call={"name": "extract_data"}
)
JSON Mode:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "Extract as JSON"},
{"role": "user", "content": text}
],
response_format={"type": "json_object"}
)
Which approach is more reliable? I've noticed function calling sometimes fails validation, but JSON mode can return unexpected schemas. What's the industry best practice?
Comments
Structured outputs (the new OpenAI feature) might be worth looking into as well. It guarantees valid JSON.
Please log in to add a comment
Log In1 Answer
I've tested both extensively. Here's my take:
Function Calling Pros:
- ✅ Built-in validation against your schema
- ✅ Better for complex nested structures
- ✅ Clearer intent to the model
Function Calling Cons:
- ❌ Can fail validation and return error
- ❌ Slightly higher latency
- ❌ More verbose API calls
JSON Mode Pros:
- ✅ More flexible (model can adapt schema)
- ✅ Lower failure rate
- ✅ Simpler API calls
JSON Mode Cons:
- ❌ No built-in validation
- ❌ Schema drift over time
- ❌ Need to handle unexpected fields
My recommendation: Use function calling for:
- Critical data extraction where validation is essential
- Well-defined schemas that won't change
- When you need strict type safety
Use JSON mode for:
- Exploratory/prototype work
- When schema might evolve
- When you have robust downstream validation
In production, I use function calling with retry logic:
try:
result = call_with_function(prompt)
except ValidationError:
result = call_with_json_mode(prompt)
validate_manually(result)
Comments
The cost comparison is really helpful. We were considering self-hosting but $2k/month + engineering time makes Azure OpenAI look much more attractive.
Please log in to add a comment
Log InSign in to post an answer
Sign In