ファイル入力の対応範囲は、API エンドポイントによって異なります。Responses API は、以下の形式のファイルを input_file 項目として受け付けます。Chat Completions が file コンテンツパートとして受け付けるのは PDF ファイルのみです。
| 入力方法 |
Responses API |
Chat 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 として直接渡す必要はありません。
- スプレッドシートを中心に、集計、結合、グラフ作成、独自の計算などの詳細な分析を行う場合は、ホスト型シェルを使用してください。
PDF 以外のファイルでは、Responses API は埋め込まれた画像やグラフをモデルのコンテキストに取り込みません。
グラフや図を元の表示どおりにモデルに渡すには、まずファイルを PDF に変換し、その PDF を input_file として送信してください。
表形式のファイル(.xlsx、.xls、.csv、.tsv、.iif など)に対して、Responses API はスプレッドシート専用の処理で補足情報を追加します。
API はシート全体をモデルに渡す代わりに、各シートの先頭から最大 1,000 行を解析し、モデルが生成した要約とヘッダーのメタデータを追加します。これにより、モデルは元のシートよりもコンパクトで構造化されたデータを扱えます。
Responses API に PDF を入力する場合は、オプションの detail フィールドを input_file 項目で auto、low、high のいずれかに設定すると、API が各ページの画像を処理する際の詳細度を指定できます。detail を省略すると、デフォルトの auto が適用されます。GPT-5.6 以降のモデルでは auto は high に、それ以前のモデルでは low に相当します。入力トークン数を抑えるには low を、情報量の多いグラフ、小さな文字、図などの細部まで読み取るには high を使用してください。
detail の設定が影響するのは PDF のページ画像の処理のみです。PDF から抽出したテキストは設定にかかわらず入力に含まれます。Chat Completions のファイル入力は detail に対応していません。
以下は、高い詳細度を明示的に指定した Responses API の最小限のリクエストボディです。
1234567891011121314151617181920{
"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 で対応している形式は .pdf(application/pdf)のみで、この制限は file_data と file_id の両方に適用されます。
| カテゴリ |
代表的な拡張子 |
| PDF ファイル |
.pdf |
| テキストとコード |
.txt、.md、.json、.html、.xml、コードファイル |
| 書式付き文書 |
.doc、.docx、.rtf、.odt |
| プレゼンテーション |
.ppt、.pptx |
| スプレッドシート |
.csv、.xls、.xlsx |
外部ファイルの URL を指定して、ファイルを入力として渡せます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
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",
},
],
},
],
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24from openai import OpenAI
client = OpenAI()
response = client.responses.create(
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",
},
],
},
],
)
print(response.output_text)
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
29
30
31
32
33
34
35
36
37
38
39
40
41package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{
responses.ResponseInputContentParamOfInputText(
"Analyze the letter and provide a summary of the key points.",
),
{
OfInputFile: &responses.ResponseInputFileParam{
FileURL: openai.String(
"https://www.berkshirehathaway.com/letters/2024ltr.pdf",
),
},
},
},
responses.EasyInputMessageRoleUser,
),
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
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
29
30import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputFile;
import com.openai.models.responses.ResponseInputItem;
import java.util.List;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addInputTextContent(
"Analyze the letter and provide a summary of the key points.")
.addContent(
ResponseInputFile.builder()
.fileUrl(
"https://www.berkshirehathaway.com/letters/2024ltr.pdf")
.build())
.build())))
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
Uri fileUrl = new(
"https://www.berkshirehathaway.com/letters/2024ltr.pdf"
);
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
[
ResponseItem.CreateUserMessageItem(
[
ResponseContentPart.CreateInputTextPart(
"Analyze the letter and provide a summary of the key points."
),
ResponseContentPart.CreateInputFilePart(fileUrl),
]
),
]
);
Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
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"
}
]
}
]
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21curl "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"
}
]
}
]
}'
Chat Completions はファイル URL に対応していません。ファイル URL を使用する場合は、Responses API を使用してください。
次の例では、Files API でファイルをアップロードし、モデルへのリクエストでそのファイルの ID を指定します。
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
29import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const file = await client.files.create({
file: fs.createReadStream("fixtures/draconomicon.pdf"),
purpose: "user_data",
});
const response = await client.responses.create({
model: "gpt-6-astra",
input: [
{
role: "user",
content: [
{
type: "input_file",
file_id: file.id,
},
{
type: "input_text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(response.output_text);
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
26from openai import OpenAI
client = OpenAI()
file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")
response = client.responses.create(
model="gpt-6-astra",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": file.id,
},
{
"type": "input_text",
"text": "What is the first dragon in the book?",
},
],
}
],
)
print(response.output_text)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
file, err := os.Open("draconomicon.pdf")
if err != nil {
panic(err)
}
defer file.Close()
uploadedFile, err := client.Files.New(context.Background(), openai.FileNewParams{
File: file,
Purpose: openai.FilePurposeUserData,
})
if err != nil {
panic(err)
}
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{
{
OfInputFile: &responses.ResponseInputFileParam{
FileID: openai.String(uploadedFile.ID),
},
},
responses.ResponseInputContentParamOfInputText(
"What is the first dragon in the book?",
),
},
responses.EasyInputMessageRoleUser,
),
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
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
29
30
31
32
33
34
35
36
37
38
39
40import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FilePurpose;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputFile;
import com.openai.models.responses.ResponseInputItem;
import java.nio.file.Path;
import java.util.List;
var file =
client
.files()
.create(
FileCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))
.purpose(FilePurpose.USER_DATA)
.build());
var response =
client
.responses()
.create(
ResponseCreateParams.builder()
.model("gpt-6-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addContent(
ResponseInputFile.builder().fileId(file.id()).build())
.addInputTextContent("What is the first dragon in the book?")
.build())))
.build());
response.output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
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
29using OpenAI.Files;
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
OpenAIFileClient files = new(key);
OpenAIFile file = await files.UploadFileAsync(
"draconomicon.pdf",
FileUploadPurpose.UserData
);
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
[
ResponseItem.CreateUserMessageItem(
[
ResponseContentPart.CreateInputFilePart(file.Id),
ResponseContentPart.CreateInputTextPart(
"What is the first dragon in the book?"
),
]
),
]
);
Console.WriteLine(response.GetOutputText());
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
29
30require "openai"
require "pathname"
openai = OpenAI::Client.new
file = openai.files.create(
file: Pathname("draconomicon.pdf"),
purpose: "user_data"
)
response = openai.responses.create(
model: "gpt-6-astra",
input: [
{
role: "user",
content: [
{
type: "input_file",
file_id: file.id
},
{
type: "input_text",
text: "What is the first dragon in the book?"
}
]
}
]
)
puts(response.output_text)
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
26curl 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?"
}
]
}
]
}'
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
29
30
31import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const file = await client.files.create({
file: fs.createReadStream("fixtures/draconomicon.pdf"),
purpose: "user_data",
});
const completion = await client.chat.completions.create({
model: "gpt-6-astra",
messages: [
{
role: "user",
content: [
{
type: "file",
file: {
file_id: file.id,
},
},
{
type: "text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(completion.choices[0].message.content);
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
28from openai import OpenAI
client = OpenAI()
file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")
completion = client.chat.completions.create(
model="gpt-6-astra",
messages=[
{
"role": "user",
"content": [
{
"type": "file",
"file": {
"file_id": file.id,
},
},
{
"type": "text",
"text": "What is the first dragon in the book?",
},
],
}
],
)
print(completion.choices[0].message.content)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
file, err := os.Open("draconomicon.pdf")
if err != nil {
panic(err)
}
defer file.Close()
uploadedFile, err := client.Files.New(context.Background(), openai.FileNewParams{
File: file,
Purpose: openai.FilePurposeUserData,
})
if err != nil {
panic(err)
}
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage([]openai.ChatCompletionContentPartUnionParam{
openai.FileContentPart(openai.ChatCompletionContentPartFileFileParam{
FileID: openai.String(uploadedFile.ID),
}),
openai.TextContentPart("What is the first dragon in the book?"),
}),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
}
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionContentPart;
import com.openai.models.chat.completions.ChatCompletionContentPartText;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.openai.models.chat.completions.ChatCompletionUserMessageParam;
import com.openai.models.files.FileCreateParams;
import com.openai.models.files.FilePurpose;
import java.nio.file.Path;
import java.util.List;
var file =
client
.files()
.create(
FileCreateParams.builder()
.file(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH")))
.purpose(FilePurpose.USER_DATA)
.build());
var filePart =
ChatCompletionContentPart.ofFile(
ChatCompletionContentPart.File.builder()
.file(ChatCompletionContentPart.File.FileObject.builder().fileId(file.id()).build())
.build());
var textPart =
ChatCompletionContentPart.ofText(
ChatCompletionContentPartText.builder().text("Summarize this PDF.").build());
var completion =
client
.chat()
.completions()
.create(
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addMessage(
ChatCompletionUserMessageParam.builder()
.contentOfArrayOfContentParts(List.of(filePart, textPart))
.build())
.build());
completion.choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22using OpenAI.Chat;
using OpenAI.Files;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
ChatClient client = new(model, key);
OpenAIFileClient files = new(key);
OpenAIFile file = await files.UploadFileAsync("draconomicon.pdf", FileUploadPurpose.UserData);
ChatCompletion completion = await client.CompleteChatAsync(
new UserChatMessage(
[
ChatMessageContentPart.CreateFilePart(file.Id),
ChatMessageContentPart.CreateTextPart("What is the first dragon in the book?"),
]
)
);
Console.WriteLine(completion.Content[0].Text);
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
28require "openai"
require "pathname"
client = OpenAI::Client.new
file = client.files.create(
file: Pathname("draconomicon.pdf"),
purpose: :user_data
)
completion = client.chat.completions.create(
model: "gpt-6-astra",
messages: [
{
role: :user,
content: [
{
type: :file,
file: { file_id: file.id }
},
{
type: :text,
text: "Summarize this PDF."
}
]
}
]
)
puts(completion.choices.fetch(0).message.content)
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
28curl 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-6-astra",
"messages": [
{
"role": "user",
"content": [
{
"type": "file",
"file": {
"file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
}
},
{
"type": "text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'
ファイル入力は、Base64 エンコードしたファイルデータとして送信することもできます。
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
28import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const data = fs.readFileSync("fixtures/draconomicon.pdf");
const base64String = data.toString("base64");
const response = await client.responses.create({
model: "gpt-6-astra",
input: [
{
role: "user",
content: [
{
type: "input_file",
filename: "draconomicon.pdf",
file_data: `data:application/pdf;base64,${base64String}`,
},
{
type: "input_text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(response.output_text);
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
29
30
31import base64
from openai import OpenAI
client = OpenAI()
with open("draconomicon.pdf", "rb") as f:
data = f.read()
base64_string = base64.b64encode(data).decode("utf-8")
response = client.responses.create(
model="gpt-6-astra",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"filename": "draconomicon.pdf",
"file_data": f"data:application/pdf;base64,{base64_string}",
},
{
"type": "input_text",
"text": "What is the first dragon in the book?",
},
],
},
],
)
print(response.output_text)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
data, err := os.ReadFile("draconomicon.pdf")
if err != nil {
panic(err)
}
fileData := "data:application/pdf;base64," + base64.StdEncoding.EncodeToString(data)
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{
OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage(
responses.ResponseInputMessageContentListParam{
{
OfInputFile: &responses.ResponseInputFileParam{
Filename: openai.String("draconomicon.pdf"),
FileData: openai.String(fileData),
},
},
responses.ResponseInputContentParamOfInputText(
"What is the first dragon in the book?",
),
},
responses.EasyInputMessageRoleUser,
),
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
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
29
30
31
32
33
34
35
36import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputFile;
import com.openai.models.responses.ResponseInputItem;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
String pdfData =
Base64.getEncoder()
.encodeToString(Files.readAllBytes(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH"))));
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofMessage(
ResponseInputItem.Message.builder()
.role(ResponseInputItem.Message.Role.USER)
.addContent(
ResponseInputFile.builder()
.filename("document.pdf")
.fileData("data:application/pdf;base64," + pdfData)
.build())
.addInputTextContent("Summarize this document.")
.build())))
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
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
26using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
BinaryData fileBytes = BinaryData.FromBytes(await File.ReadAllBytesAsync("draconomicon.pdf"));
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
[
ResponseItem.CreateUserMessageItem(
[
ResponseContentPart.CreateInputFilePart(
fileBytes,
"application/pdf",
"draconomicon.pdf"
),
ResponseContentPart.CreateInputTextPart(
"What is the first dragon in the book?"
),
]
),
]
);
Console.WriteLine(response.GetOutputText());
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
26require "base64"
require "openai"
client = OpenAI::Client.new
pdf_data = Base64.strict_encode64(File.binread("draconomicon.pdf"))
response = client.responses.create(
model: "gpt-6-astra",
input: [
{
role: :user,
content: [
{
type: :input_file,
filename: "document.pdf",
file_data: "data:application/pdf;base64,#{pdf_data}"
},
{
type: :input_text,
text: "Summarize this document."
}
]
}
]
)
puts(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22curl "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?"
}
]
}
]
}'
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
29
30import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const data = fs.readFileSync("fixtures/draconomicon.pdf");
const base64String = data.toString("base64");
const completion = await client.chat.completions.create({
model: "gpt-6-astra",
messages: [
{
role: "user",
content: [
{
type: "file",
file: {
filename: "draconomicon.pdf",
file_data: `data:application/pdf;base64,${base64String}`,
},
},
{
type: "text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(completion.choices[0].message.content);
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
29
30
31
32
33import base64
from openai import OpenAI
client = OpenAI()
with open("draconomicon.pdf", "rb") as f:
data = f.read()
base64_string = base64.b64encode(data).decode("utf-8")
completion = client.chat.completions.create(
model="gpt-6-astra",
messages=[
{
"role": "user",
"content": [
{
"type": "file",
"file": {
"filename": "draconomicon.pdf",
"file_data": f"data:application/pdf;base64,{base64_string}",
},
},
{
"type": "text",
"text": "What is the first dragon in the book?",
},
],
},
],
)
print(completion.choices[0].message.content)
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
29
30
31
32
33
34
35
36
37
38package main
import (
"context"
"encoding/base64"
"fmt"
"os"
"github.com/openai/openai-go/v3"
)
func main() {
client := openai.NewClient()
data, err := os.ReadFile("draconomicon.pdf")
if err != nil {
panic(err)
}
fileData := "data:application/pdf;base64," + base64.StdEncoding.EncodeToString(data)
completion, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "gpt-6-astra",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage([]openai.ChatCompletionContentPartUnionParam{
openai.FileContentPart(openai.ChatCompletionContentPartFileFileParam{
Filename: openai.String("draconomicon.pdf"),
FileData: openai.String(fileData),
}),
openai.TextContentPart("What is the first dragon in the book?"),
}),
},
})
if err != nil {
panic(err)
}
fmt.Println(completion.Choices[0].Message.Content)
}
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
29
30
31
32
33
34
35
36
37
38
39
40import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionContentPart;
import com.openai.models.chat.completions.ChatCompletionContentPartText;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.openai.models.chat.completions.ChatCompletionUserMessageParam;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.List;
String encoded =
Base64.getEncoder()
.encodeToString(Files.readAllBytes(Path.of(System.getenv("OPENAI_EXAMPLE_FILE_PATH"))));
ChatCompletionContentPart file =
ChatCompletionContentPart.ofFile(
ChatCompletionContentPart.File.builder()
.file(
ChatCompletionContentPart.File.FileObject.builder()
.filename("document.pdf")
.fileData("data:application/pdf;base64," + encoded)
.build())
.build());
ChatCompletionContentPart text =
ChatCompletionContentPart.ofText(
ChatCompletionContentPartText.builder().text("Summarize this document.").build());
ChatCompletionCreateParams params =
ChatCompletionCreateParams.builder()
.model("gpt-6-astra")
.addMessage(
ChatCompletionUserMessageParam.builder()
.contentOfArrayOfContentParts(List.of(file, text))
.build())
.build();
client.chat().completions().create(params).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22using OpenAI.Chat;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
string model = "gpt-6-astra";
ChatClient client = new(model, key);
BinaryData fileBytes = BinaryData.FromBytes(await File.ReadAllBytesAsync("draconomicon.pdf"));
ChatCompletion completion = await client.CompleteChatAsync(
new UserChatMessage(
[
ChatMessageContentPart.CreateFilePart(
fileBytes,
"application/pdf",
"draconomicon.pdf"
),
ChatMessageContentPart.CreateTextPart("What is the first dragon in the book?"),
]
)
);
Console.WriteLine(completion.Content[0].Text);
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
28require "base64"
require "openai"
client = OpenAI::Client.new
pdf_data = Base64.strict_encode64(File.binread("draconomicon.pdf"))
completion = client.chat.completions.create(
model: "gpt-6-astra",
messages: [
{
role: :user,
content: [
{
type: :file,
file: {
filename: "document.pdf",
file_data: "data:application/pdf;base64,#{pdf_data}"
}
},
{
type: :text,
text: "Summarize this document."
}
]
}
]
)
puts(completion.choices.fetch(0).message.content)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"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?"
}
]
}
]
}'
ファイル入力を使用する際は、次の点に注意してください。
- トークン使用量: PDF を解析すると、抽出したテキストと各ページの画像がコンテキストに含まれるため、トークン使用量が増えることがあります。Responses API では、
detail を auto(デフォルト)、low、high のいずれかに設定して、PDF のページ画像の詳細度を調整できます。大規模に利用する前に、料金とトークン使用量への影響を確認してください。料金の詳細を参照してください。
- ファイルサイズの上限: 1 回のリクエストに複数のファイルを含められますが、各ファイルのサイズは 50 MB 未満である必要があります。リクエストに含まれるすべてのファイルの合計サイズの上限は 50 MB です。
- 対応モデル: テキストとページ画像の両方を使って PDF を解析するには、
gpt-4o 以降のモデルなど、画像入力に対応したモデルが必要です。
- アップロードの目的: ファイルのアップロード時には、対応している任意の目的を指定できます。モデルの入力として渡す予定のファイルには
user_data を指定してください。
この一覧は Responses API に適用されます。Chat Completions で対応している形式は .pdf(application/pdf)のみで、この制限は file_data と file_id の両方に適用されます。
| カテゴリ |
拡張子 |
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 スプレッドシート |
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 ドキュメント |
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 スライド |
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 |
目的に応じて、次のリソースを活用してください。