DeepSeekDSH
Independent community guideNot affiliated with DeepSeek.Download versions and guide references

DeepSeek API Function Calling and Structured Output Guide

DeepSeek-V3 natively supports standard OpenAI tool calling schemas and JSON mode. Use these code templates to execute function calls in automated agents.

On this page

1. Model capability and support

Use deepseek-chat for function and tool execution tasks. DeepSeek-Chat supports native OpenAI-format tool calling schemas. deepseek-reasoner is specialized for pure reasoning and does not support tool calling.

2. Standard 4-step tool calling workflow

  1. Define tool schemas: Provide the tools array containing function names, descriptions, and JSON Schema parameters.
  2. Model decision: The API returns tool_calls with parsed arguments.
  3. Execute locally: Your application invokes the corresponding local function with the provided arguments.
  4. Return result: Append the tool output (with role: "tool") to messages and request the final synthesized answer.

3. Complete executable Python example

import json from openai import OpenAI client = OpenAI( api_key="sk-your_deepseek_api_key", base_url="https://api.deepseek.com" ) # 1. Local implementation def get_weather(location: str, unit: str = "celsius"): return json.dumps({"location": location, "temperature": 22, "condition": "Sunny", "unit": unit}) # 2. Tool schema tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a specified location", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "City name, e.g. London"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["location"] } } }] messages = [{"role": "user", "content": "What is the weather in London today?"}] # 3. Initial request triggering tool call response = client.chat.completions.create( model="deepseek-chat", messages=messages, tools=tools ) message = response.choices[0].message if message.tool_calls: messages.append(message) for tool_call in message.tool_calls: if tool_call.function.name == "get_weather": args = json.loads(tool_call.function.arguments) tool_result = get_weather(**args) # 4. Append tool execution output messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": tool_result }) # 5. Second request producing final answer final_response = client.chat.completions.create( model="deepseek-chat", messages=messages ) print("Final answer:", final_response.choices[0].message.content)

4. Structured JSON Object output mode

When you only require strict JSON formatted output without external tools, set response_format:

response = client.chat.completions.create( model="deepseek-chat", messages=[ {"role": "system", "content": "You are a data extraction assistant. Always output valid JSON."}, {"role": "user", "content": "Extract data: John Doe, 29, Software Engineer in Seattle"} ], response_format={"type": "json_object"} ) print(response.choices[0].message.content)

5. Troubleshooting and retry tips

JSON decode error when parsing arguments

Wrap json.loads() in a try...except block. If arguments are malformed, append the error message back to the conversation for auto-repair.

Model fails to trigger tool execution

Ensure the function description clearly matches the user prompt intent, or pass tool_choice="auto" explicitly.

References