From 1d5a69a445619310b7fb6f1d34359ba8eacfc4fd Mon Sep 17 00:00:00 2001 From: pradeep7127 Date: Tue, 7 Apr 2026 17:41:34 -0700 Subject: [PATCH] fix(api_server): preserve conversation history when /v1/runs input is a message array When /v1/runs receives an OpenAI-style array of messages as input, all messages except the last user turn are now extracted as conversation_history. Previously only the last message was kept, silently discarding earlier context in multi-turn conversations. Handles multi-part content blocks by flattening text portions. Only fires when no explicit conversation_history was provided. Based on PR #5837 by pradeep7127. --- gateway/platforms/api_server.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py index 241df3a6d1..aafc1579af 100644 --- a/gateway/platforms/api_server.py +++ b/gateway/platforms/api_server.py @@ -1454,6 +1454,21 @@ class APIServerAdapter(BasePlatformAdapter): if instructions is None: instructions = stored.get("instructions") + # When input is a multi-message array, extract all but the last + # message as conversation history (the last becomes user_message). + # Only fires when no explicit history was provided. + if not conversation_history and isinstance(raw_input, list) and len(raw_input) > 1: + for msg in raw_input[:-1]: + if isinstance(msg, dict) and msg.get("role") and msg.get("content"): + content = msg["content"] + if isinstance(content, list): + # Flatten multi-part content blocks to text + content = " ".join( + part.get("text", "") for part in content + if isinstance(part, dict) and part.get("type") == "text" + ) + conversation_history.append({"role": msg["role"], "content": str(content)}) + session_id = body.get("session_id") or run_id ephemeral_system_prompt = instructions