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
//! Request transformation helpers for the OpenAI Responses API adapter.
//!
//! These functions convert an internal Chat Completions payload into the
//! format expected by the OpenAI Responses API.
use serde_json::{Value, json};
use super::{OpenAiAdapter, REASONING_PREFIXES};
impl OpenAiAdapter {
/// Check if the model is a reasoning model (o1/o3).
pub(super) fn is_reasoning_model(payload: &Value) -> bool {
payload
.get("model")
.and_then(|m| m.as_str())
.map(|model| {
REASONING_PREFIXES
.iter()
.any(|prefix| model.starts_with(prefix))
})
.unwrap_or(false)
}
/// Convert messages array to Responses API `input` items and optional `instructions`.
pub(super) fn convert_messages(messages: &[Value]) -> (Option<String>, Vec<Value>) {
let mut instructions: Option<String> = None;
let mut input_items: Vec<Value> = Vec::new();
for msg in messages {
let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
match role {
"system" => {
instructions = msg
.get("content")
.and_then(|c| c.as_str())
.map(String::from);
}
"user" => {
let content = msg.get("content").cloned().unwrap_or(json!(""));
input_items.push(json!({
"type": "message",
"role": "user",
"content": Self::convert_content_blocks(&content),
}));
}
"assistant" => {
// Text content → message item
if let Some(content) = msg.get("content")
&& content.is_string()
&& !content.as_str().unwrap_or("").is_empty()
{
input_items.push(json!({
"type": "message",
"role": "assistant",
"content": content,
}));
}
// Tool calls → function_call items
if let Some(tool_calls) = msg.get("tool_calls").and_then(|tc| tc.as_array()) {
for tc in tool_calls {
let func = tc.get("function").cloned().unwrap_or(json!({}));
input_items.push(json!({
"type": "function_call",
"call_id": tc.get("id").and_then(|i| i.as_str()).unwrap_or(""),
"name": func.get("name").and_then(|n| n.as_str()).unwrap_or(""),
"arguments": func.get("arguments").and_then(|a| a.as_str()).unwrap_or("{}"),
}));
}
}
}
"tool" => {
input_items.push(json!({
"type": "function_call_output",
"call_id": msg.get("tool_call_id").and_then(|i| i.as_str()).unwrap_or(""),
"output": msg.get("content").and_then(|c| c.as_str()).unwrap_or(""),
}));
}
_ => {}
}
}
(instructions, input_items)
}
/// Convert content blocks from internal (Anthropic-like) format to Responses API format.
///
/// - `{"type": "text", ...}` → `{"type": "input_text", ...}`
/// - `{"type": "image", "source": {...}}` → `{"type": "input_image", "image_url": "data:...;base64,..."}`
///
/// If content is a plain string, it is returned unchanged.
pub(super) fn convert_content_blocks(content: &Value) -> Value {
match content {
Value::String(_) => content.clone(),
Value::Array(blocks) => {
let converted: Vec<Value> = blocks
.iter()
.map(|block| {
let block_type = block.get("type").and_then(|t| t.as_str()).unwrap_or("");
match block_type {
"text" => {
json!({
"type": "input_text",
"text": block.get("text").and_then(|t| t.as_str()).unwrap_or(""),
})
}
"image" => {
let source = block.get("source").cloned().unwrap_or(json!({}));
let media_type = source
.get("media_type")
.and_then(|m| m.as_str())
.unwrap_or("image/png");
let data = source
.get("data")
.and_then(|d| d.as_str())
.unwrap_or("");
json!({
"type": "input_image",
"image_url": format!("data:{media_type};base64,{data}"),
})
}
_ => block.clone(),
}
})
.collect();
Value::Array(converted)
}
_ => content.clone(),
}
}
/// Flatten Chat Completions tool definitions to Responses API format.
///
/// `{type: "function", function: {name, description, parameters}}`
/// → `{type: "function", name, description, parameters}`
pub(super) fn convert_tools(tools: &[Value]) -> Vec<Value> {
tools
.iter()
.filter_map(|tool| {
if tool.get("type").and_then(|t| t.as_str()) == Some("function") {
let func = tool.get("function")?;
Some(json!({
"type": "function",
"name": func.get("name").and_then(|n| n.as_str()).unwrap_or(""),
"description": func.get("description").and_then(|d| d.as_str()).unwrap_or(""),
"parameters": func.get("parameters").cloned().unwrap_or(json!({})),
}))
} else {
None
}
})
.collect()
}
}