OpenAI SDK 兼容性
Anthropic 提供了一个兼容层,使您能够使用 OpenAI SDK 来测试 Claude API。只需少量代码更改,即可快速评估 Anthropic 模型的能力。
此兼容层主要用于测试和比较模型能力,对于大多数用例来说不被视为长期或生产就绪的解决方案。虽然它旨在保持完全功能且不会有破坏性变更,但优先考虑的是 Claude API 的可靠性和有效性。
有关已知兼容性限制的更多信息,请参阅重要的 OpenAI 兼容性限制。
如果您在使用 OpenAI SDK 兼容功能时遇到任何问题,请通过此兼容性反馈表分享您的反馈。
为获得最佳体验并访问 Claude API 的完整功能集(PDF 处理、引文、扩展思考和提示缓存),请使用原生 Claude API。
OpenAI SDK 入门
要使用 OpenAI SDK 兼容功能,您需要:
- 使用官方 OpenAI SDK
- 更改以下内容
- 更新您的基础 URL 以指向 Claude API
- 将您的 API 密钥替换为 Claude API 密钥
- 更新您的模型名称以使用 Claude 模型
- 查阅以下文档了解支持的功能
快速开始示例
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-7", # Claude model name
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who are you?"},
],
)
print(response.choices[0].message.content)
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: "ANTHROPIC_API_KEY", // Your Claude API key
baseURL: "https://api.anthropic.com/v1/" // Claude API endpoint
});
const response = await openai.chat.completions.create({
messages: [{ role: "user", content: "Who are you?" }],
model: "claude-opus-4-7" // Claude model name
});
console.log(response.choices[0].message.content);
重要的 OpenAI 兼容性限制
API 行为
以下是使用 OpenAI 时最显著的差异:
- 函数调用的
strict参数会被忽略,这意味着工具使用的 JSON 不保证遵循提供的模式。如需保证模式一致性,请使用原生 Claude API 的结构化输出。 - 不支持音频输入;它将被忽略并从输入中移除
- 不支持提示缓存,但在 Anthropic SDK 中支持
- System/developer 消息会被提升并拼接到对话的开头,因为 Anthropic 仅支持单个初始系统消息。
大多数不支持的字段会被静默忽略而不是产生错误。以下均有文档记录。
输出质量考虑
如果您对提示进行了大量调整,它很可能专门针对 OpenAI 进行了优化。考虑使用 Claude 控制台中的提示改进器作为良好的起点。
System / developer 消息提升
OpenAI SDK 的大多数输入都可以直接映射到 Anthropic 的 API 参数,但一个显著的差异是 system / developer 提示的处理。这两个提示可以通过 OpenAI 放置在整个聊天对话中。由于 Anthropic 仅支持初始系统消息,API 会将所有 system/developer 消息拼接在一起,中间用单个换行符 (\n) 分隔。然后将此完整字符串作为单个系统消息提供在消息的开头。
扩展思考支持
您可以通过添加 thinking 参数来启用扩展思考功能。虽然这可以改善 Claude 在复杂任务中的推理能力,但 OpenAI SDK 不会返回 Claude 的详细思考过程。如需完整的扩展思考功能,包括访问 Claude 的逐步推理输出,请使用原生 Claude API。
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("ANTHROPIC_API_KEY"),
base_url="https://api.anthropic.com/v1/",
)
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}},
)
const response = await openai.chat.completions.create({
messages: [{ role: "user", content: "Who are you?" }],
model: "claude-sonnet-4-6",
// @ts-expect-error
thinking: { type: "enabled", budget_tokens: 2000 }
});
速率限制
速率限制遵循 Anthropic 对 /v1/messages 端点的标准限制。
详细的 OpenAI 兼容 API 支持
请求字段
简单字段
| 字段 | 支持状态 |
|---|---|
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 字段
Show fields
messages 数组字段
Show fields
messages[n].role == "developer" 的字段
Developer 消息会被提升到对话开头,作为初始系统消息的一部分
| 字段 | 支持状态 |
|---|---|
content | 完全支持,但会被提升 |
name | 忽略 |
messages[n].role == "system" 的字段
System 消息会被提升到对话开头,作为初始系统消息的一部分
| 字段 | 支持状态 |
|---|---|
content | 完全支持,但会被提升 |
name | 忽略 |
messages[n].role == "user" 的字段
| 字段 | 变体 | 子字段 | 支持状态 |
|---|---|---|---|
content | string | 完全支持 | |
array, type == "text" | 完全支持 | ||
array, type == "image_url" | url | 完全支持 | |
detail | 忽略 | ||
array, type == "input_audio" | 忽略 | ||
array, type == "file" | 忽略 | ||
name | 忽略 |
messages[n].role == "assistant" 的字段
| 字段 | 变体 | 支持状态 |
|---|---|---|
content | string | 完全支持 |
array, type == "text" | 完全支持 | |
array, type == "refusal" | 忽略 | |
tool_calls | 完全支持 | |
function_call | 完全支持 | |
audio | 忽略 | |
refusal | 忽略 |
messages[n].role == "tool" 的字段
| 字段 | 变体 | 支持状态 |
|---|---|---|
content | string | 完全支持 |
array, type == "text" | 完全支持 | |
tool_call_id | 完全支持 | |
tool_choice | 完全支持 | |
name | 忽略 |
messages[n].role == "function" 的字段
| 字段 | 变体 | 支持状态 |
|---|---|---|
content | string | 完全支持 |
array, type == "text" | 完全支持 | |
tool_choice | 完全支持 | |
name | 忽略 |
响应字段
| 字段 | 支持状态 |
|---|---|
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 | 始终为空 |