Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Add compression for OpenAI Responses API (/v1/responses)

chopratejas0007a9f0f489
diff --git a/headroom/proxy/responses_converter.py b/headroom/proxy/responses_converter.py
new file mode 100644
index 000000000..96dd892f5
--- /dev/null
+++ b/headroom/proxy/responses_converter.py
@@ -0,0 +1,266 @@
+"""Convert between OpenAI Responses API items and Chat Completions messages.
+
+The Responses API uses a flat item model where function_call and
+function_call_output are top-level items, content parts use input_text /
+output_text types, and reasoning items must be preserved verbatim.
+
+The Headroom compression pipeline works on Chat Completions messages
+(role + content / tool_calls). This module converts back and forth so
+the existing pipeline can compress Responses API input without changes.
+
+Pattern follows the Gemini converter in server.py (_gemini_contents_to_messages).
+"""
+
+from __future__ import annotations
+
+import copy
+from typing import Any
+
+# Content part types that indicate non-text media (must be preserved, not compressed)
+_NON_TEXT_CONTENT_TYPES = frozenset(
+ {
+ "input_image",
+ "input_file",
+ "input_audio",
+ "image_url",
+ "image_file",
+ }
+)
+
+
+def responses_items_to_messages(
+ items: list[dict[str, Any]],
+) -> tuple[list[dict[str, Any]], list[int]]:
+ """Convert Responses API input items to Chat Completions messages.
+
+ Args:
+ items: The ``input`` array from a ``/v1/responses`` request.
+ Contains a mix of message items, function_call items,
+ function_call_output items, reasoning items, etc.
+
+ Returns:
+ (messages, preserved_indices) where:
+ - messages: OpenAI Chat Completions format messages suitable for
+ the Headroom compression pipeline.
+ - preserved_indices: Indices into *items* for entries that must
+ be restored verbatim (reasoning, images, unknown types).
+ """
+ if not items:
+ return [], []
+
+ messages: list[dict[str, Any]] = []
+ preserved_indices: list[int] = []
+ pending_tool_calls: list[tuple[int, dict[str, Any]]] = []
+
+ for idx, item in enumerate(items):
+ item_type = item.get("type")
+ role = item.get("role")
+
+ # --- Reasoning items: preserve exactly ---
+ if item_type == "reasoning":
+ _flush_pending(messages, pending_tool_calls)
+ preserved_indices.append(idx)
+ continue
+
+ # --- function_call items: accumulate, flush as one assistant message ---
+ if item_type == "function_call":
+ pending_tool_calls.append((idx, item))
+ continue
+
+ # --- function_call_output items: convert to role=tool ---
+ if item_type == "function_call_output":
+ _flush_pending(messages, pending_tool_calls)
+ messages.append(
+ {
+ "role": "tool",
+ "tool_call_id": item.get("call_id", ""),
+ "content": item.get("output", ""),
+ }
+ )
+ continue
+
+ # --- Message items (role-based, with or without type="message") ---
+ if role is not None:
+ _flush_pending(messages, pending_tool_calls)
+ content = item.get("content", "")
+
+ # Handle content part arrays
+ if isinstance(content, list):
+ if _has_non_text_parts(content):
+ preserved_indices.append(idx)
+ continue
+ content = _extract_text_from_parts(content)
+
+ mapped_role = "system" if role == "developer" else role
+ messages.append({"role": mapped_role, "content": content})
+ continue
+
+ # --- Unknown item type: preserve ---
+ _flush_pending(messages, pending_tool_calls)
+ preserved_indices.append(idx)
+
+ # Flush any trailing tool calls
+ _flush_pending(messages, pending_tool_calls)
+
+ return messages, preserved_indices
+
+
+def messages_to_responses_items(
+ messages: list[dict[str, Any]],
+ original_items: list[dict[str, Any]],
+ preserved_indices: list[int],
+) -> list[dict[str, Any]]:
+ """Convert compressed Chat Completions messages back to Responses API items.
+
+ Uses a two-pass approach:
+ 1. Index compressed messages by call_id (for tool outputs) and collect
+ regular messages in order.
+ 2. Walk original_items, restoring preserved items and substituting
+ compressed content where applicable.
+
+ Args:
+ messages: Compressed messages from the pipeline.
+ original_items: The original ``input`` array (pre-compression).
+ preserved_indices: Indices returned by ``responses_items_to_messages``.
+
+ Returns:
+ New items list with compressed content, ready to send to OpenAI.
+ """
+ if not original_items:
+ return []
+
+ preserved_set = frozenset(preserved_indices)
+
+ # --- Pass 1: Index compressed messages ---
+ tool_outputs: dict[str, str] = {} # call_id → compressed output
+ regular_msgs: list[dict[str, Any]] = []
+
+ for msg in messages:
+ role = msg.get("role")
+ if role == "tool":
+ tool_outputs[msg.get("tool_call_id", "")] = msg.get("content", "")
+ elif role == "assistant" and msg.get("tool_calls"):
+ # function_call items pass through uncompressed — skip
+ pass
+ else:
+ regular_msgs.append(msg)
+
+ # --- Pass 2: Reconstruct items ---
+ result: list[dict[str, Any]] = []
+ reg_idx = 0
+
+ for orig_idx, item in enumerate(original_items):
+ # Preserved items go back exactly as they were
+ if orig_idx in preserved_set:
+ result.append(item)
+ continue
+
+ item_type = item.get("type")
+
+ if item_type == "function_call":
+ # Small — pass through unmodified
+ result.append(item)
+
+ elif item_type == "function_call_output":
+ call_id = item.get("call_id", "")
+ compressed = tool_outputs.get(call_id, item.get("output", ""))
+ result.append({**item, "output": compressed})
+
+ else:
+ # Regular message — take next compressed message
+ if reg_idx < len(regular_msgs):
+ msg = regular_msgs[reg_idx]
+ reg_idx += 1
+ result.append(_reconstruct_item(item, msg))
+ else:
+ # Safety: more original items than compressed messages
+ result.append(item)
+
+ return result
+
+
+# ---------------------------------------------------------------------------
+# Helpers
+# ---------------------------------------------------------------------------
+
+
+def _flush_pending(
+ messages: list[dict[str, Any]],
+ pending: list[tuple[int, dict[str, Any]]],
+) -> None:
+ """Flush accumulated function_call items as one assistant message."""
+ if not pending:
+ return
+ tool_calls = []
+ for _idx, item in pending:
+ tool_calls.append(
+ {
+ "id": item.get("call_id", ""),
+ "type": "function",
+ "function": {
+ "name": item.get("name", ""),
+ "arguments": item.get("arguments", "{}"),
+ },
+ }
+ )
+ messages.append(
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": tool_calls,
+ }
+ )
+ pending.clear()
+
+
+def _has_non_text_parts(content: list[dict[str, Any]]) -> bool:
+ """Check if a content array contains non-text parts (images, files, audio)."""
+ return any(p.get("type") in _NON_TEXT_CONTENT_TYPES for p in content)
+
+
+def _extract_text_from_parts(content: list[dict[str, Any]]) -> str:
+ """Extract text from Responses API content parts.
+
+ Handles both input (input_text) and output (output_text) part types,
+ plus standard ``text`` parts.
+ """
+ parts = []
+ for p in content:
+ ptype = p.get("type", "")
+ if ptype in ("input_text", "output_text", "text"):
+ parts.append(p.get("text", ""))
+ return "\n".join(parts) if parts else ""
+
+
+def _reconstruct_item(
+ original: dict[str, Any],
+ compressed_msg: dict[str, Any],
+) -> dict[str, Any]:
+ """Rebuild a Responses API item from its original structure + compressed text.
+
+ Preserves the original content format: if the original had
+ ``content: [{"type": "input_text", ...}]``, the compressed text goes
+ back into that same structure rather than being flattened to a string.
+ """
+ compressed_text = compressed_msg.get("content", "")
+ original_content = original.get("content")
+
+ # If original had a content-part array, reconstruct it
+ if isinstance(original_content, list) and original_content:
+ new_content = []
+ text_replaced = False
+ for part in original_content:
+ ptype = part.get("type", "")
+ if ptype in ("input_text", "output_text", "text") and not text_replaced:
+ new_content.append({**part, "text": compressed_text})
+ text_replaced = True
+ else:
+ new_content.append(part)
+ rebuilt = copy.copy(original)
+ rebuilt["content"] = new_content
+ return rebuilt
+
+ # String content or missing — just replace
+ rebuilt = copy.copy(original)
+ rebuilt["content"] = compressed_text if compressed_text is not None else ""
+ return rebuilt
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 16e249b5f..5ed6fcd8b 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -6365,20 +6365,32 @@ class HeadroomProxy:
model = body.get("model", "unknown")
stream = body.get("stream", False)
- # Convert Responses API input to messages format for optimization
- # The Responses API accepts either a string or array of messages
+ # Convert Responses API input to messages format for optimization.
+ # The Responses API uses a different item model (function_call,
+ # function_call_output, reasoning as top-level items) — we convert to
+ # Chat Completions messages for the pipeline, then convert back.
+ from headroom.proxy.responses_converter import (
+ messages_to_responses_items,
+ responses_items_to_messages,
+ )
+
input_data = body.get("input", "")
instructions = body.get("instructions")
+ previous_response_id = body.get("previous_response_id")
+
+ messages: list[dict[str, Any]] = []
+ original_items: list[dict[str, Any]] | None = None
+ preserved_indices: list[int] = []
- messages = []
if instructions:
messages.append({"role": "system", "content": instructions})
if isinstance(input_data, str):
messages.append({"role": "user", "content": input_data})
elif isinstance(input_data, list):
- # Input is already an array of message objects
- messages.extend(input_data)
+ original_items = input_data
+ converted, preserved_indices = responses_items_to_messages(input_data)
+ messages.extend(converted)
headers = dict(request.headers.items())
headers.pop("host", None)
@@ -6400,12 +6412,60 @@ class HeadroomProxy:
tokenizer = get_tokenizer(model)
original_tokens = tokenizer.count_messages(messages)
- # Note: We pass through to OpenAI without optimization for now
- # The Responses API has different semantics that may not work well with compression
+ # Optimize: convert items → compress → convert back
tokens_saved = 0
transforms_applied: list[str] = []
+ optimized_messages = messages
+ optimized_tokens = original_tokens
+
+ _bypass = (
+ request.headers.get("x-headroom-bypass", "").lower() == "true"
+ or request.headers.get("x-headroom-mode", "").lower() == "passthrough"
+ )
+ _should_compress = (
+ self.config.optimize
+ and original_items is not None
+ and not previous_response_id
+ and not _bypass
+ and len(messages) > 1
+ )
+ _license_ok = self.usage_reporter.should_compress if self.usage_reporter else True
+
+ if _should_compress and _license_ok:
+ try:
+ context_limit = self.openai_provider.get_context_limit(model)
+ result = await asyncio.wait_for(
+ asyncio.to_thread(
+ lambda: self.openai_pipeline.apply(
+ messages=messages,
+ model=model,
+ model_limit=context_limit,
+ context=extract_user_query(messages),
+ )
+ ),
+ timeout=COMPRESSION_TIMEOUT_SECONDS,
+ )
+ if result.messages != messages:
+ optimized_messages = result.messages
+ transforms_applied = result.transforms_applied
+ original_tokens = result.tokens_before
+ optimized_tokens = result.tokens_after
+ except Exception as e:
+ logger.warning(f"[{request_id}] Responses API optimization failed: {e}")
+
+ tokens_saved = max(0, original_tokens - optimized_tokens)
optimization_latency = (time.time() - start_time) * 1000
+ # Convert compressed messages back to Responses API items
+ if optimized_messages is not messages and original_items is not None:
+ opt_msgs = optimized_messages
+ # Strip system message (instructions) — it's separate in Responses API
+ if instructions and opt_msgs and opt_msgs[0].get("role") == "system":
+ body["instructions"] = opt_msgs[0]["content"]
+ opt_msgs = opt_msgs[1:]
+
+ body["input"] = messages_to_responses_items(opt_msgs, original_items, preserved_indices)
+
url = f"{self.OPENAI_API_URL}/v1/responses"
try:
diff --git a/tests/test_proxy_openai_responses_integration.py b/tests/test_proxy_openai_responses_integration.py
index 74f8a8504..4ae97963a 100644
--- a/tests/test_proxy_openai_responses_integration.py
+++ b/tests/test_proxy_openai_responses_integration.py
@@ -219,6 +219,85 @@ class TestOpenAIResponsesCompression:
# At least some tokens should have been saved
assert stats["tokens"]["saved"] >= 0 # May or may not compress depending on size
+ def test_compression_on_function_call_output(self, openai_responses_client, api_key):
+ """Large function_call_output gets compressed (Codex pattern)."""
+ # Create large tool output (simulating Codex file read or shell output)
+ large_output = json.dumps(
+ [{"id": i, "name": f"record_{i}", "value": f"data_{i}" * 10} for i in range(200)]
+ )
+
+ response = openai_responses_client.post(
+ "/v1/responses",
+ headers={"Authorization": f"Bearer {api_key}"},
+ json={
+ "model": "gpt-4o-mini",
+ "input": [
+ {"role": "user", "content": "How many records are in the database?"},
+ {
+ "type": "function_call",
+ "call_id": "call_test_1",
+ "name": "query_database",
+ "arguments": "{}",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_test_1",
+ "output": large_output,
+ },
+ ],
+ },
+ )
+ assert response.status_code == 200
+ data = response.json()
+
+ # Model should be able to answer
+ assert "output" in data
+ assert len(data["output"]) > 0
+
+ # Compression should have saved tokens
+ stats = openai_responses_client.get("/stats").json()
+ assert stats["tokens"]["saved"] > 0
+
+ def test_no_compression_with_string_input(self, openai_responses_client, api_key):
+ """String input (single message) should not crash or compress."""
+ response = openai_responses_client.post(
+ "/v1/responses",
+ headers={"Authorization": f"Bearer {api_key}"},
+ json={"model": "gpt-4o-mini", "input": "What is 1+1?"},
+ )
+ assert response.status_code == 200
+
+ def test_bypass_header_skips_compression(self, openai_responses_client, api_key):
+ """x-headroom-bypass header skips compression."""
+ items = [
+ {"id": i, "name": f"Item {i}", "desc": f"Description for item {i}"} for i in range(100)
+ ]
+ tool_output = json.dumps(items)
+
+ # Reset stats first
+ openai_responses_client.post("/stats/reset")
+
+ response = openai_responses_client.post(
+ "/v1/responses",
+ headers={
+ "Authorization": f"Bearer {api_key}",
+ "x-headroom-bypass": "true",
+ },
+ json={
+ "model": "gpt-4o-mini",
+ "input": [
+ {"role": "user", "content": "Get items"},
+ {"role": "assistant", "content": f"Results:\n{tool_output}"},
+ {"role": "user", "content": "How many?"},
+ ],
+ },
+ )
+ assert response.status_code == 200
+
+ stats = openai_responses_client.get("/stats").json()
+ # With bypass, no tokens should be saved
+ assert stats["tokens"]["saved"] == 0
+
class TestOpenAIResponsesStats:
"""Test that proxy stats track /v1/responses requests correctly."""
diff --git a/tests/test_responses_converter.py b/tests/test_responses_converter.py
new file mode 100644
index 000000000..bebf5a1fb
--- /dev/null
+++ b/tests/test_responses_converter.py
@@ -0,0 +1,408 @@
+"""Tests for OpenAI Responses API ↔ Chat Completions message conversion.
+
+Tests cover:
+1. Forward conversion (Responses items → Chat Completions messages)
+2. Reverse conversion (compressed messages → Responses items)
+3. Round-trip fidelity (convert → compress → convert back)
+4. Edge cases (empty input, unknown types, mixed ordering)
+"""
+
+from __future__ import annotations
+
+import json
+
+from headroom.proxy.responses_converter import (
+ messages_to_responses_items,
+ responses_items_to_messages,
+)
+
+# =============================================================================
+# Forward conversion: responses_items_to_messages
+# =============================================================================
+
+
+class TestItemsToMessages:
+ """Test converting Responses API items to Chat Completions messages."""
+
+ def test_simple_user_message(self):
+ """String content user message passes through."""
+ items = [{"role": "user", "content": "Hello"}]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ assert messages[0] == {"role": "user", "content": "Hello"}
+ assert preserved == []
+
+ def test_content_array_input_text(self):
+ """input_text content parts are extracted to plain text."""
+ items = [
+ {
+ "role": "user",
+ "content": [{"type": "input_text", "text": "What is 2+2?"}],
+ }
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ assert messages[0]["role"] == "user"
+ assert messages[0]["content"] == "What is 2+2?"
+
+ def test_output_text_assistant(self):
+ """output_text content parts from assistant messages are extracted."""
+ items = [
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "output_text", "text": "The answer is 4."}],
+ }
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ assert messages[0]["role"] == "assistant"
+ assert messages[0]["content"] == "The answer is 4."
+
+ def test_function_call_to_tool_calls(self):
+ """Single function_call item becomes assistant message with tool_calls."""
+ items = [
+ {
+ "type": "function_call",
+ "call_id": "call_abc",
+ "name": "get_weather",
+ "arguments": '{"city": "Paris"}',
+ }
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ msg = messages[0]
+ assert msg["role"] == "assistant"
+ assert msg["content"] is None
+ assert len(msg["tool_calls"]) == 1
+ tc = msg["tool_calls"][0]
+ assert tc["id"] == "call_abc"
+ assert tc["function"]["name"] == "get_weather"
+ assert tc["function"]["arguments"] == '{"city": "Paris"}'
+
+ def test_consecutive_function_calls_merge(self):
+ """Consecutive function_call items merge into one assistant message."""
+ items = [
+ {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "search",
+ "arguments": '{"q": "foo"}',
+ },
+ {
+ "type": "function_call",
+ "call_id": "call_2",
+ "name": "read_file",
+ "arguments": '{"path": "/tmp/x"}',
+ },
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ assert len(messages[0]["tool_calls"]) == 2
+ assert messages[0]["tool_calls"][0]["id"] == "call_1"
+ assert messages[0]["tool_calls"][1]["id"] == "call_2"
+
+ def test_function_call_output_to_tool_role(self):
+ """function_call_output becomes role=tool message."""
+ items = [
+ {
+ "type": "function_call_output",
+ "call_id": "call_abc",
+ "output": '{"temp": 22, "unit": "C"}',
+ }
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 1
+ assert messages[0]["role"] == "tool"
+ assert messages[0]["tool_call_id"] == "call_abc"
+ assert messages[0]["content"] == '{"temp": 22, "unit": "C"}'
+
+ def test_reasoning_preserved(self):
+ """Reasoning items go to preserved_indices, not messages."""
+ items = [
+ {"role": "user", "content": "Think hard."},
+ {
+ "type": "reasoning",
+ "id": "rs_1",
+ "summary": [{"type": "summary_text", "text": "Thinking..."}],
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "output_text", "text": "Done."}],
+ },
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 2 # user + assistant (reasoning skipped)
+ assert 1 in preserved # index 1 is the reasoning item
+
+ def test_image_content_preserved(self):
+ """Items with input_image content are preserved, not converted."""
+ items = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "input_text", "text": "Describe this image."},
+ {"type": "input_image", "image_url": "data:image/png;base64,abc"},
+ ],
+ }
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 0 # skipped (has non-text)
+ assert 0 in preserved
+
+ def test_developer_maps_to_system(self):
+ """developer role maps to system."""
+ items = [{"role": "developer", "content": "You are helpful."}]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert messages[0]["role"] == "system"
+ assert messages[0]["content"] == "You are helpful."
+
+ def test_system_passthrough(self):
+ """system role passes through as-is."""
+ items = [{"role": "system", "content": "Be concise."}]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert messages[0]["role"] == "system"
+ assert messages[0]["content"] == "Be concise."
+
+ def test_message_type_item(self):
+ """Item with explicit type='message' is handled."""
+ items = [
+ {"type": "message", "role": "user", "content": "Hi"},
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert messages[0] == {"role": "user", "content": "Hi"}
+
+ def test_empty_input(self):
+ """Empty items list produces empty messages."""
+ messages, preserved = responses_items_to_messages([])
+ assert messages == []
+ assert preserved == []
+
+ def test_unknown_type_preserved(self):
+ """Unknown item types are preserved, not converted."""
+ items = [{"type": "some_future_type", "data": "something"}]
+ messages, preserved = responses_items_to_messages(items)
+
+ assert len(messages) == 0
+ assert 0 in preserved
+
+ def test_function_call_flush_on_non_function_call(self):
+ """Pending function_calls flush when a non-function_call item arrives."""
+ items = [
+ {"type": "function_call", "call_id": "c1", "name": "f1", "arguments": "{}"},
+ {"type": "function_call_output", "call_id": "c1", "output": "result"},
+ ]
+ messages, preserved = responses_items_to_messages(items)
+
+ # Should be: assistant (tool_calls), tool (output)
+ assert len(messages) == 2
+ assert messages[0]["role"] == "assistant"
+ assert messages[0]["tool_calls"][0]["id"] == "c1"
+ assert messages[1]["role"] == "tool"
+
+
+# =============================================================================
+# Reverse conversion: messages_to_responses_items
+# =============================================================================
+
+
+class TestMessagesToItems:
+ """Test converting compressed messages back to Responses API items."""
+
+ def test_round_trip_simple(self):
+ """Simple user/assistant conversation round-trips."""
+ original = [
+ {"role": "user", "content": "Hello"},
+ {"type": "message", "role": "assistant", "content": "Hi!"},
+ ]
+ messages, preserved = responses_items_to_messages(original)
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert len(result) == 2
+ assert result[0]["content"] == "Hello"
+ assert result[1]["content"] == "Hi!"
+
+ def test_round_trip_with_tools(self):
+ """Full tool call flow round-trips correctly."""
+ original = [
+ {"role": "user", "content": "Weather in Paris?"},
+ {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "get_weather",
+ "arguments": '{"city": "Paris"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_1",
+ "output": "Sunny, 22C",
+ },
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "output_text", "text": "It's sunny."}],
+ },
+ ]
+ messages, preserved = responses_items_to_messages(original)
+
+ assert len(messages) == 4 # user, assistant(tool_calls), tool, assistant
+
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert len(result) == 4
+ # function_call passes through unmodified
+ assert result[1]["type"] == "function_call"
+ assert result[1]["call_id"] == "call_1"
+ # function_call_output has original content (no compression happened)
+ assert result[2]["type"] == "function_call_output"
+ assert result[2]["output"] == "Sunny, 22C"
+
+ def test_round_trip_compressed_output(self):
+ """Simulated compression: shortened tool output appears in result."""
+ original = [
+ {"role": "user", "content": "Get data"},
+ {
+ "type": "function_call",
+ "call_id": "call_1",
+ "name": "search",
+ "arguments": "{}",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_1",
+ "output": json.dumps([{"id": i, "name": f"item_{i}"} for i in range(100)]),
+ },
+ ]
+ messages, preserved = responses_items_to_messages(original)
+
+ # Simulate compression: replace tool output with shorter version
+ for msg in messages:
+ if msg.get("role") == "tool":
+ msg["content"] = "[100 items, first: item_0, last: item_99]"
+
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert result[2]["type"] == "function_call_output"
+ assert result[2]["output"] == "[100 items, first: item_0, last: item_99]"
+
+ def test_round_trip_with_reasoning(self):
+ """Reasoning items survive round-trip exactly."""
+ reasoning_item = {
+ "type": "reasoning",
+ "id": "rs_abc",
+ "summary": [{"type": "summary_text", "text": "Let me think..."}],
+ }
+ original = [
+ {"role": "user", "content": "Complex question"},
+ reasoning_item,
+ {"type": "message", "role": "assistant", "content": "Answer."},
+ ]
+ messages, preserved = responses_items_to_messages(original)
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert len(result) == 3
+ assert result[1] == reasoning_item # Exact match
+ assert result[1]["type"] == "reasoning"
+
+ def test_round_trip_content_array_preserved(self):
+ """Content array structure (input_text) is preserved through round-trip."""
+ original = [
+ {
+ "role": "user",
+ "content": [{"type": "input_text", "text": "Original question"}],
+ },
+ ]
+ messages, preserved = responses_items_to_messages(original)
+
+ # Simulate compression changing the text
+ messages[0]["content"] = "Compressed question"
+
+ result = messages_to_responses_items(messages, original, preserved)
+
+ # Should reconstruct the array structure
+ assert isinstance(result[0]["content"], list)
+ assert result[0]["content"][0]["type"] == "input_text"
+ assert result[0]["content"][0]["text"] == "Compressed question"
+
+ def test_mixed_ordering(self):
+ """Complex sequence maintains correct ordering."""
+ original = [
+ {"role": "user", "content": "Do two things"},
+ {
+ "type": "function_call",
+ "call_id": "c1",
+ "name": "task_a",
+ "arguments": "{}",
+ },
+ {
+ "type": "function_call",
+ "call_id": "c2",
+ "name": "task_b",
+ "arguments": "{}",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "c1",
+ "output": "Result A",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "c2",
+ "output": "Result B",
+ },
+ {
+ "type": "reasoning",
+ "id": "rs_1",
+ "summary": [{"type": "summary_text", "text": "Thinking..."}],
+ },
+ {"type": "message", "role": "assistant", "content": "All done."},
+ ]
+ messages, preserved = responses_items_to_messages(original)
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert len(result) == 7
+ assert result[0]["role"] == "user"
+ assert result[1]["type"] == "function_call"
+ assert result[1]["call_id"] == "c1"
+ assert result[2]["type"] == "function_call"
+ assert result[2]["call_id"] == "c2"
+ assert result[3]["type"] == "function_call_output"
+ assert result[3]["call_id"] == "c1"
+ assert result[4]["type"] == "function_call_output"
+ assert result[4]["call_id"] == "c2"
+ assert result[5]["type"] == "reasoning"
+ assert result[6]["content"] == "All done."
+
+ def test_image_preserved_in_round_trip(self):
+ """Image items survive round-trip at their original position."""
+ image_item = {
+ "role": "user",
+ "content": [
+ {"type": "input_text", "text": "Describe this"},
+ {"type": "input_image", "image_url": "https://example.com/img.png"},
+ ],
+ }
+ original = [
+ {"role": "user", "content": "Hi"},
+ image_item,
+ {"role": "user", "content": "Also tell me about this"},
+ ]
+ messages, preserved = responses_items_to_messages(original)
+ result = messages_to_responses_items(messages, original, preserved)
+
+ assert len(result) == 3
+ assert result[0]["content"] == "Hi"
+ assert result[1] == image_item # Preserved exactly
+ assert result[2]["content"] == "Also tell me about this"