For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主要導覽

檔案輸入

瞭解如何在 OpenAI API 中使用檔案作為輸入。

檔案輸入的支援範圍取決於 API 端點。Responses API 接受下列檔案類型作為 input_file 項目。Chat Completions 則僅接受 PDF 檔案作為 file 內容部分。

輸入方式Responses APIChat Completions
Base64 編碼的檔案資料(file_data下列支援的檔案類型僅限 PDF
已上傳檔案的 ID(file_id下列支援的檔案類型僅限 PDF
外部檔案 URL(file_url下列支援的檔案類型不支援

若要輸入非 PDF 檔案,請使用 Responses API。若要在 Chat Completions 中使用檔案中的文字,請在應用程式中讀取檔案,並將其內容作為 text 內容部分傳送。

運作方式

在 Responses API 中,input_file 的處理方式取決於檔案類型:

  • PDF 檔案:使用具備視覺能力的模型(例如 gpt-4o 及後續模型)時,API 會同時擷取文字和頁面影像,並將兩者傳送給模型。
  • 非 PDF 格式的文件與文字檔案 (例如 .docx.pptx.txt 和程式碼檔案):API 僅擷取文字。
  • 試算表檔案 (例如 .xlsx.csv.tsv):API 會執行試算表專用的增強處理流程(詳見下文)。

如果以下相關工具更適合你的任務,請使用這些工具:

  • 使用檔案搜尋檢索大型檔案,而非直接以 input_file 傳入整份檔案。
  • 對於以試算表為主且需要詳細分析的任務,例如彙總、聯結、繪製圖表或自訂計算,請使用託管 Shell 環境

非 PDF 檔案的影像與圖表限制

對於非 PDF 檔案,Responses API 不會擷取內嵌的影像或圖表 作為模型的上下文。

若要保留圖表與示意圖的細節,請先將檔案轉換為 PDF,再 以 input_file 傳送 PDF。

試算表增強處理的運作方式

對於試算表類型的檔案(例如 .xlsx.xls.csv.tsv.iif),Responses API 會使用試算表專用的增強處理流程。

API 不會將整張工作表傳給模型,而是最多解析每張工作表的前 1,000 列,並加入模型產生的摘要與標頭中繼資料,讓 模型能以較精簡的結構化資料檢視進行處理。

PDF 細節層級

在 Responses API 中輸入 PDF 時,可以將 input_file 項目的選用欄位 detail 設為 autolowhigh,以控制 API 處理 頁面影像的方式。若省略此欄位,detail 預設為 auto。對於 GPT-5.6 及後續 模型,auto 會採用 high;較早的模型則採用 low。使用 low 可減少 輸入 Token 用量;使用 high 則可保留更多視覺細節,例如密集的圖表、小字 或示意圖。

detail 設定僅影響 PDF 頁面影像的處理方式。從 PDF 擷取的文字 仍會納入。Chat Completions 的檔案輸入不支援 detail

以下是明確指定高細節層級的最簡 Responses API 請求主體範例:

{
  "model": "gpt-4.1",
  "input": [
    {
      "role": "user",
      "content": [
        {
          "type": "input_file",
          "filename": "document.pdf",
          "file_data": "data:application/pdf;base64,...",
          "detail": "high"
        },
        {
          "type": "input_text",
          "text": "Summarize this document."
        }
      ]
    }
  ]
}

支援的檔案類型

下表列出 Responses API 可接受作為 input_file 項目的常見檔案類型。完整的副檔名與 MIME 類型清單列於 本頁後段。Chat Completions 的 file_datafile_id 都僅支援 .pdfapplication/pdf)。

類別常見副檔名
PDF 檔案.pdf
文字與程式碼.txt.md.json.html.xml、程式碼檔案
豐富格式文件.doc.docx.rtf.odt
簡報.ppt.pptx
試算表.csv.xls.xlsx

檔案 URL

你可以透過外部 URL 連結提供檔案輸入。

使用外部檔案 URL
curl "https://api.openai.com/v1/responses" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d '{
        "model": "gpt-6-astra",
        "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"
                    }
                ]
            }
        ]
    }'

上傳檔案

以下範例使用 Files API 上傳檔案,再於傳送給模型的請求中參照該檔案的 ID。

上傳檔案
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-6-astra",
        "input": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "input_file",
                        "file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
                    },
                    {
                        "type": "input_text",
                        "text": "What is the first dragon in the book?"
                    }
                ]
            }
        ]
    }'

Base64 編碼的檔案

你也可以使用 Base64 編碼的檔案資料傳送檔案輸入。

傳送 Base64 編碼的檔案
curl "https://api.openai.com/v1/responses" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d '{
        "model": "gpt-6-astra",
        "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?"
                    }
                ]
            }
        ]
    }'

使用注意事項

使用檔案輸入時,請留意以下限制:

  • Token 用量: 解析 PDF 時,擷取的文字和頁面影像都會納入上下文,因此可能增加 Token 用量。在 Responses API 中,將 detail 設為 auto(預設值)、lowhigh,可控制 PDF 頁面影像的視覺細節量。大規模部署前,請先瞭解定價及對 Token 用量的影響。瞭解定價詳情
  • 檔案大小限制: 單一請求可以包含多個檔案,但每個檔案都必須小於 50 MB。請求中所有檔案的總大小上限為 50 MB。
  • 支援的模型: 若要在解析 PDF 時同時處理文字和頁面影像,需要使用具備視覺能力的模型,例如 gpt-4o 及後續模型。
  • 檔案上傳用途: 你可以指定任何支援的用途來上傳檔案,但對於打算作為模型輸入的檔案,請使用 user_data

支援的檔案類型完整清單

此清單適用於 Responses API。Chat Completions 的 file_datafile_id 都僅支援 .pdfapplication/pdf)。

類別副檔名MIME 類型
PDF 檔案PDF 檔案(.pdfapplication/pdf
試算表Excel 工作表(.xla, .xlb, .xlc, .xlm, .xls, .xlsx, .xlt, .xlwapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel
試算表CSV / TSV / IIF(.csv, .tsv, .iif)、Google Sheetstext/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 Docsapplication/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 Slidesapplication/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, .xmlapplication/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

後續步驟

接下來,你可以參考以下資源: