> ## Documentation Index
> Fetch the complete documentation index at: https://docs.heihuzi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# DeepSeek Chat Completions

> 已实测的 JSON、普通文本和流式调用。

调用 `POST https://code.heihuzi.ai/v1/chat/completions`。实测日期：**2026-09-19**。先准备[DeepSeek Key 和 SDK](/cn/api-reference/deepseek/index)。

## 请求参数

<ParamField body="model" type="string" required>
  填写 `deepseek-flash`。本页普通文本、JSON、流式、工具和图片示例均使用此模型。
</ParamField>

<ParamField body="messages" type="object[]" required>
  消息历史，使用 `role` 和 `content`。工具结果还需 `tool_call_id`；图片输入使用内容数组，见对应专题。
</ParamField>

<ParamField body="max_tokens" type="integer">
  本页成功示例使用 `1024`。另外使用 `1024` 请求长输出，实测输出在上限内结束，`finish_reason` 为 `length`；客户端应将它识别为截断。
</ParamField>

<ParamField body="response_format" type="object">
  本次验证 `{"type":"json_object"}`，并对返回内容执行 JSON 解析和字段断言。
</ParamField>

<ParamField body="stream" type="boolean">
  `true` 返回 SSE；完整示例核对最终 `finish_reason`。`stream_options.include_usage: true` 的测试收到了用量。
</ParamField>

## 非流式 JSON 示例

此完整示例已通过便利店执行，返回对象为 `{"name":"Heihuzi","count":4}`。提示词明确要求 JSON，客户端仍需检查终止原因并解析内容。

<RequestExample>
  ```python theme={null}
  import os
  from openai import OpenAI
  client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
  import json
  result = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'Return only a JSON object with name="Heihuzi" and count=4.'}], response_format={'type': 'json_object'}, max_tokens=1024)
  assert result.choices[0].finish_reason == 'stop'
  value = json.loads(result.choices[0].message.content)
  assert value == {'name': 'Heihuzi', 'count': 4}
  print(value)
  ```
</RequestExample>

## 流式文本与思考内容

本次收到答案 `42`、非空 `reasoning_content`、`finish_reason: "stop"` 和用量，原始事件流以 `data: [DONE]` 结束。最后的用量块可能没有 choices，代码按数组遍历处理。

```python theme={null}
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['HEIHUZI_API_KEY'], base_url='https://code.heihuzi.ai/v1', timeout=120.0, max_retries=0)
events = client.chat.completions.create(model='deepseek-flash', messages=[{'role': 'user', 'content': 'What is 19 + 23? Reply with the number only.'}], max_tokens=1024, stream=True, stream_options={'include_usage': True})
(answer, reasoning) = ('', '')
(finish_reason, usage) = (None, None)
for event in events:
    if event.usage:
        usage = event.usage
    for choice in event.choices:
        answer += choice.delta.content or ''
        reasoning += getattr(choice.delta, 'reasoning_content', None) or ''
        finish_reason = choice.finish_reason or finish_reason
print(answer)
print('finish_reason', finish_reason, 'reasoning_chars', len(reasoning))
print('usage', usage)
assert answer.strip() == '42' and finish_reason == 'stop'
assert reasoning, 'No reasoning_content returned'
```

## 响应与限制

正文在 `choices[].message.content`，思考内容可能在同级 `reasoning_content`；流式读取对应 `delta`。本次用量返回 `prompt_tokens`、`completion_tokens`、`total_tokens`，没有返回完整缓存及推理细分字段。

需要工具时见[完整工具闭环](/cn/api-reference/deepseek/tools)，需要图片时见[图片理解](/cn/api-reference/deepseek/vision)。本页只列出通过实测的请求配置，见[实测范围](/cn/api-reference/deepseek/compatibility)。

格式参考：[DeepSeek Chat Completions](https://api-docs.deepseek.com/zh-cn/api/create-chat-completion)、[JSON Output](https://api-docs.deepseek.com/zh-cn/guides/json_mode/)。本页承诺范围以便利店实测为准。
