此相容層主要用於測試和比較模型功能,對於大多數使用案例而言,並不被視為長期或可用於生產環境的解決方案。雖然我們希望它能保持完整功能且不會有破壞性變更,但優先考量的仍是 Claude API 的可靠性與效能。
如需了解已知相容性限制的更多資訊,請參閱重要的 OpenAI 相容性限制。
如果您在使用 OpenAI SDK 相容性功能時遇到任何問題,請透過此相容性意見回饋表單分享您的意見。
為獲得最佳體驗並存取 Claude API 的完整功能集(PDF 處理、引用、擴展思考和提示快取),請使用原生 Claude API。
若要使用 OpenAI SDK 相容性功能,您需要:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("ANTHROPIC_API_KEY"), # Your Claude API key
base_url="https://api.anthropic.com/v1/", # the Claude API endpoint
)
response = client.chat.completions.create(
model="claude-opus-4-8", # Claude model name
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"},
],
)
print(response.choices[0].message.content)以下是與使用 OpenAI 最主要的差異:
strict 參數會被忽略,這表示工具使用的 JSON 不保證會遵循所提供的結構描述。如需保證結構描述的一致性,請使用原生 Claude API 搭配結構化輸出。大多數不支援的欄位會被靜默忽略,而不會產生錯誤。這些內容都記錄在下方。
如果您已對提示進行了大量調整,它很可能是專門針對 OpenAI 進行最佳化的。建議使用 Claude Console 中的提示改進工具作為良好的起點。
OpenAI SDK 的大多數輸入都能清楚地直接對應到 Anthropic 的 API 參數,但一個明顯的差異是系統/開發者提示的處理方式。透過 OpenAI,這兩種提示可以放置在聊天對話的任何位置。由於 Anthropic 僅支援初始系統訊息,API 會將所有系統/開發者訊息收集起來,並在它們之間以單一換行符號(\n)串接在一起。然後,這個完整的字串會作為單一系統訊息提供在訊息的開頭。
您可以透過新增 thinking 參數來啟用擴展思考功能。雖然這能改善 Claude 對複雜任務的推理能力,但 OpenAI SDK 不會回傳 Claude 的詳細思考過程。如需完整的擴展思考功能,包括存取 Claude 的逐步推理輸出,請使用原生 Claude API。
response = client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Who are you?"}],
extra_body={"thinking": {"type": "enabled", "budget_tokens": 2000}},
)速率限制遵循 Anthropic 針對 /v1/messages 端點的標準限制。
| 欄位 | 支援狀態 |
|---|---|
model | 使用 Claude 模型名稱 |
max_tokens | 完全支援 |
max_completion_tokens | 完全支援 |
stream | 完全支援 |
stream_options | 完全支援 |
top_p | 完全支援 |
parallel_tool_calls | 完全支援 |
stop | 所有非空白字元的停止序列皆可運作 |
temperature | 介於 0 和 1 之間(含)。大於 1 的值會被限制為 1。 |
n | 必須恰好為 1 |
logprobs | 忽略 |
metadata | 忽略 |
response_format | 忽略。如需 JSON 輸出,請使用原生 Claude API 的結構化輸出 |
prediction | 忽略 |
presence_penalty | 忽略 |
frequency_penalty | 忽略 |
seed | 忽略 |
service_tier | 忽略 |
audio | 忽略 |
logit_bias | 忽略 |
store | 忽略 |
user | 忽略 |
modalities | 忽略 |
top_logprobs | 忽略 |
reasoning_effort | 忽略 |
tools / functions 欄位messages 陣列欄位| 欄位 | 支援狀態 |
|---|---|
id | 完全支援 |
choices[] | 長度永遠為 1 |
choices[].finish_reason | 完全支援 |
choices[].index | 完全支援 |
choices[].message.role | 完全支援 |
choices[].message.content | 完全支援 |
choices[].message.tool_calls | 完全支援 |
object | 完全支援 |
created | 完全支援 |
model | 完全支援 |
finish_reason | 完全支援 |
content | 完全支援 |
usage.completion_tokens | 完全支援 |
usage.prompt_tokens | 完全支援 |
usage.total_tokens | 完全支援 |
usage.completion_tokens_details | 永遠為空 |
usage.prompt_tokens_details | 永遠為空 |
choices[].message.refusal | 永遠為空 |
choices[].message.audio | 永遠為空 |
logprobs | 永遠為空 |
service_tier | 永遠為空 |
system_fingerprint | 永遠為空 |
相容層會維持與 OpenAI API 一致的錯誤格式。然而,詳細的錯誤訊息內容不會完全相同。請僅將錯誤訊息用於記錄和除錯。
雖然 OpenAI SDK 會自動管理標頭,但以下是 Claude API 支援的完整標頭清單,供需要直接處理標頭的開發人員參考。
| 標頭 | 支援狀態 |
|---|---|
x-ratelimit-limit-requests | 完全支援 |
x-ratelimit-limit-tokens | 完全支援 |
x-ratelimit-remaining-requests | 完全支援 |
x-ratelimit-remaining-tokens | 完全支援 |
x-ratelimit-reset-requests | 完全支援 |
x-ratelimit-reset-tokens | 完全支援 |
retry-after | 完全支援 |
request-id | 完全支援 |
openai-version | 永遠為 2020-10-01 |
authorization | 完全支援 |
openai-processing-ms | 永遠為空 |
Was this page helpful?