回應 API 是來自 Azure OpenAI 的新具狀態 API。 它會將聊天完成和助理 API 中的最佳功能整合在一個統一的體驗中。 回應 API 也會新增支援支援computer-use-preview功能的新模型。
回應 API
API 支援
區域可用性
回應 API 目前可在下列區域中使用:
- australiaeast
- brazilsouth(巴西南部)
- canadacentral
- canadaeast
- eastus
- eastus2
- francecentral
- 德國西中部
- italynorth
- japaneast
- koreacentral
- northcentralus
- norwayeast
- polandcentral
- southafricanorth
- southcentralus
- southeastasia
- southindia
- spaincentral
- swedencentral
- switzerlandnorth
- uaenorth
- uksouth
- westus
- westus3
模型支援
-
gpt-5.1-codex-max(版本:2025-12-04) -
gpt-5.1(版本:2025-11-13) -
gpt-5.1-chat(版本:2025-11-13) -
gpt-5.1-codex(版本:2025-11-13) -
gpt-5.1-codex-mini(版本:2025-11-13) -
gpt-5-pro(版本:2025-10-06) -
gpt-5-codex(版本:2025-09-11) -
gpt-5(版本:2025-08-07) -
gpt-5-mini(版本:2025-08-07) -
gpt-5-nano(版本:2025-08-07) -
gpt-5-chat(版本:2025-08-07) -
gpt-5-chat(版本:2025-10-03) -
gpt-5-codex(版本:2025-09-15) -
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) -
gpt-image-1-mini(版本:2025-10-06) -
o1(版本:2024-12-17) -
o3-mini(版本:2025-01-31) -
o3(版本:2025-04-16) -
o4-mini(版本:2025-04-16)
回應 API 所支援的區域不提供每個模型。 檢查模型頁面以了解模型區域的可用性。
Note
目前不支援:
- 與
/responses/compact的壓縮 - 利用多回合剪輯與串流產生影像。
- 影像無法上傳為檔案,然後參考為輸入。
有下列已知問題:
-
現在支援 PDF 作為輸入檔,但目前不支援將檔案上傳目的設定為
user_data。 - 背景模式與串流搭配使用時的效能問題。 問題預計將很快解決。
參考文件
開始使用回應 API
若要存取回應 API 命令,您需要升級 OpenAI 連結庫的版本。
pip install --upgrade openai
產生文字回應
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
)
response = client.responses.create(
model="gpt-4.1-nano", # Replace with your model deployment name
input="This is a test.",
)
print(response.model_dump_json(indent=2))
Important
請謹慎使用 API 金鑰。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。 如果您使用 API 金鑰,請安全地將其儲存在 Azure 金鑰保存庫。 如需在應用程式中安全地使用 API 金鑰的詳細資訊,請參閱 使用 Azure Key Vault 的 API 金鑰。
如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求。
擷取回應
從先前對回應 API 的呼叫擷取回應。
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
base_url="https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
)
response = client.responses.retrieve("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
Important
請謹慎使用 API 金鑰。 請勿在程式碼中直接包含 API 金鑰,且切勿公開張貼金鑰。 如果您使用 API 金鑰,請安全地將其儲存在 Azure 金鑰保存庫。 如需在應用程式中安全地使用 API 金鑰的詳細資訊,請參閱 使用 Azure Key Vault 的 API 金鑰。
如需 AI 服務安全性的詳細資訊,請參閱驗證對 Azure AI 服務的要求。
刪除回應
根據預設,回應數據會保留 30 天。 若要刪除回應,您可以使用 response.delete ("{response_id}")
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
response = client.responses.delete("resp_67cb61fa3a448190bcf2c42d96f0d1a8")
print(response)
將回應鏈結在一起
您可以透過將上一個回應中的 response.id 傳遞至 previous_response_id 參數,來將回應串接在一起。
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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
}
手動鏈結回應
或者,您可以使用下列方法,手動將響應鏈結在一起:
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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='')
函式呼叫
回應 API 支援函數呼叫。
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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))
程式碼解譯器
程式代碼解釋器工具可讓模型在安全的沙盒化環境中撰寫和執行 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?"
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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)
容器
Important
程式碼解譯器除了 Azure OpenAI 使用量的權杖費用之外,還有額外的費用。 如果您的回應 API 會在兩個不同的線程中同時呼叫程式代碼解釋器,則會建立兩個程式代碼解釋器會話。 預設情況下,每個會話的作用中時間為 1 小時,閒置逾時為 20 分鐘。
程式代碼解釋器工具需要容器,也就是模型可執行 Python 程式代碼的完全沙盒化虛擬機。 容器可以包含上傳的檔案或在執行期間產生的檔案。
若要建立容器,請在建立新的 Response 物件時,於工具組態中指定 "container": { "type": "auto", "file_ids": ["file-1", "file-2"] } 。 這會自動建立新的容器,或重複使用模型內容中先前 code_interpreter_call 的作用中容器。 在 APIwill 的輸出中,code_interpreter_call 包含產生的 container_id。 如果未使用 20 分鐘,此容器就會過期。
檔案輸入和輸出
執行程式代碼解釋器時,模型可以建立自己的檔案。 例如,如果您要求其建構繪圖或建立 CSV,它會直接在容器上建立這些映像。 它會在下一則訊息的註釋中引用這些檔案。
模型輸入中的任何檔案都會自動上傳至容器。 您不需要明確地將它上傳至容器。
受支援的檔案
| 檔案格式 | 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 |
列出輸入項目
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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"
}
影像輸入
對於具備視覺功能的模型,支援 PNG(.png)、JPEG(.jpeg 和 .jpg)、WEBP(.webp)格式的圖片。
影像 URL
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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)
檔案輸入
具有視覺功能的模型支援 PDF 輸入。 PDF 檔案可以做為Base64編碼的數據或檔案標識碼來提供。 為了協助模型解譯 PDF 內容,每個頁面的擷取文字和影像都會包含在模型的內容中。 當透過圖表或非文字內容傳達重要資訊時,這會很有用。
Note
所有擷取的文字和影像都會放入模型的內容中。 請務必瞭解使用 PDF 作為輸入時的定價和令牌使用的影響。
在單一 API 請求中,跨多個輸入 (檔案) 上傳的內容大小應在模型的內容長度內。
只有同時支援文字和影像輸入的模型才能接受 PDF 檔案作為輸入。
目前不支援
purpose的user_data。 作為暫時的因應措施,您必須將目的設定為assistants。
將 PDF 轉換為 Base64 並分析
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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。
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
# 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 下進行處理:
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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 \
-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 \
-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) 伺服器上裝載的工具,以擴充模型的功能。 這些伺服器由開發人員和組織維護,並公開 MCP 相容用戶端可以存取的工具,例如回應 API。
模型內容通訊協定 (MCP) 是一個開放標準,可定義應用程式如何為大型語言模型 (LLM) 提供工具和內容相關數據。 其可讓外部工具與模型工作流程整合一致且可調整。
下列範例示範如何使用虛構的MCP伺服器來查詢 Azure REST API 的相關信息。 這可讓模型即時擷取和推理存放庫內容。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \
-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?"
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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 工具僅適用於回應 API,而且適用於所有較新的模型(gpt-4o、gpt-4.1 和我們的推理模型)。 當您使用 MCP 工具時,您只需支付匯入工具定義或進行工具呼叫時所使用的令牌,不需要額外費用。
Approvals
根據預設,回應 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 \
-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"
}]
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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
Important
- 回應 API 中的 MCP 用戶端需要 TLS 1.2 或更高版本。
- 目前不支援互助 TLS(mTLS)。
- Azure 服務標籤 目前不支援 MCP 用戶端流量。
與 GitHub MCP 伺服器不同,大部分的遠端 MCP 伺服器都需要驗證。 回應 API 中的 MCP 工具支援自定義標頭,可讓您使用所需的驗證配置安全地連線到這些伺服器。
您可以直接在要求中指定 API 金鑰、OAuth 存取令牌或其他認證等標頭。 最常使用的標頭是 Authorization 標頭。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \
-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"
}
]
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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)
背景任務
背景模式可讓您使用 o3 和 o1-pro 等模型,以異步方式執行長時間執行的工作。 這對於需要幾分鐘才能完成的複雜推理工作特別有用,例如 Codex 或 Deep Research 等代理程式所處理的工作。
藉由啟用背景模式,您可以避免超時,並在長時間作業期間維持可靠性。 當使用 "background": true 發送請求時,任務將以異步方式處理,而且您可以不斷輪詢其狀態。
若要啟動背景工作,請在您的要求中將 background 參數設定為 true:
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN" \
-d '{
"model": "o3",
"input": "Write me a very long story",
"background": true
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
response = client.responses.create(
model = "o3",
input = "Write me a very long story",
background = True
)
print(response.status)
使用GET端點來檢查背景響應的狀態。 在狀態為已排入佇列或 in_progress 時,繼續輪詢。 一旦回應達到最終(終態)狀態,即可進行擷取。
curl GET https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses/resp_1234567890 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
from time import sleep
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
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 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
response = client.responses.cancel("resp_1234567890")
print(response.status)
串流背景回應
若要串流背景回應,請將 background 和 stream 設定為 true。 如果您想要稍後在中斷的連線時繼續串流,這會很有用。 使用每個事件的序列號來追蹤您的位置。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \
-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
}'
import os
from openai import OpenAI
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=os.getenv("AZURE_OPENAI_API_KEY")
)
# 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 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AZURE_OPENAI_AUTH_TOKEN"
加密的推理項目
在使用回應 API 的無狀態模式時,無論是設定 store 為 false,或是您的組織已註冊為零資料保留,您仍須保留在交談回合中的推理內容。 若要這樣做,請在 API 請求中包含加密的推理項目。
若要跨回合保留推理專案,請在您的要求中新增 reasoning.encrypted_content 至 include 參數。 這可確保回應包含推理過程的加密版本,這個加密版本可以在未來的要求中傳遞。
curl https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/responses \
-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"]
}'
影像產生 (預覽)
回應 API 可讓影像產生成為交談和多步驟工作流程的一部分。 它支援內容中的影像輸入和輸出,並包含用於產生和編輯影像的內建工具。
相較於獨立映像 API,回應 API 提供數個優點:
- 串流:在產生期間顯示部分影像輸出,以改善感知的延遲。
- 彈性輸入:除了原始影像位元組之外,還接受圖像文件標識碼作為輸入。
Note
Responses API 中的影像生成工具僅支援 gpt-image-1-系列模型。 不過,你可以從以下支援的模型gpt-4o列表中呼叫此模型——、、gpt-4o-mini、gpt-4.1gpt-4.1-minigpt-4.1-nanoo3gpt-5及gpt-5.1系列模型。
回應 API 影像產生工具目前不支援串流模式。 若要使用串流模式並產生部分影像,請直接在回應 API 外部呼叫 影像產生 API 。
如果您想要使用 GPT Image 建立對話式影像體驗,請使用 Responses API。
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=token_provider,
default_headers={"x-ms-oai-image-generation-deployment":"gpt-image-1", "api_version":"preview"}
)
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))
推理模型
如需如何使用推理模型搭配回應 API 的範例,請參閱 推理模型指南。
計算機使用
搭配 Playwright 的電腦使用已移至 專用電腦使用模型指南