Skip to content

Commit a2dd09f

Browse files
committed
server: fix reasoning item content format handling for multi-turn
Accept all valid reasoning item content formats in multi-turn input: - Array of objects: [{"type":"reasoning_text","text":"..."}] (spec format) - Plain string: "thinking about it" (OpenCode format) - Null: content:null with encrypted_content (Codex, openai/codex#11834) - Omitted entirely: no content field present Previously threw "item['content'] is not an array" for non-array formats, breaking OpenCode multi-turn conversations. The encrypted_content field is accepted but ignored for local models (no server-side decryption). Add 4 tests covering each format variant. Refs: openai/codex#11834, anomalyco/opencode#19081
1 parent 1aa7cf5 commit a2dd09f

2 files changed

Lines changed: 98 additions & 10 deletions

File tree

‎tools/server/server-chat.cpp‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,29 @@ json server_chat_convert_responses_to_chatcmpl(const json & response_body) {
238238
item.at("type") == "reasoning") {
239239
// #responses_create-input-input_item_list-item-reasoning
240240

241-
if (!exists_and_is_array(item, "content")) {
242-
throw std::invalid_argument("item['content'] is not an array");
243-
}
244-
if (item.at("content").empty()) {
245-
throw std::invalid_argument("item['content'] is empty");
246-
}
247-
if (!exists_and_is_string(item.at("content")[0], "text")) {
248-
throw std::invalid_argument("item['content']['text'] is not a string");
241+
// content can be: null, omitted, a string, or array of {type, text} objects.
242+
// Codex may send content:null or omit it entirely (issue openai/codex#11834).
243+
// OpenCode may send content as a plain string.
244+
// The spec uses array format: [{"type":"reasoning_text","text":"..."}].
245+
// encrypted_content (opaque string) is accepted but ignored for local models.
246+
std::string reasoning_text;
247+
if (!item.contains("content") || item.at("content").is_null()) {
248+
// null or missing content - skip (encrypted_content only, or empty reasoning)
249+
} else if (item.at("content").is_string()) {
250+
reasoning_text = item.at("content").get<std::string>();
251+
} else if (item.at("content").is_array() && !item.at("content").empty()
252+
&& exists_and_is_string(item.at("content")[0], "text")) {
253+
reasoning_text = item.at("content")[0].at("text").get<std::string>();
249254
}
250255

251256
if (merge_prev) {
252257
auto & prev_msg = chatcmpl_messages.back();
253-
prev_msg["reasoning_content"] = item.at("content")[0].at("text");
258+
prev_msg["reasoning_content"] = reasoning_text;
254259
} else {
255260
chatcmpl_messages.push_back(json {
256261
{"role", "assistant"},
257262
{"content", json::array()},
258-
{"reasoning_content", item.at("content")[0].at("text")},
263+
{"reasoning_content", reasoning_text},
259264
});
260265
}
261266
} else {

‎tools/server/tests/unit/test_compat_oai_responses.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,86 @@ def test_responses_stream_delta_events_have_indices():
509509
assert saw_output_item_added, "never received response.output_item.added"
510510
assert saw_content_part_added, "never received response.content_part.added"
511511
assert saw_output_text_delta, "never received response.output_text.delta"
512+
513+
514+
def test_responses_reasoning_content_array():
515+
"""Reasoning items with content as array (spec format) must be accepted."""
516+
global server
517+
server.start()
518+
res = server.make_request("POST", "/v1/responses", data={
519+
"model": "gpt-4.1",
520+
"input": [
521+
{"role": "user", "content": [{"type": "input_text", "text": "Hi"}]},
522+
{"type": "reasoning", "summary": [],
523+
"content": [{"type": "reasoning_text", "text": "thinking"}]},
524+
{"role": "assistant", "type": "message",
525+
"content": [{"type": "output_text", "text": "Hello"}]},
526+
{"role": "user", "content": [{"type": "input_text", "text": "How are you"}]},
527+
],
528+
"max_output_tokens": 8,
529+
"temperature": 0.8,
530+
})
531+
assert res.status_code == 200
532+
assert res.body["status"] == "completed"
533+
534+
535+
def test_responses_reasoning_content_string():
536+
"""Reasoning items with content as plain string (OpenCode format) must be accepted."""
537+
global server
538+
server.start()
539+
res = server.make_request("POST", "/v1/responses", data={
540+
"model": "gpt-4.1",
541+
"input": [
542+
{"role": "user", "content": [{"type": "input_text", "text": "Hi"}]},
543+
{"type": "reasoning", "summary": [], "content": "thinking about it"},
544+
{"role": "assistant", "type": "message",
545+
"content": [{"type": "output_text", "text": "Hello"}]},
546+
{"role": "user", "content": [{"type": "input_text", "text": "How are you"}]},
547+
],
548+
"max_output_tokens": 8,
549+
"temperature": 0.8,
550+
})
551+
assert res.status_code == 200
552+
assert res.body["status"] == "completed"
553+
554+
555+
def test_responses_reasoning_content_null():
556+
"""Reasoning items with content:null (Codex format, issue openai/codex#11834)
557+
must be accepted — content may be null when encrypted_content is present."""
558+
global server
559+
server.start()
560+
res = server.make_request("POST", "/v1/responses", data={
561+
"model": "gpt-4.1",
562+
"input": [
563+
{"role": "user", "content": [{"type": "input_text", "text": "Hi"}]},
564+
{"type": "reasoning", "summary": [], "content": None,
565+
"encrypted_content": "opaque_data_here"},
566+
{"role": "assistant", "type": "message",
567+
"content": [{"type": "output_text", "text": "Hello"}]},
568+
{"role": "user", "content": [{"type": "input_text", "text": "How are you"}]},
569+
],
570+
"max_output_tokens": 8,
571+
"temperature": 0.8,
572+
})
573+
assert res.status_code == 200
574+
assert res.body["status"] == "completed"
575+
576+
577+
def test_responses_reasoning_content_omitted():
578+
"""Reasoning items with content omitted entirely must be accepted."""
579+
global server
580+
server.start()
581+
res = server.make_request("POST", "/v1/responses", data={
582+
"model": "gpt-4.1",
583+
"input": [
584+
{"role": "user", "content": [{"type": "input_text", "text": "Hi"}]},
585+
{"type": "reasoning", "summary": []},
586+
{"role": "assistant", "type": "message",
587+
"content": [{"type": "output_text", "text": "Hello"}]},
588+
{"role": "user", "content": [{"type": "input_text", "text": "How are you"}]},
589+
],
590+
"max_output_tokens": 8,
591+
"temperature": 0.8,
592+
})
593+
assert res.status_code == 200
594+
assert res.body["status"] == "completed"

0 commit comments

Comments
 (0)