Responses API は、Azure OpenAI からの新しいステートフル API です。 チャットの完了とアシスタント API の最高の機能を 1 つの統合されたエクスペリエンスにまとめます。 Responses API では、computer-use-preview
機能を強化する新しい モデルのサポートも追加されています。
Responses API
API support
Region Availability
応答 API は現在、次のリージョンで使用できます。
- australiaeast
- eastus
- eastus2
- francecentral
- japaneast
- norwayeast
- polandcentral
- southindia
- swedencentral
- switzerlandnorth
- uaenorth
- uksouth
- westus
- westus3
Model support
-
gpt-4o
(バージョン:2024-11-20
、2024-08-06
、2024-05-13
) -
gpt-4o-mini
(バージョン:2024-07-18
) computer-use-preview
-
gpt-4.1
(バージョン:2025-04-14
) -
gpt-4.1-nano
(バージョン:2025-04-14
) -
gpt-4.1-mini
(バージョン:2025-04-14
) -
gpt-image-1
(バージョン:2025-04-15
) -
o1
(バージョン:2024-12-17
) -
o3-mini
(バージョン:2025-01-31
) -
o3
(バージョン:2025-04-16
) -
o4-mini
(バージョン:2025-04-16
)
応答 API でサポートされているリージョンですべてのモデルを使用できるわけではありません。 モデルページでモデル地域の可用性を確認します。
Note
現在サポートされていません。
- Web 検索ツール
- 複数ターンの編集とストリーミングを使用した画像の生成 - 近日公開予定
- 画像をファイルとしてアップロードし、入力として参照することはできません。 Coming soon.
次の既知の問題があります。
- 入力ファイルとしての PDF がサポートされるようになりましたが、ファイルのアップロードの目的を
user_data
に設定することは現在サポートされていません。 - バックグラウンド モードがストリーミングで使用される場合のパフォーマンス。 この問題は間もなく解決される予定です。
Reference documentation
応答 API の使い始め方
応答 API コマンドにアクセスするには、OpenAI ライブラリのバージョンをアップグレードする必要があります。
pip install --upgrade openai
テキスト応答を生成する
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4.1-nano",
input= "This is a test"
)
print(response.model_dump_json(indent=2))
応答を取得する
応答 API への以前の呼び出しから応答を取得します。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.retrieve("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
print(response.model_dump_json(indent=2))
Delete response
既定では、応答データは 30 日間保持されます。 応答を削除するには、response.delete"("{response_id})
を使用してください。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.delete("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
print(response)
応答を連結する
前の応答の response.id
を previous_response_id
パラメーターに渡すことで、応答を連結できます。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4o", # replace with your model deployment name
input="Define and explain the concept of catastrophic forgetting?"
)
second_response = client.responses.create(
model="gpt-4o", # replace with your model deployment name
previous_response_id=response.id,
input=[{"role": "user", "content": "Explain this at a level that could be understood by a college freshman"}]
)
print(second_response.model_dump_json(indent=2))
出力から、最初の入力質問を second_response
API 呼び出しと共有したことがない場合でも、モデル previous_response_id
を渡すことによって、新しい質問に答える前の質問と応答の完全なコンテキストがあることに注意してください。
Output:
{
"id": "resp_67cbc9705fc08190bbe455c5ba3d6daf",
"created_at": 1741408624.0,
"error": null,
"incomplete_details": null,
"instructions": null,
"metadata": {},
"model": "gpt-4o-2024-08-06",
"object": "response",
"output": [
{
"id": "msg_67cbc970fd0881908353a4298996b3f6",
"content": [
{
"annotations": [],
"text": "Sure! Imagine you are studying for exams in different subjects like math, history, and biology. You spend a lot of time studying math first and get really good at it. But then, you switch to studying history. If you spend all your time and focus on history, you might forget some of the math concepts you learned earlier because your brain fills up with all the new history facts. \n\nIn the world of artificial intelligence (AI) and machine learning, a similar thing can happen with computers. We use special programs called neural networks to help computers learn things, sort of like how our brain works. But when a neural network learns a new task, it can forget what it learned before. This is what we call \"catastrophic forgetting.\"\n\nSo, if a neural network learned how to recognize cats in pictures, and then you teach it how to recognize dogs, it might get really good at recognizing dogs but suddenly become worse at recognizing cats. This happens because the process of learning new information can overwrite or mess with the old information in its \"memory.\"\n\nScientists and engineers are working on ways to help computers remember everything they learn, even as they keep learning new things, just like students have to remember math, history, and biology all at the same time for their exams. They use different techniques to make sure the neural network doesn’t forget the important stuff it learned before, even when it gets new information.",
"type": "output_text"
}
],
"role": "assistant",
"status": null,
"type": "message"
}
],
"parallel_tool_calls": null,
"temperature": 1.0,
"tool_choice": null,
"tools": [],
"top_p": 1.0,
"max_output_tokens": null,
"previous_response_id": "resp_67cbc96babbc8190b0f69aedc655f173",
"reasoning": null,
"status": "completed",
"text": null,
"truncation": null,
"usage": {
"input_tokens": 405,
"output_tokens": 285,
"output_tokens_details": {
"reasoning_tokens": 0
},
"total_tokens": 690
},
"user": null,
"reasoning_effort": null
}
応答を手動で連結する
または、次の方法を使用して、応答を手動で連結することもできます。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
inputs = [{"type": "message", "role": "user", "content": "Define and explain the concept of catastrophic forgetting?"}]
response = client.responses.create(
model="gpt-4o", # replace with your model deployment name
input=inputs
)
inputs += response.output
inputs.append({"role": "user", "type": "message", "content": "Explain this at a level that could be understood by a college freshman"})
second_response = client.responses.create(
model="gpt-4o",
input=inputs
)
print(second_response.model_dump_json(indent=2))
Streaming
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
input = "This is a test",
model = "o4-mini", # replace with model deployment name
stream = True
)
for event in response:
if event.type == 'response.output_text.delta':
print(event.delta, end='')
Function calling
応答 API では、関数呼び出しがサポートされています。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4o", # replace with your model deployment name
tools=[
{
"type": "function",
"name": "get_weather",
"description": "Get the weather for a ___location",
"parameters": {
"type": "object",
"properties": {
"___location": {"type": "string"},
},
"required": ["___location"],
},
}
],
input=[{"role": "user", "content": "What's the weather in San Francisco?"}],
)
print(response.model_dump_json(indent=2))
# To provide output to tools, add a response for each tool call to an array passed
# to the next response as `input`
input = []
for output in response.output:
if output.type == "function_call":
match output.name:
case "get_weather":
input.append(
{
"type": "function_call_output",
"call_id": output.call_id,
"output": '{"temperature": "70 degrees"}',
}
)
case _:
raise ValueError(f"Unknown function call: {output.name}")
second_response = client.responses.create(
model="gpt-4o",
previous_response_id=response.id,
input=input
)
print(second_response.model_dump_json(indent=2))
Code Interpreter
コード インタープリター ツールを使用すると、セキュリティで保護されたサンドボックス環境で Python コードを記述して実行できます。 次のようなさまざまな高度なタスクがサポートされています。
- さまざまなデータ形式と構造を持つファイルの処理
- データと視覚化を含むファイルの生成 (グラフなど)
- 問題を解決するためのコードを繰り返し記述して実行します。モデルは、成功するまでコードをデバッグして再試行できます
- トリミング、ズーム、回転などの画像変換を有効にして、サポートされているモデル (o3、o4-mini など) の視覚的推論を強化する
- このツールは、データ分析、数学計算、およびコード生成を含むシナリオに特に役立ちます。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1",
"tools": [
{ "type": "code_interpreter", "container": {"type": "auto"} }
],
"instructions": "You are a personal math tutor. When asked a math question, write and run code using the python tool to answer the question.",
"input": "I need to solve the equation 3x + 11 = 14. Can you help me?"
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
instructions = "You are a personal math tutor. When asked a math question, write and run code using the python tool to answer the question."
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "code_interpreter",
"container": {"type": "auto"}
}
],
instructions=instructions,
input="I need to solve the equation 3x + 11 = 14. Can you help me?",
)
print(response.output)
Containers
Important
コード インタープリターを使うと、Azure OpenAI の使用に対するトークン ベースの料金の他に、追加料金が発生します。 Responses API が 2 つの異なるスレッドでコード インタープリターを同時に呼び出すと、2 つのコード インタープリター セッションが作成されます。 各セッションは既定で 1 時間アクティブで、アイドル タイムアウトは 30 分です。
コード インタープリター ツールには、コンテナー (モデルが Python コードを実行できる完全にサンドボックス化された仮想マシン) が必要です。 コンテナーには、アップロードされたファイルまたは実行中に生成されたファイルを含めることができます。
コンテナーを作成するには、新しい Response オブジェクトを作成するときにツール構成で "container": { "type": "auto", "files": ["file-1", "file-2"] }
を指定します。 これにより、新しいコンテナーが自動的に作成されるか、モデルのコンテキストで以前のcode_interpreter_callのアクティブなコンテナーが再利用されます。 APIwill の出力の code_interpreter_call
には、生成された container_id
が含まれます。 このコンテナーは、20 分間使用しないと有効期限が切れます。
ファイルの入力と出力
コード インタープリターを実行する場合、モデルは独自のファイルを作成できます。 たとえば、プロットの作成や CSV の作成を依頼した場合、コンテナーにこれらのイメージが直接作成されます。 次のメッセージの注釈でこれらのファイルを引用します。
モデル入力内のすべてのファイルがコンテナーに自動的にアップロードされます。 コンテナーに明示的にアップロードする必要はありません。
Supported Files
File format | MIME の種類 |
---|---|
.c |
text/x-c |
.cs |
text/x-csharp |
.cpp |
text/x-c++ |
.csv |
text/csv |
.doc |
application/msword |
.docx |
application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.html |
text/html |
.java |
text/x-java |
.json |
application/json |
.md |
text/markdown |
.pdf |
application/pdf |
.php |
text/x-php |
.pptx |
application/vnd.openxmlformats-officedocument.presentationml.presentation |
.py |
text/x-python |
.py |
text/x-script.python |
.rb |
text/x-ruby |
.tex |
text/x-tex |
.txt |
text/plain |
.css |
text/css |
.js |
text/JavaScript |
.sh |
application/x-sh |
.ts |
application/TypeScript |
.csv |
application/csv |
.jpeg |
image/jpeg |
.jpg |
image/jpeg |
.gif |
image/gif |
.pkl |
application/octet-stream |
.png |
image/png |
.tar |
application/x-tar |
.xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xml |
application/xml または "text/xml" |
.zip |
application/zip |
入力項目を一覧表示する
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.input_items.list("resp_67d856fcfba0819081fd3cffee2aa1c0")
print(response.model_dump_json(indent=2))
Output:
{
"data": [
{
"id": "msg_67d856fcfc1c8190ad3102fc01994c5f",
"content": [
{
"text": "This is a test.",
"type": "input_text"
}
],
"role": "user",
"status": "completed",
"type": "message"
}
],
"has_more": false,
"object": "list",
"first_id": "msg_67d856fcfc1c8190ad3102fc01994c5f",
"last_id": "msg_67d856fcfc1c8190ad3102fc01994c5f"
}
Image input
Image url
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4o",
input=[
{
"role": "user",
"content": [
{ "type": "input_text", "text": "what is in this image?" },
{
"type": "input_image",
"image_url": "<image_URL>"
}
]
}
]
)
print(response)
Base64 でエンコードされたイメージ
import base64
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Path to your image
image_path = "path_to_your_image.jpg"
# Getting the Base64 string
base64_image = encode_image(image_path)
response = client.responses.create(
model="gpt-4o",
input=[
{
"role": "user",
"content": [
{ "type": "input_text", "text": "what is in this image?" },
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image}"
}
]
}
]
)
print(response)
File input
ビジョン機能を備えたモデルでは、PDF 入力がサポートされます。 PDF ファイルは、Base64 でエンコードされたデータまたはファイル ID として提供できます。 モデルが PDF コンテンツを解釈できるように、抽出されたテキストと各ページの画像の両方がモデルのコンテキストに含まれます。 これは、重要な情報が図やテキスト以外のコンテンツを介して伝達される場合に便利です。
Note
抽出されたすべてのテキストと画像は、モデルのコンテキストに配置されます。 PDF を入力として使用した場合の価格とトークンの使用への影響を理解していることを確認してください。
API への 1 回の要求で、複数のファイル入力に対して最大 100 ページと合計コンテンツの 32 MB をアップロードできます。
gpt-4o
、gpt-4o-mini
、o1
など、テキスト入力と画像入力の両方をサポートするモデルのみが、PDF ファイルを入力として受け入れることができます。現在、
purpose
のuser_data
はサポートされていません。 一時的な回避策として、目的をassistants
に設定する必要があります。
PDF を Base64 に変換して分析する
import base64
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE=NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
with open("PDF-FILE-NAME.pdf", "rb") as f: # assumes PDF is in the same directory as the executing script
data = f.read()
base64_string = base64.b64encode(data).decode("utf-8")
response = client.responses.create(
model="gpt-4o-mini", # model deployment name
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"filename": "PDF-FILE-NAME.pdf",
"file_data": f"data:application/pdf;base64,{base64_string}",
},
{
"type": "input_text",
"text": "Summarize this PDF",
},
],
},
]
)
print(response.output_text)
PDF をアップロードして分析する
PDF ファイルをアップロードします。 現在、purpose
のuser_data
はサポートされていません。 回避策として、目的を assistants
に設定する必要があります。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
azure_endpoint = "https://YOUR-RESOURCE=NAME.openai.azure.com/",
azure_ad_token_provider=token_provider,
api_version="2024-10-21"
)
# Upload a file with a purpose of "assistants"
file = client.files.create(
file=open("nucleus_sampling.pdf", "rb"), # This assumes a .pdf file in the same directory as the executing script
purpose="assistants"
)
print(file.model_dump_json(indent=2))
file_id = file.id
Output:
{
"id": "assistant-KaVLJQTiWEvdz8yJQHHkqJ",
"bytes": 4691115,
"created_at": 1752174469,
"filename": "nucleus_sampling.pdf",
"object": "file",
"purpose": "assistants",
"status": "processed",
"expires_at": null,
"status_details": null
}
次に、 id
の値を取得し、それをモデルに渡して、 file_id
で処理します。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE=NAME.openai.azure.com/openai/v1",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4o-mini",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id":"assistant-KaVLJQTiWEvdz8yJQHHkqJ"
},
{
"type": "input_text",
"text": "Summarize this PDF",
},
],
},
]
)
print(response.output_text)
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/files?api-version=2024-10-21 \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-F purpose="assistants" \
-F file="@your_file.pdf" \
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1",
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": "assistant-123456789"
},
{
"type": "input_text",
"text": "ASK SOME QUESTION RELATED TO UPLOADED PDF"
}
]
}
]
}'
リモート MCP サーバーの使用
モデルの機能を拡張するには、リモート モデル コンテキスト プロトコル (MCP) サーバーでホストされているツールに接続します。 これらのサーバーは開発者や組織によって管理され、応答 API など、MCP と互換性のあるクライアントがアクセスできるツールを公開します。
モデル コンテキスト プロトコル (MCP) は、アプリケーションが大規模言語モデル (LLM) にツールとコンテキスト データを提供する方法を定義するオープン標準です。 これにより、外部ツールをモデル ワークフローに一貫性のあるスケーラブルに統合できます。
次の例では、架空の MCP サーバーを使用して、Azure REST API に関する情報を照会する方法を示します。 これにより、モデルはリポジトリのコンテンツをリアルタイムで取得して推論できます。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1",
"tools": [
{
"type": "mcp",
"server_label": "github",
"server_url": "https://contoso.com/Azure/azure-rest-api-specs",
"require_approval": "never"
}
],
"input": "What is this repo in 100 words?"
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4.1", # replace with your model deployment name
tools=[
{
"type": "mcp",
"server_label": "github",
"server_url": "https://contoso.com/Azure/azure-rest-api-specs",
"require_approval": "never"
},
],
input="What transport protocols are supported in the 2025-03-26 version of the MCP spec?",
)
print(response.output_text)
MCP ツールは Responses API でのみ機能し、すべての新しいモデル (gpt-4o、gpt-4.1、および推論モデル) で使用できます。 MCP ツールを使用している場合は、ツール定義のインポートまたはツール呼び出し時に使用されるトークンに対してのみ支払います。追加料金はかかりません。
Approvals
既定では、Responses API は、リモート MCP サーバーとデータを共有する前に明示的な承認を必要とします。 この承認手順は、透明性を確保するのに役立ち、外部に送信される情報を制御できます。
リモート MCP サーバーと共有されているすべてのデータを確認し、必要に応じて監査目的でログに記録することをお勧めします。
承認が必要な場合、モデルは応答出力で mcp_approval_request
項目を返します。 このオブジェクトには、保留中の要求の詳細が含まれており、続行する前にデータを検査または変更できます。
{
"id": "mcpr_682bd9cd428c8198b170dc6b549d66fc016e86a03f4cc828",
"type": "mcp_approval_request",
"arguments": {},
"name": "fetch_azure_rest_api_docs",
"server_label": "github"
}
リモート MCP 呼び出しを続行するには、mcp_approval_response項目を含む新しい応答オブジェクトを作成して、承認要求に応答する必要があります。 このオブジェクトは、モデルが指定されたデータをリモート MCP サーバーに送信することを許可する意図を確認します。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1",
"tools": [
{
"type": "mcp",
"server_label": "github",
"server_url": "https://contoso.com/Azure/azure-rest-api-specs",
"require_approval": "never"
}
],
"previous_response_id": "resp_682f750c5f9c8198aee5b480980b5cf60351aee697a7cd77",
"input": [{
"type": "mcp_approval_response",
"approve": true,
"approval_request_id": "mcpr_682bd9cd428c8198b170dc6b549d66fc016e86a03f4cc828"
}]
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4.1", # replace with your model deployment name
tools=[
{
"type": "mcp",
"server_label": "github",
"server_url": "https://contoso.com/Azure/azure-rest-api-specs",
"require_approval": "never"
},
],
previous_response_id="resp_682f750c5f9c8198aee5b480980b5cf60351aee697a7cd77",
input=[{
"type": "mcp_approval_response",
"approve": True,
"approval_request_id": "mcpr_682bd9cd428c8198b170dc6b549d66fc016e86a03f4cc828"
}],
)
Authentication
GitHub MCP サーバーとは異なり、ほとんどのリモート MCP サーバーでは認証が必要です。 Responses API の MCP ツールはカスタム ヘッダーをサポートしているため、必要な認証スキームを使用してこれらのサーバーに安全に接続できます。
API キー、OAuth アクセス トークン、その他の資格情報などのヘッダーは、要求で直接指定できます。 最も一般的に使用されるヘッダーは、 Authorization
ヘッダーです。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "gpt-4.1",
"input": "What is this repo in 100 words?"
"tools": [
{
"type": "mcp",
"server_label": "github",
"server_url": "https://contoso.com/Azure/azure-rest-api-specs",
"headers": {
"Authorization": "Bearer $YOUR_API_KEY"
}
]
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model="gpt-4.1",
input="What is this repo in 100 words?",
tools=[
{
"type": "mcp",
"server_label": "github",
"server_url": "https://gitmcp.io/Azure/azure-rest-api-specs",
"headers": {
"Authorization": "Bearer $YOUR_API_KEY"
}
]
)
print(response.output_text)
Background tasks
バックグラウンド モードでは、o3 や o1-pro などのモデルを使用して、実行時間の長いタスクを非同期的に実行できます。 これは、Codex や Deep Research などのエージェントによって処理されるタスクなど、完了までに数分かかる複雑な推論タスクに特に役立ちます。
バックグラウンド モードを有効にすると、タイムアウトを回避し、拡張操作中に信頼性を維持できます。
"background": true
を使用して要求が送信されると、タスクは非同期的に処理され、時間の経過と共にその状態をポーリングできます。
バックグラウンド タスクを開始するには、要求で background パラメーターを true に設定します。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "o3",
"input": "Write me a very long story",
"background": true
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model = "o3",
input = "Write me a very long story",
background = True
)
print(response.status)
GET
エンドポイントを使用して、バックグラウンド応答の状態を確認します。 状態が queued または in_progress の間、ポーリングを続行します。 応答が最終的な (ターミナル) 状態になると、取得できるようになります。
curl GET https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses/resp_1234567890?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from time import sleep
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.create(
model = "o3",
input = "Write me a very long story",
background = True
)
while response.status in {"queued", "in_progress"}:
print(f"Current status: {response.status}")
sleep(2)
response = client.responses.retrieve(response.id)
print(f"Final status: {response.status}\nOutput:\n{response.output_text}")
cancel
エンドポイントを使用して、進行中のバックグラウンド タスクを取り消すことができます。 キャンセルは、処理を繰り返しても結果が変わらない性質があります。続いて行われる呼び出しでは、最終的な応答オブジェクトが返されます。
curl -X POST https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses/resp_1234567890/cancel?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
response = client.responses.cancel("resp_1234567890")
print(response.status)
バックグラウンド応答をストリーミングする
バックグラウンド応答をストリーミングするには、 background
と stream
の両方を true に設定します。 これは、接続が切断された場合に後でストリーミングを再開する場合に便利です。 各イベントのsequence_numberを使用して、自分の位置を追跡します。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "o3",
"input": "Write me a very long story",
"background": true,
"stream": true
}'
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview"
)
# Fire off an async response but also start streaming immediately
stream = client.responses.create(
model="o3",
input="Write me a very long story",
background=True,
stream=True,
)
cursor = None
for event in stream:
print(event)
cursor = event["sequence_number"]
Note
バックグラウンド応答では、現在、同期応答よりも、最初のトークンまでの待機時間が長くなっています。 このギャップを減らすために、改善が進行中です。
Limitations
- バックグラウンド モードでは、
store=true
が必要です。 ステートレス要求はサポートされていません。 - 元の要求が
stream=true
含まれている場合にのみ、ストリーミングを再開できます。 - 同期応答を取り消すには、接続を直接終了します。
特定のポイントからのストリーミングを再開する
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses/resp_1234567890?stream=true&starting_after=42&api-version=2025-04-01-preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
暗号化された推論項目
store
を false に設定するか、組織がゼロ データ保持に登録されている場合に、ステートレス モードで Responses API を使用する場合は、会話のターン間で推論コンテキストを保持する必要があります。 これを行うには、API 要求に暗号化された推論項目を含めます。
複数の順番で理由項目を保持するには、要求のreasoning.encrypted_content
パラメーターにinclude
を追加します。 これにより、応答に暗号化されたバージョンの推論トレースが含まれていることが保証されます。これは、今後の要求で渡すことができます。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses?api-version=preview \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "o4-mini",
"reasoning": {"effort": "medium"},
"input": "What is the weather like today?",
"tools": [<YOUR_FUNCTION GOES HERE>],
"include": ["reasoning.encrypted_content"]
}'
Image generation
Responses API を使用すると、会話とマルチステップ ワークフローの一部として画像を生成できます。 コンテキスト内の画像の入力と出力をサポートし、画像を生成および編集するための組み込みのツールが含まれています。
スタンドアロンの Image API と比較して、Responses API にはいくつかの利点があります。
- ストリーミング: 生成中に部分的な画像出力を表示して、認識される待機時間を改善します。
- 柔軟な入力: 生の画像バイトに加えて、画像ファイル ID を入力として受け入れます。
Note
Responses API の画像生成ツールは、 gpt-image-1
モデルでのみサポートされています。 ただし、このモデルは、サポートされているモデルの一覧 ( gpt-4o
、 gpt-4o-mini
、 gpt-4.1
、 gpt-4.1-mini
、 gpt-4.1-nano
、 o3
) から呼び出すことができます。
次の場合は、Responses API を使用します。
- GPT Image を使用して会話型イメージ エクスペリエンスを構築します。
- 生成中に部分的な画像の結果をストリーミングして、よりスムーズなユーザー エクスペリエンスを実現します。
イメージを生成する
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview",
default_headers={"x-ms-oai-image-generation-deployment":"YOUR-GPT-IMAGE1-DEPLOYMENT-NAME"}
)
response = client.responses.create(
model="o3",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation"}],
)
# Save the image to a file
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
Streaming
Responses API を使用して部分的なイメージをストリーミングできます。
partial_images
を使用して、1 ~ 3 個の部分的な画像を受信できます。
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview",
default_headers={"x-ms-oai-image-generation-deployment":"YOUR-GPT-IMAGE1-DEPLOYMENT-NAME"}
)
stream = client.responses.create(
model="gpt-4.1",
input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
stream=True,
tools=[{"type": "image_generation", "partial_images": 2}],
)
for event in stream:
if event.type == "response.image_generation_call.partial_image":
idx = event.partial_image_index
image_base64 = event.partial_image_b64
image_bytes = base64.b64decode(image_base64)
with open(f"river{idx}.png", "wb") as f:
f.write(image_bytes)
Edit images
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
import base64
client = AzureOpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
azure_ad_token_provider=token_provider,
api_version="preview",
default_headers={"x-ms-oai-image-generation-deployment":"YOUR-GPT-IMAGE1-DEPLOYMENT-NAME"}
)
def create_file(file_path):
with open(file_path, "rb") as file_content:
result = client.files.create(
file=file_content,
purpose="vision",
)
return result.id
def encode_image(file_path):
with open(file_path, "rb") as f:
base64_image = base64.b64encode(f.read()).decode("utf-8")
return base64_image
prompt = """Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures."""
base64_image1 = encode_image("image1.png")
base64_image2 = encode_image("image2.png")
file_id1 = create_file("image3.png")
file_id2 = create_file("image4.png")
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": prompt},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image1}",
},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image2}",
},
{
"type": "input_image",
"file_id": file_id1,
},
{
"type": "input_image",
"file_id": file_id2,
}
],
}
],
tools=[{"type": "image_generation"}],
)
image_generation_calls = [
output
for output in response.output
if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
with open("gift-basket.png", "wb") as f:
f.write(base64.b64decode(image_base64))
else:
print(response.output.content)
Reasoning models
応答 API で推論モデルを使用する方法の例については、 推論モデルガイドを参照してください。
Computer use
Playwright でのコンピューターの使用は、専用のコンピューター使用モデル ガイドに移行しました