How to beginner · 3 min read

How to use DeepSeek function calling

Quick answer
Use the OpenAI Python SDK with base_url="https://api.deepseek.com" to call DeepSeek's deepseek-chat model. Pass your function definitions in the functions parameter and handle the function_call response to execute or process the function output.

PREREQUISITES

  • Python 3.8+
  • DeepSeek API key
  • pip install openai>=1.0

Setup

Install the openai Python package and set your DeepSeek API key as an environment variable. Use the OpenAI client with the base_url set to DeepSeek's API endpoint.

bash
pip install openai>=1.0

Step by step

This example demonstrates how to call DeepSeek's deepseek-chat model with function calling enabled. Define your functions, send them in the request, and handle the function call response.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"], base_url="https://api.deepseek.com")

functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location"]
        }
    }
]

messages = [
    {"role": "user", "content": "What's the weather like in New York?"}
]

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=messages,
    functions=functions,
    function_call="auto"
)

choice = response.choices[0]
message = choice.message

if message.get("function_call"):
    function_name = message["function_call"]["name"]
    function_args = message["function_call"]["arguments"]
    print(f"Function to call: {function_name}")
    print(f"Arguments: {function_args}")
else:
    print(message.content)
output
Function to call: get_current_weather
Arguments: {"location": "New York"}

Common variations

You can specify function_call as "none" to disable function calling or provide a specific function name to force calling. Use different DeepSeek models like deepseek-reasoner by changing the model parameter. Async calls require custom HTTP clients as the OpenAI SDK does not natively support async for DeepSeek.

Troubleshooting

  • If you get authentication errors, verify your DEEPSEEK_API_KEY environment variable is set correctly.
  • If function calls are not triggered, ensure your functions parameter is correctly formatted as a list of JSON schema objects.
  • Check network connectivity to https://api.deepseek.com if requests time out.

Key Takeaways

  • Use the OpenAI SDK with base_url="https://api.deepseek.com" to access DeepSeek APIs.
  • Pass function definitions in the functions parameter to enable function calling.
  • Handle the function_call field in the response to execute or process functions.
  • Specify function_call="auto" to let the model decide when to call functions.
  • Verify environment variables and JSON schema formatting to avoid common errors.
Verified 2026-04 · deepseek-chat, deepseek-reasoner
Verify ↗