DeepSeek V4 Pro
DeepSeek V4 Pro
Tutorial

DeepSeek API Function Calling: tools vs JSON Output

✍️ DeepSeek V4 Pro Team 📅 Sep 28, 2026 ⏱️ 8 min read 🔄 Updated Sep 28, 2026
DeepSeek API Function Calling: tools vs JSON Output
📑 Table of Contents

Introduction

Search DeepSeek API Function Calling, and many people mash two features together: hang tools for real actions, then also set response_format: json_object for “extra safety.” Officially they are separate — use Tool Calls to call out; use JSON Output when you only need a valid JSON string.

Short pick: need DB/API/state changes → tools; need structured fields for your parser → response_format. Use shipping model names deepseek-flash or deepseek-v4-pro (pricing). This site is an independent community; fields and Beta limits follow official Tool Calls / JSON Output. Echo rules with thinking: DeepSeek thinking mode.

DeepSeek API Function Calling: choose tools or JSON Output
DeepSeek API Function Calling: choose tools or JSON Output

I. Split Them First: Function Calling ≠ JSON Output

NeedUseKey params
Model proposes a function you implement; you return results into the chatTool Calls / Function Callingtools; inspect tool_calls on the response
Model emits a valid JSON text string for local json.loadsJSON Outputresponse_format={"type":"json_object"}
Strict fields and a real call-outPrefer tools + schemaShape comes from the function parameter schema; do not stack JSON Output

Official Tool Calls docs: the model does not execute functions — it only proposes calls; execution and safety checks are yours. For a support-bot skeleton that embeds tools, see Build a chatbot with the DeepSeek API — that post is the scenario frame; this one covers protocol and selection.

II. Minimal Function Calling Loop (deepseek-flash)

base_url stays https://api.deepseek.com. Four steps: user message → model returns tool_calls → you run the function → reply with role: tool → request again for the natural-language answer.

Minimal example:

from openai import OpenAI

client = OpenAI(api_key="YOUR_KEY", base_url="https://api.deepseek.com")
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get weather of a location.",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "City, e.g. Hangzhou"},
            },
            "required": ["location"],
        },
    },
}]

messages = [{"role": "user", "content": "How's the weather in Hangzhou?"}]
msg = client.chat.completions.create(
    model="deepseek-flash", messages=messages, tools=tools,
).choices[0].message
messages.append(msg)

if msg.tool_calls:
    call = msg.tool_calls[0]
    messages.append({
        "role": "tool",
        "tool_call_id": call.id,
        "content": "24C, cloudy",
    })
    final = client.chat.completions.create(
        model="deepseek-flash", messages=messages, tools=tools,
    )
    print(final.choices[0].message.content)

Must do:

  1. try/except parse tool.function.arguments, then map to a allow-listed function.
  2. With thinking on and tools present, historical reasoning_content must be echoed or you get HTTP 400 — appending the whole message is safest.
  3. Chat Completions cannot insert tool calls that the model did not generate mid-thread; use the Anthropic-compatible endpoint or Responses API when you must inject history.

III. strict Mode (Beta): Turn On When Schema Must Hold

When the server must enforce argument shape, set strict: true:

  1. base_url="https://api.deepseek.com/beta"
  2. Every function in the list sets "strict": true
  3. Every object: all properties in required, and additionalProperties: false

Supported types include object / string / number / integer / boolean / array / enum / anyOf; string may use pattern and format (email, uuid, …) but not minLength / maxLength; array not minItems / maxItems. Invalid schemas error before the model runs — useful at deploy time.

Default production traffic still uses https://api.deepseek.com; do not blindly route everything to /beta.

IV. JSON Output: The Prompt Must Contain json

When you only need structured text and no call-out:

  1. response_format={"type": "json_object"}
  2. system or user prompt must include the word json, plus an example of the expected fields
  3. Set max_tokens sanely so finish_reason=length does not truncate mid-JSON
  4. Official docs note occasional empty content — retry with a tweaked prompt; check finish_reason before parsing

Minimal example:

response = client.chat.completions.create(
    model="deepseek-flash",
    messages=[
        {"role": "system", "content": "Extract fields as JSON. EXAMPLE JSON OUTPUT: {\"name\":\"A\",\"age\":1}"},
        {"role": "user", "content": "Alice is 30 years old."},
    ],
    response_format={"type": "json_object"},
    max_tokens=256,
)
print(response.choices[0].message.content)
DeepSeek API tools vs json_object: call-out vs structured text
DeepSeek API tools vs json_object: call-out vs structured text

V. Selection Checklist and Common Traps

SceneTip
Order lookup / tickets / internal APIsFunction Calling
Extract fields, report rows, fixed key/value returnsJSON Output
Long agent chains + thinkingtools + thinking echo rules
Server-enforced schemaBeta strict
429 / concurrencyDeepSeek API 429
Bill controlPeak hours

Don’t:

  • Enable tools and JSON Output together as “double constraints” — structure belongs in the tool schema.
  • eval or SQL-concatenate unchecked arguments.
  • Keep expired model names; new code writes deepseek-flash / deepseek-v4-pro. Overview: DeepSeek API.

FAQ

How many functions can Function Calling take?
The upgrade note mentioned up to 128 per call with parallel calls; re-check the live Tool Calls docs and regression-test your tool list before shipping.

Is JSON Output the same as JSON inside tool arguments?
No. The former constrains the assistant’s final content; the latter is the tool_calls[].function.arguments string — still validate locally.

Does the web app expose Function Calling?
This article covers the DeepSeek API. Web chat: DeepSeek web; protocol capabilities follow the API docs.

Summary

Shortest path for DeepSeek API Function Calling: call-outs → tools loop with validated args; JSON only → response_format + prompt contains json; hard schema → Beta strict. Open official Tool Calls / JSON Output pages first; do not skip thinking + tools echo rules.

Further reading

Share this article:
D

DeepSeek V4 Pro Team

DeepSeek V4 Pro technical team

✨

Ready to experience DeepSeek V4?

Start chatting now and feel the power of 1M-token context.

🚀 Start Chatting

Free · No sign-up required

🧭 In this series

Explore related guides and hub pages in this topic cluster.

Category hub

Tutorial →

Related resources

📚 Recommended Reading