OpenAI 模型可以接受文件作为 input_file 项。在 Responses API 中,您可以将文件作为 Base64 编码的数据、由 Files API 返回的文件 ID(/v1/files)或外部 URL 发送。
工作原理
input_file 处理方式取决于文件类型:
- PDF 文件: 在具有视觉能力的模型上,例如
gpt-4o及更高版本模型,API 会同时提取文本和页面图像,并将两者一并发送给模型。 - 非 PDF 文档和文本文件 (例如,
.docx,.pptx,.txt, 和代码文件): API 仅提取文本。 - 电子表格文件 (例如,
.xlsx,.csv,.tsv):API 会运行专用的电子表格增强流程(如下所述)。
当以下相关工具更适合您的任务时,请使用它们:
非 PDF 图像和图表限制
对于非 PDF 文件,API 不会将嵌入的图像或图表提取到模型上下文中。
要保持图表和图示的精确度,请先将文件转换为 PDF,然后将该 PDF 作为 input_file.
电子表格增强的工作原理
对于类似电子表格的文件(例如 .xlsx, .xls, .csv, .tsv,且
.iif), input_file 使用专用的电子表格增强过程。
API 不会将整个工作表传递给模型,而是解析每个工作表的前 1,000 行,并添加由模型生成的摘要和表头元数据,以便模型能够基于更小、结构化的数据视图进行处理。
接受的文件类型
下表列出了在 input_file。扩展名和 MIME 类型的完整列表将在本页稍后显示。
| 类别 | 常见扩展名 |
|---|---|
| PDF 文件 | .pdf |
| 文本和代码 | .txt, .md, .json, .html, .xml、代码文件 |
| 富文本文档 | .doc, .docx, .rtf, .odt |
| 演示文稿 | .ppt, .pptx |
| 电子表格 | .csv, .xls, .xlsx |
文件 URL
你可以通过链接外部 URL 来提供文件输入。
使用外部文件 URL
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze the letter and provide a summary of the key points."
},
{
"type": "input_file",
"file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"
}
]
}
]
}'Chat Completions 不支持文件 URL。请使用 Responses API for this option.
上传文件
以下示例使用 Files API,然后在向模型发送的请求中引用其文件 ID。
上传文件
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="user_data" \
-F file="@draconomicon.pdf"
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
},
{
"type": "input_text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'上传文件
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="user_data" \
-F file="@draconomicon.pdf"
curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{
"type": "file",
"file": {
"file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
}
},
{
"type": "text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'Base64 编码的文件
你也可以将文件输入作为 Base64 编码的文件数据发送。
发送 Base64 编码的文件
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"filename": "draconomicon.pdf",
"file_data": "...base64 encoded PDF bytes here..."
},
{
"type": "input_text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'发送 Base64 编码的文件
curl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5",
"messages": [
{
"role": "user",
"content": [
{
"type": "file",
"file": {
"filename": "draconomicon.pdf",
"file_data": "...base64 encoded bytes here..."
}
},
{
"type": "text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'使用注意事项
使用文件输入时,请牢记以下限制:
- Token 用量: PDF 解析会在上下文中同时包含提取的文本和页面图像,这可能会增加 Token 用量。在大规模部署之前,请查看定价和 Token 影响。 了解更多定价.
- 文件大小限制: 单个请求可以包含多个文件,但每个文件不得超过 50 MB。请求中所有文件的合并大小上限为 50 MB。
- 支持的模型: 包含文本和页面图像的 PDF 解析需要具备视觉能力的模型,例如
gpt-4oand later models. - 文件上传用途: 你可以为计划作为模型输入的文件上传任何支持的 用途,但请使用
user_data针对您计划作为模型输入传递的文件。
已接受文件类型的完整列表
| 类别 | 扩展名 | MIME 类型 |
|---|---|---|
| PDF 文件 | PDF 文件 (.pdf) | application/pdf |
| 电子表格 | Excel 表格 (.xla, .xlb, .xlc, .xlm, .xls, .xlsx, .xlt, .xlw) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel |
| 电子表格 | CSV / TSV / IIF (.csv, .tsv, .iif)、Google Sheets | text/csv, application/csv, text/tsv, text/x-iif, application/x-iif, application/vnd.google-apps.spreadsheet |
| 富文本文档 | Word/ODT/RTF 文档 (.doc, .docx, .dot, .odt, .rtf)、Pages、Google Docs | application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/msword, application/rtf, text/rtf, application/vnd.oasis.opendocument.text, application/vnd.apple.pages, application/vnd.google-apps.document, application/vnd.apple.iwork |
| 演示文稿 | PowerPoint 幻灯片 (.pot, .ppa, .pps, .ppt, .pptx, .pwz, .wiz)、Keynote、Google Slides | application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.ms-powerpoint, application/vnd.apple.keynote, application/vnd.google-apps.presentation, application/vnd.apple.iwork |
| 文本和代码 | 文本/代码格式 (.asm, .bat, .c, .cc, .conf, .cpp, .css, .cxx, .def, .dic, .eml, .h, .hh, .htm, .html, .ics, .ifb, .in, .js, .json, .ksh, .list, .log, .markdown, .md, .mht, .mhtml, .mime, .mjs, .nws, .pl, .py, .rst, .s, .sql, .srt, .text, .txt, .vcf, .vtt, .xml) | application/javascript, application/typescript, text/xml, text/x-shellscript, text/x-rst, text/x-makefile, text/x-lisp, text/x-asm, text/vbscript, text/css, message/rfc822, application/x-sql, application/x-scala, application/x-rust, application/x-powershell, text/x-diff, text/x-patch, application/x-patch, text/plain, text/markdown, text/x-java, text/x-script.python, text/x-python, text/x-c, text/x-c++, text/x-golang, text/html, text/x-php, application/x-php, application/x-httpd-php, application/x-httpd-php-source, text/x-ruby, text/x-sh, text/x-bash, application/x-bash, text/x-zsh, text/x-tex, text/x-csharp, application/json, text/x-typescript, text/javascript, text/x-go, text/x-rust, text/x-scala, text/x-kotlin, text/x-swift, text/x-lua, text/x-r, text/x-R, text/x-julia, text/x-perl, text/x-objectivec, text/x-objectivec++, text/x-erlang, text/x-elixir, text/x-haskell, text/x-clojure, text/x-groovy, text/x-dart, text/x-awk, application/x-awk, text/jsx, text/tsx, text/x-handlebars, text/x-mustache, text/x-ejs, text/x-jinja2, text/x-liquid, text/x-erb, text/x-twig, text/x-pug, text/x-jade, text/x-tmpl, text/x-cmake, text/x-dockerfile, text/x-gradle, text/x-ini, text/x-properties, text/x-protobuf, application/x-protobuf, text/x-sql, text/x-sass, text/x-scss, text/x-less, text/x-hcl, text/x-terraform, application/x-terraform, text/x-toml, application/x-toml, application/graphql, application/x-graphql, text/x-graphql, application/x-ndjson, application/json5, application/x-json5, text/x-yaml, application/toml, application/x-yaml, application/yaml, text/x-astro, text/srt, application/x-subrip, text/x-subrip, text/vtt, text/x-vcard, text/calendar |
后续步骤
接下来,您可能想要探索以下资源之一: