1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Anthropic → `OpenAI` Responses API request transform.
//!
//! Converts Anthropic Messages API requests into `OpenAI` Responses API requests
//! by mapping message structure, tools, and configuration fields.
#![allow(clippy::too_many_lines)]
use bytes::Bytes;
use serde_json::json;
use super::{
anthropic_to_openai::{AnthropicBody, AnthropicToolChoice, parse_anthropic_body},
response_transforms::extract_text_from_content,
};
use crate::model::{TransformError, TransformRequest, TransformResponse};
/// Transform an Anthropic Messages request to an `OpenAI` Responses API request.
///
/// # Errors
///
/// Returns `TransformError::InvalidFormat` if the request body cannot be parsed
/// as Anthropic JSON or if content blocks have missing required fields.
pub fn anthropic_to_openai_responses(
req: &TransformRequest,
) -> Result<TransformResponse, TransformError> {
let body: AnthropicBody = parse_anthropic_body(&req.body)?;
// Validate messages array length.
if body.messages.len() > crate::model::MAX_MESSAGES_COUNT {
return Err(TransformError::BufferLimitExceeded(format!(
"messages array length {} exceeds maximum of {}",
body.messages.len(),
crate::model::MAX_MESSAGES_COUNT
)));
}
// Header mapping: x-api-key -> Authorization: Bearer
let mut headers = std::collections::HashMap::new();
if let Some(api_key) = req.headers.get("x-api-key") {
headers.insert("authorization".to_string(), format!("Bearer {api_key}"));
}
headers.insert("content-type".to_string(), "application/json".to_string());
// Path mapping: /v1/messages -> /v1/responses
let path = "/v1/responses".to_string();
// Build the input array for Responses API.
let mut input_items: Vec<serde_json::Value> = Vec::new();
// system -> instructions (top-level field in Responses)
let instructions = body
.system
.as_ref()
.filter(|s| !s.is_empty())
.cloned()
.map(serde_json::Value::String);
// messages -> input items
for msg in &body.messages {
match &msg.content {
None | Some(serde_json::Value::Null) => {
// Skip empty content blocks — Responses API needs meaningful input.
}
Some(serde_json::Value::String(s)) => {
// Plain string content -> single message input item.
input_items.push(json!({
"type": "message",
"role": msg.role,
"content": [{"type": "input_text", "text": s}],
}));
}
Some(serde_json::Value::Array(blocks)) => {
let mut text_parts = Vec::new();
let mut tool_calls: Vec<serde_json::Value> = Vec::new();
let mut tool_result_items: Vec<serde_json::Value> = Vec::new();
for block in blocks {
let block_type =
block.get("type").and_then(|v| v.as_str()).ok_or_else(|| {
TransformError::MissingRequiredField("content block 'type'".to_string())
})?;
match block_type {
"text" => {
let text =
block.get("text").and_then(|v| v.as_str()).ok_or_else(|| {
TransformError::MissingRequiredField(
"text block 'text' field".to_string(),
)
})?;
text_parts.push(serde_json::Value::Object(serde_json::Map::from_iter(
[
(
"type".to_string(),
serde_json::Value::String("input_text".to_string()),
),
(
"text".to_string(),
serde_json::Value::String(text.to_string()),
),
],
)));
}
"tool_use" => {
let id = block.get("id").and_then(|v| v.as_str()).ok_or_else(|| {
TransformError::MissingRequiredField(
"tool_use block 'id' field".to_string(),
)
})?;
let name =
block.get("name").and_then(|v| v.as_str()).ok_or_else(|| {
TransformError::MissingRequiredField(
"tool_use block 'name' field".to_string(),
)
})?;
let input = block
.get("input")
.cloned()
.unwrap_or(serde_json::Value::Object(serde_json::Map::new()));
tool_calls.push(json!({
"type": "function_call",
"call_id": id,
"name": name,
"arguments": serde_json::to_string(&input).map_err(|e| {
TransformError::InvalidFormat(format!("tool_use input serialization: {e}"))
})?,
}));
}
"tool_result" => {
let tool_use_id = block
.get("tool_use_id")
.and_then(|v| v.as_str())
.ok_or_else(|| {
TransformError::MissingRequiredField(
"tool_result block 'tool_use_id' field".to_string(),
)
})?;
let content = block
.get("content")
.cloned()
.unwrap_or(serde_json::Value::String(String::new()));
let text = extract_text_from_content(&content);
tool_result_items.push(json!({
"type": "function_call_output",
"call_id": tool_use_id,
"output": text,
}));
}
"thinking" => {
tracing::debug!(
"lossy downgrade: skipping Anthropic thinking block in Responses \
transform"
);
}
"image" => {
tracing::debug!(
"lossy downgrade: skipping image content block in Responses \
transform"
);
}
other => {
tracing::debug!(
"lossy downgrade: skipping unsupported Anthropic content block \
type '{other}' in Responses transform"
);
}
}
}
let has_text = !text_parts.is_empty();
let has_tool_calls = !tool_calls.is_empty();
let has_tool_results = !tool_result_items.is_empty();
let had_content = has_text || has_tool_calls || has_tool_results;
// If we have text, emit a message input item.
if has_text {
input_items.push(json!({
"type": "message",
"role": msg.role,
"content": text_parts,
}));
}
// If we have tool_calls, emit function_call items (separate from message).
for tc in tool_calls {
input_items.push(tc);
}
// Tool results go directly into input.
input_items.extend(tool_result_items);
// If all blocks were lossy-downgraded (thinking/image/unknown), emit a placeholder
// so the message is not silently dropped.
if !had_content {
tracing::debug!(
"lossy downgrade: message had only thinking/image/unknown blocks, \
emitting placeholder"
);
input_items.push(json!({
"type": "message",
"role": msg.role,
"content": [{"type": "input_text", "text": ""}],
}));
}
}
other => {
return Err(TransformError::InvalidFormat(format!(
"unexpected content type: {other:?}"
)));
}
}
}
let mut body_obj = serde_json::Map::new();
body_obj.insert("model".to_string(), serde_json::Value::String(body.model));
body_obj.insert("input".to_string(), serde_json::Value::Array(input_items));
if let Some(instructions) = instructions {
body_obj.insert("instructions".to_string(), instructions);
}
if let Some(max_tokens) = body.max_tokens {
body_obj.insert(
"max_output_tokens".to_string(),
serde_json::Value::Number(serde_json::Number::from(max_tokens)),
);
}
if let Some(temperature) = body.temperature {
body_obj.insert("temperature".to_string(), json!(temperature));
}
if let Some(top_p) = body.top_p {
body_obj.insert("top_p".to_string(), json!(top_p));
}
if let Some(ref stop) = body.stop_sequences {
body_obj.insert("stop".to_string(), json!(stop));
}
if let Some(stream) = body.stream {
body_obj.insert("stream".to_string(), serde_json::Value::Bool(stream));
}
// Anthropic thinking config: Responses API doesn't have an equivalent, so lossy downgrade.
if body.thinking.is_some() {
tracing::debug!(
"lossy downgrade: skipping Anthropic thinking config in Responses transform"
);
}
if let Some(ref tools) = body.tools {
let responses_tools: Vec<_> = tools
.iter()
.map(|tool| -> serde_json::Value {
let mut obj = serde_json::Map::new();
obj.insert(
"type".to_string(),
serde_json::Value::String("function".to_string()),
);
obj.insert(
"name".to_string(),
serde_json::Value::String(tool.name.clone()),
);
if let Some(ref description) = tool.description {
obj.insert(
"description".to_string(),
serde_json::Value::String(description.clone()),
);
}
if let Some(ref parameters) = tool.input_schema {
obj.insert("parameters".to_string(), parameters.clone());
}
serde_json::Value::Object(obj)
})
.collect::<Vec<_>>();
body_obj.insert(
"tools".to_string(),
serde_json::Value::Array(responses_tools),
);
}
if let Some(ref tool_choice) = body.tool_choice {
let responses_tool_choice = anthropic_tool_choice_to_responses(tool_choice)?;
body_obj.insert("tool_choice".to_string(), responses_tool_choice);
}
let body_bytes = serde_json::to_vec(&serde_json::Value::Object(body_obj))
.map_err(|e| TransformError::InvalidFormat(format!("response serialization: {e}")))?;
Ok(TransformResponse {
headers,
path,
body: Bytes::from(body_bytes),
})
}
/// Map Anthropic `tool_choice` to Responses API `tool_choice` format.
fn anthropic_tool_choice_to_responses(
choice: &AnthropicToolChoice,
) -> Result<serde_json::Value, TransformError> {
match choice.choice_type.as_str() {
"auto" => Ok(serde_json::Value::String("auto".to_string())),
"none" => Ok(serde_json::Value::String("none".to_string())),
"any" => Ok(serde_json::Value::String("required".to_string())),
"tool" => {
let name = choice.name.as_ref().ok_or_else(|| {
TransformError::MissingRequiredField("tool_choice.name for type 'tool'".to_string())
})?;
Ok(json!({
"type": "function",
"name": name,
}))
}
other => Err(TransformError::InvalidFormat(format!(
"unsupported Anthropic tool_choice type: {other}"
))),
}
}