DeepSeek-v4-flash-vision-exp:支持多格式图片输入的视觉模型
deepseek-v4-flash-vision-exp 模型支持图文输入,你可以让模型描述图片、读取截图文字、分析图表等。
支持的图片格式:**JPEG、PNG、GIF 和 WebP**。格式会根据文件实际内容检测,而非文件名或声明的 MIME 类型。
发送图片
向模型提供图片有三种方式。它们都使用标准的 OpenAI 兼容 Chat Completions 格式,其中 content 是一个块数组而非普通字符串。在 Responses API 中也提供了这三种方法,图片通过 input_image 内容部分传递。
示例中的 base_url 为 https://api.deepseek.com。
1. Base64 编码图片(内联)
将图片编码并直接作为 data: URL 嵌入请求中。这是处理本地文件最简单的方式。编码后的数据计入48 MiB 的请求体限制(详见 Limits)。
import base64
from openai import OpenAI
client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
with open("image.jpg", "rb") as f:
b64 = base64.b64encode(f.read()).decode("utf-8")
response = client.chat.completions.create(
model="deepseek-v4-flash-vision-exp",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{b64}"},
},
],
}
],
)
print(response.choices[0].message.content)
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-v4-flash-vision-exp",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,<BASE64_DATA>"}}
]
}
]
}'
2. 外部图片 URL
提供公开可访问的 http(s) 链接,模型会自动下载图片。URL 长度不得超过 8192 个字符,图片文件大小不得超过 32 MiB,且必须在 60 秒 内完成下载。如果链接过长,请改用 base64 data URL 或 Files API。
response = client.chat.completions.create(
model="deepseek-v4-flash-vision-exp",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"},
},
],
}
],
)
print(response.choices[0].message.content)
3. 引用 Files API 上传的文件
使用 Files API 上传一次图片,然后在请求中引用其 file_id。当需要在多次请求中复用同一张图片,或图片体积超过 48 MiB 的内联限制时,这是最佳选择。与内联图片不同,通过 Files API 引用的 file_id 图片大小可达 64 MiB,且不受单张图片 32 MiB 的限制。
使用带有返回的 file_id(格式为 file-api-...)的 file 内容块:
response = client.chat.completions.create(
model="deepseek-v4-flash-vision-exp",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{"type": "file", "file_id": "file-api-xxxxxxxxxxxxxxxx"},
],
}
],
)
print(response.choices[0].message.content)
或者,file 块也可以通过 file_data 而非 file_id(两者互斥)将图片以 base64 形式内联携带:
{
"type": "file",
"file_data": "data:image/jpeg;base64,<BASE64_DATA>",
"filename": "image.jpg"
}
详细程度
对于 image_url 输入,你可以设置 detail 字段来控制图像的处理方式:
| 值 | 行为 |
|---|---|
low | 推理前将图像缩小至 512×512。当不需要精细视觉细节时,速度更快且更便宜。 |
high | 保持原始图像。(为了兼容性提供;等同于 original。) |
original | 保持原始图像。 |
auto | 自动选择。目前等同于 original。 |
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg", "detail": "low"}
}
何时使用 Files API
内联图像(base64 或 file_data)计入请求体大小限制 48 MiB。当满足以下条件时,请考虑使用 Files API:
- 单次请求将超出体大小限制。
- 图像大于 32 MiB(这只能通过 Files API 实现)。
- 在多个请求中引用同一张图像,且希望避免每次都重新上传。
Token 使用量
图像根据其尺寸转换为 tokens,这些 tokens 与文本 tokens 一起计费。
推理前,每张图像都会自动调整大小:
- 总像素数低于约 384×384 的图像会按比例放大。
- 较大的图像会按比例缩小,使调整大小后的总像素数大致相当于一张 800×800 图像。
因此,每张图像的 token 上限为 384:例如,一张 2000×2000 的图像和一张 5000×5000 的图像在调整大小后消耗的 token 数相同。当请求包含多张图像时,每张图像都遵循同一规则独立计算——多图像请求没有单独的计算方式。
要估算特定尺寸图片的 Token 成本,请使用 Token & Token Usage 页面上的图片 Token 计算器。Limits
| 限制 | 数值 |
|---|---|
| 支持的格式 | JPEG, PNG, GIF, WebP |
| 外部 URL 长度 | 8192 个字符 |
| 请求体大小 | 48 MiB |
| 单张图片最大尺寸(base64 / 外部 URL) | 32 MiB |
单张图片最大尺寸(Files API file_id) | 64 MiB |
| 单次请求最大图片数量 | 600 |
| 单次请求最大图片总大小 | 不含 file_id 图片时为 64 MiB;包含 file_id 图片时最高可达 200 MiB |
| 图片最大尺寸 | 单边 8192 px;当请求包含 15 张或更多图片时,单边降至 4096 px |
关于通过 Files API 上传文件的存储和上传配额,请参阅 Files API: Limits。
Restrictions
- 仅支持在
user消息中发送图片:在system或assistant消息中发送图片会返回400错误。 - 仅视觉模型(
deepseek-v4-flash-vision-exp)支持图片;其他模型会返回400错误("This model does not support image")。 - 包含保留图片占位符 Token 的用户文本会被拒绝,并返回
400错误。
Using Images with the Anthropic API
除了上述 OpenAI 兼容端点外,您还可以通过 Anthropic 兼容的 /messages 端点(base_url = https://api.deepseek.com/anthropic)发送图片。关于通用设置,请参阅 Anthropic API。
区别在于图片内容块的形状。Anthropic 使用 image 块,其中包含一个 source 对象,其 type 为 base64、url 或 file 之一:
import anthropic
client = anthropic.Anthropic() # ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic
message = client.messages.create(
model="deepseek-v4-flash-vision-exp",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What is in this image?"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "<BASE64_DATA>",
},
},
],
}
],
)
print(message.content)
这三种 source 变体对应了上述的 OpenAI 方法:
source.type | Equivalent OpenAI method | Notes |
|---|---|---|
base64 | Base64-encoded image | Requires a media_type field (image/jpeg, image/png, image/gif, or image/webp). |
source.type | Equivalent OpenAI method | Notes |
|---|---|---|
url | External image URL | Max 8192 characters. |
source.type | Equivalent OpenAI method | Notes |
|---|---|---|
file | Files API file_id | Requires the header anthropic-beta: files-api-2025-04-14. |
Using Images with the Responses API
The deepseek-v4-flash-vision-exp model also accepts images through the OpenAI-compatible Responses API. The same three input methods (base64 data URL, external http(s) URL, Files API file_id) and the same limits apply; only the content part shape differs — images are carried in input_image parts, either in user / developer messages or in the output of function_call_output / custom_tool_call_output items:
response = client.responses.create(
model="deepseek-v4-flash-vision-exp",
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": "What is in this image?"},
`input_image` 部分支持 `detail` 字段,语义与上文一致(`low` / `high` / `original` / `auto`)。通过 `file_id` 提供图片时会忽略 `detail`,且 `image_url` 和 `file_id` 互斥。
有关字段语义、限制(`system` / `assistant` 消息中的图片会被拒绝并返回 `400` 错误)以及工具输出图片,请参阅 Responses API 指南。