Openai大模型消息格式 vs Anthropic消息格式
b1babo
2026年4月18日
2026年4月18日
OpenAI vs Anthropic 消息格式完整对比
本文档详细对比 OpenAI 和 Anthropic 两大 API 的消息格式差异,涵盖用户请求、模型响应、工具调用等完整流程。
目录
1. 基础格式对比
1.1 用户请求
OpenAI 格式
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "What's the weather in Tokyo?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
}
]
}Anthropic 格式
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "You are a helpful assistant.",
"messages": [
{
"role": "user",
"content": "What's the weather in Tokyo?"
}
],
"tools": [
{
"name": "get_weather",
"description": "Get weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
]
}区别:
- Anthropic 的
system是独立字段,不在 messages 数组中 - Anthropic 工具定义使用
input_schema而非嵌套的function.parameters - Anthropic 需要显式指定
max_tokens
1.2 大模型响应
OpenAI 格式
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Let me check the weather for you."
}
}
]
}Anthropic 格式
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Let me check the weather for you."
}
]
}区别:
- Anthropic 响应结构更扁平,
role和content在顶层 - Anthropic 的
content始终是数组,每项都有type字段
1.3 Tool Call 请求
OpenAI 格式
{
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Tokyo\"}"
}
}
]
}
}
]
}Anthropic 格式
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "toolu_abc123",
"name": "get_weather",
"input": {"location": "Tokyo"}
}
],
"stop_reason": "tool_use"
}区别:
| 特性 | OpenAI | Anthropic |
|---|---|---|
| 字段名 | tool_calls | content 数组中的 tool_use |
| 参数格式 | JSON 字符串 | JSON 对象 |
| 参数字段 | function.arguments | input |
| ID 前缀 | call_ | toolu_ |
1.4 Tool 返回结果
OpenAI 格式
{
"messages": [
{
"role": "assistant",
"tool_calls": [
{"id": "call_abc123", "type": "function", "function": {...}}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"temperature\": 22, \"condition\": \"sunny\"}"
}
]
}Anthropic 格式
{
"messages": [
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": "toolu_abc123", "name": "get_weather", "input": {...}}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_abc123",
"content": "{\"temperature\": 22, \"condition\": \"sunny\"}"
}
]
}
]
}区别:
| 特性 | OpenAI | Anthropic |
|---|---|---|
| role | tool | user |
| 字段 | tool_call_id | content[].tool_use_id |
| content | 字符串 | 可为字符串或对象数组(支持多部分结果) |
2. 单工具调用完整示例
场景说明
用户问:"现在几点了?" → 大模型调用 get_current_time 工具 → 返回结果给用户
2.1 OpenAI 格式
请求
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "你是一个乐于助人的助手。"
},
{
"role": "user",
"content": "现在几点了?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "获取当前时间",
"parameters": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "时区,例如 Asia/Shanghai"
}
}
}
}
}
]
}响应(需要调用工具)
{
"id": "chatcmpl-abc123xyz",
"object": "chat.completion",
"created": 1716134400,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc487def",
"type": "function",
"function": {
"name": "get_current_time",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 50,
"completion_tokens": 20,
"total_tokens": 70
}
}工具返回(发回给大模型)
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "你是一个乐于助人的助手。"
},
{
"role": "user",
"content": "现在几点了?"
},
{
"role": "assistant",
"tool_calls": [
{
"id": "call_abc487def",
"type": "function",
"function": {
"name": "get_current_time",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc487def",
"content": "{\"time\": \"2026-04-19 14:30:25\", \"timezone\": \"Asia/Shanghai\"}"
}
]
}最终回复
{
"id": "chatcmpl-def456uvw",
"object": "chat.completion",
"created": 1716134405,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "现在是 2026年4月19日 14:30:25(上海时间)。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 95,
"completion_tokens": 25,
"total_tokens": 120
}
}2.2 Anthropic 格式
请求
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "你是一个乐于助人的助手。",
"messages": [
{
"role": "user",
"content": "现在几点了?"
}
],
"tools": [
{
"name": "get_current_time",
"description": "获取当前时间",
"input_schema": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "时区,例如 Asia/Shanghai"
}
}
}
}
]
}响应(需要调用工具)
{
"id": "msg_abc123xyz",
"type": "message",
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "toolu_abc487def",
"name": "get_current_time",
"input": {
"timezone": "Asia/Shanghai"
}
}
],
"stop_reason": "tool_use",
"model": "claude-sonnet-4-6",
"usage": {
"input_tokens": 320,
"output_tokens": 45
}
}工具返回(发回给大模型)
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "你是一个乐于助人的助手。",
"messages": [
{
"role": "user",
"content": "现在几点了?"
},
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"id": "toolu_abc487def",
"name": "get_current_time",
"input": {
"timezone": "Asia/Shanghai"
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_abc487def",
"content": "{\"time\": \"2026-04-19 14:30:25\", \"timezone\": \"Asia/Shanghai\"}"
}
]
}
]
}最终回复
{
"id": "msg_def456uvw",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "现在是 2026年4月19日 14:30:25(上海时间)。"
}
],
"stop_reason": "end_turn",
"model": "claude-sonnet-4-6",
"usage": {
"input_tokens": 395,
"output_tokens": 35
}
}3. 多工具调用完整示例
场景说明
用户问:"告诉我北京的天气和现在几点" → 大模型同时调用两个工具
3.1 OpenAI 格式
请求
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "你是一个乐于助人的助手。"
},
{
"role": "user",
"content": "告诉我北京的天气和现在几点"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
},
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "获取当前时间",
"parameters": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "时区"
}
}
}
}
}
]
}响应(多个工具调用)
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1716134400,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "我来帮你查询北京的天气和当前时间。",
"tool_calls": [
{
"id": "call_abc001",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
},
{
"id": "call_abc002",
"type": "function",
"function": {
"name": "get_current_time",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 150,
"completion_tokens": 85,
"total_tokens": 235
}
}工具返回(发回给大模型)
{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "你是一个乐于助人的助手。"
},
{
"role": "user",
"content": "告诉我北京的天气和现在几点"
},
{
"role": "assistant",
"content": "我来帮你查询北京的天气和当前时间。",
"tool_calls": [
{
"id": "call_abc001",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
},
{
"id": "call_abc002",
"type": "function",
"function": {
"name": "get_current_time",
"arguments": "{\"timezone\": \"Asia/Shanghai\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc001",
"content": "{\"city\": \"北京\", \"temperature\": 22, \"condition\": \"晴天\", \"humidity\": 45}"
},
{
"role": "tool",
"tool_call_id": "call_abc002",
"content": "{\"time\": \"2026-04-19 14:30:25\", \"timezone\": \"Asia/Shanghai\"}"
}
]
}最终回复
{
"id": "chatcmpl-def456",
"object": "chat.completion",
"created": 1716134405,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "根据查询结果:\n\n🌤️ **北京天气**:温度 22°C,晴天,湿度 45%\n\n🕐 **当前时间**:2026年4月19日 14:30:25(上海时间)"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 280,
"completion_tokens": 65,
"total_tokens": 345
}
}3.2 Anthropic 格式
请求
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "你是一个乐于助人的助手。",
"messages": [
{
"role": "user",
"content": "告诉我北京的天气和现在几点"
}
],
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
},
{
"name": "get_current_time",
"description": "获取当前时间",
"input_schema": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "时区"
}
}
}
}
]
}响应(多个工具调用)
{
"id": "msg_abc123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "我来帮你查询北京的天气和当前时间。"
},
{
"type": "tool_use",
"id": "toolu_abc001",
"name": "get_weather",
"input": {
"city": "北京"
}
},
{
"type": "tool_use",
"id": "toolu_abc002",
"name": "get_current_time",
"input": {
"timezone": "Asia/Shanghai"
}
}
],
"stop_reason": "tool_use",
"model": "claude-sonnet-4-6",
"usage": {
"input_tokens": 380,
"output_tokens": 95
}
}工具返回(发回给大模型)
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"system": "你是一个乐于助人的助手。",
"messages": [
{
"role": "user",
"content": "告诉我北京的天气和现在几点"
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "我来帮你查询北京的天气和当前时间。"
},
{
"type": "tool_use",
"id": "toolu_abc001",
"name": "get_weather",
"input": {"city": "北京"}
},
{
"type": "tool_use",
"id": "toolu_abc002",
"name": "get_current_time",
"input": {"timezone": "Asia/Shanghai"}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_abc001",
"content": "{\"city\": \"北京\", \"temperature\": 22, \"condition\": \"晴天\", \"humidity\": 45}"
},
{
"type": "tool_result",
"tool_use_id": "toolu_abc002",
"content": "{\"time\": \"2026-04-19 14:30:25\", \"timezone\": \"Asia/Shanghai\"}"
}
]
}
]
}最终回复
{
"id": "msg_def456",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "根据查询结果:\n\n🌤️ **北京天气**:温度 22°C,晴天,湿度 45%\n\n🕐 **当前时间**:2026年4月19日 14:30:25(上海时间)"
}
],
"stop_reason": "end_turn",
"model": "claude-sonnet-4-6",
"usage": {
"input_tokens": 520,
"output_tokens": 75
}
}3.3 Multi-Tool Call 关键区别
| 特性 | OpenAI | Anthropic |
|---|---|---|
| 多工具调用位置 | message.tool_calls[] 数组 | content[] 数组,每个 type: "tool_use" |
| 可混合文本 | message.content + tool_calls | content[] 中混合 type: "text" 和 type: "tool_use" |
| 工具结果 | 每个工具一条消息,role: "tool" | 一条消息,content[] 数组包含多个 type: "tool_result" |
| 关联方式 | tool_call_id 匹配 | tool_use_id 匹配 |
4. 流程图
4.1 完整交互时序图
sequenceDiagram
autonumber
participant User as 👤 用户
participant Client as 💻 客户端代码
participant API as 🤖 LLM API
participant Tools as 🔧 工具服务
rect rgb(230, 240, 255)
Note left of Client: 📤 第一步:发送用户请求
Client->>API: Request 1<br/>messages: [user]<br/>tools: [get_weather, get_time]
end
rect rgb(255, 245, 230)
Note left of API: 📥 第二步:API 返回多个工具调用
API-->>Client: Response 1<br/>assistant + tool_calls
Note over API,Client: OpenAI: message.tool_calls[]<br/>Anthropic: content[type=tool_use][]
end
rect rgb(240, 255, 240)
Note left of Client: 🔧 第三步:客户端并行执行工具
par 并行执行
Client->>Tools: get_weather("北京")
Client->>Tools: get_time("Asia/Shanghai")
end
Tools-->>Client: 返回结果
end
rect rgb(255, 240, 245)
Note left of Client: 📤 第四步:发送工具结果
Client->>API: Request 2<br/>messages: [user, assistant, tool_results]
Note over Client,API: OpenAI: 多条 role="tool" 消息<br/>Anthropic: 一条 content 含多个 tool_result
end
rect rgb(230, 255, 255)
Note left of API: 📥 第五步:API 返回最终回复
API-->>Client: Response 2<br/>assistant text
Client-->>User: 显示结果
end4.2 数据结构转换流程图
graph LR
subgraph OpenAI ["OpenAI 格式"]
A1[用户请求] --> B1[API 响应<br/>tool_calls 数组]
B1 --> C1[工具结果 1<br/>role: "tool"]
B1 --> C2[工具结果 2<br/>role: "tool"]
C1 & C2 --> D1[第二轮请求]
D1 --> E1[最终回复]
end
subgraph Anthropic ["Anthropic 格式"]
A2[用户请求] --> B2[API 响应<br/>content 数组]
B2 --> C3[工具结果<br/>content 数组内多个<br/>type: "tool_result"]
C3 --> D2[第二轮请求]
D2 --> E2[最终回复]
end
style OpenAI fill:#e1f5fe
style Anthropic fill:#f3e5f54.3 JSON 结构对比流程图
┌─────────────────────────────────────────────────────────────────────────────┐
│ 多工具调用数据流对比 │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ OPENAI 格式 ANTHROPIC 格式 │
│ │
│ ┌─────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Request 1 │ │ Request 1 │ │
│ │ messages: [ │ │ messages: [{role:"user"}] │ │
│ │ {role:"user"} │ │ tools: [get_weather, get_time] │ │
│ │ ] │ └──────────────┬──────────────────┘ │
│ │ tools: [...] │ │ │
│ └──────────┬──────────┘ ▼ │
│ │ ┌─────────────────────────────────┐ │
│ ▼ │ Response 1 │ │
│ ┌─────────────────────┐ │ content: [ │ │
│ │ Response 1 │ │ {type:"text", ...}, │ │
│ │ message: { │ │ {type:"tool_use", │ │
│ │ tool_calls: [ │◄──────────│ id:"toolu_001", │ │
│ │ {id:"call_001", │ │ name:"get_weather"}, │ │
│ │ name:"weather"}│ │ {type:"tool_use", │ │
│ │ {id:"call_002", │ │ id:"toolu_002", │ │
│ │ name:"time"} │ │ name:"get_time"} │ │
│ │ ] │ │ ] │ │
│ │ } │ │ stop_reason: "tool_use" │ │
│ └──────────┬──────────┘ └──────────────┬──────────────────┘ │
│ │ │ │
│ │ ┌──────────────┐ │ │
│ └──▶ 执行工具 │◄───────────────────┘ │
│ │ call_001 │ │
│ │ call_002 │ │
│ └──────┬───────┘ │
│ ▼ │
│ ┌─────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Request 2 │ │ Request 2 │ │
│ │ messages: [ │ │ messages: [ │ │
│ │ {role:"assistant",│ │ {role:"assistant", │ │
│ │ tool_calls:[...] }│ │ content:[tool_use...]} │ │
│ │ {role:"tool", │ │ {role:"user", │ │
│ │ tool_call_id: │ │ content:[ │ │
│ │ "call_001", │◄──────────│ {type:"tool_result", │ │
│ │ content:"{...}"}, │ │ tool_use_id:"toolu_001"},│ │
│ │ {role:"tool", │ │ {type:"tool_result", │ │
│ │ tool_call_id: │ │ tool_use_id:"toolu_002"} │ │
│ │ "call_002", │ │ ]} │ │
│ │ content:"{...}"} │ │ ] │ │
│ │ ] │ └──────────────┬──────────────────┘ │
│ └──────────┬──────────┘ │ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────┐ ┌─────────────────────────────────┐ │
│ │ Response 2 │ │ Response 2 │ │
│ │ message: { │ │ content: [ │ │
│ │ role: "assistant",│ │ {type:"text", │ │
│ │ content: "最终回复"│◄──────────│ text: "最终回复"} │ │
│ │ } │ │ ] │ │
│ └─────────────────────┘ │ stop_reason: "end_turn" │ │
│ └─────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘4.4 工具返回方式差异
┌─────────────────────────────────────────────────────────────┐
│ 工具返回方式差异 │
├─────────────────────────────────────────────────────────────┤
│ │
│ OpenAI Anthropic │
│ │
│ 多条消息 单条消息 │
│ ┌─────────────────┐ ┌─────────────────────┐ │
│ │ msg 1: tool │ │ role: user │ │
│ │ tool_call_id: │ │ content: [ │ │
│ │ "call_001" │ │ {type:tool_result,│ │
│ └─────────────────┘ │ tool_use_id: │ │
│ │ │ "toolu_001"}, │ │
│ ▼ │ {type:tool_result,│ │
│ ┌─────────────────┐ │ tool_use_id: │ │
│ │ msg 2: tool │ │ "toolu_002"} │ │
│ │ tool_call_id: │ │ ] │ │
│ │ "call_002" │ └─────────────────────┘ │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘5. 快速参考
5.1 核心差异速查表
| 方面 | OpenAI | Anthropic |
|---|---|---|
| 消息结构 | message 对象 | content 数组(统一结构) |
| system 消息 | messages 中 role=system | 顶层 system 字段 |
| tool 参数 | JSON 字符串 | JSON 对象 |
| tool 结果角色 | role="tool" | role="user" + type="tool_result" |
| 停止原因 | finish_reason | stop_reason |
| max_tokens | 可选,有默认值 | 必须指定 |
5.2 字段映射表
| OpenAI | Anthropic | 说明 |
|---|---|---|
messages[].role = "system" | system (顶层字段) | 系统提示词 |
message.tool_calls | content[] (type=tool_use) | 工具调用 |
tool_call.function.arguments | tool_use.input | 工具参数 |
messages[].role = "tool" | role = "user" | 工具结果角色 |
tool_call_id | tool_use_id | 工具调用 ID |
finish_reason | stop_reason | 停止原因 |
5.3 常用值对照
| 场景 | OpenAI | Anthropic |
|---|---|---|
| 正常结束 | finish_reason: "stop" | stop_reason: "end_turn" |
| 需要调用工具 | finish_reason: "tool_calls" | stop_reason: "tool_use" |
| 达到最大长度 | finish_reason: "length" | stop_reason: "max_tokens" |
评论