Tool calling

Tool calling

Describe your functions in tools. The model replies with tool_calls; you run them and send the results back.

A tool call round trip

Python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "What's the weather in Lisbon?"}]
response = client.chat.completions.create(
    model="deepseek-v4.1-flash-1017-uncensored", messages=messages, tools=tools
)
message = response.choices[0].message

# Send the assistant message back unchanged, then one result per call.
messages.append(message)
for call in message.tool_calls:
    messages.append({
        "role": "tool",
        "tool_call_id": call.id,
        "content": '{"temp_c": 21, "sky": "clear"}',
    })

final = client.chat.completions.create(
    model="deepseek-v4.1-flash-1017-uncensored", messages=messages, tools=tools
)
print(final.choices[0].message.content)

Send the assistant message back as it came, including reasoning_content. The model needs its own thinking to continue after a tool result.

Anthropic and Responses

Tool use works the same way on /v1/messages (tool_use and tool_result blocks) and on /responses (function calls). Hosted tools such as web search are not available.