Comparing function calling vs JSON mode for structured output - which is better?

Asked about 2 months agoViewed 271 times
8

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?

asked about 2 months ago

Comments

No comments yet. Be the first to comment!

Please log in to add a comment

Log In

0 Answers

Sign in to post an answer

Sign In