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.

fix(proxy/streaming): preserve non-standard content-block fields on SSE reconstruction (#2271)

Abhay Singh6decbd1e6e67
diff --git a/headroom/proxy/handlers/streaming.py b/headroom/proxy/handlers/streaming.py
index 53a241e93..da4bdb521 100644
--- a/headroom/proxy/handlers/streaming.py
+++ b/headroom/proxy/handlers/streaming.py
@@ -443,6 +443,15 @@ class StreamingMixin:
# signature validation to pass.
if "data" in block:
current_block["data"] = block["data"]
+ elif btype:
+ # Non-standard block (server_tool_use, web_search_tool_result,
+ # ...): copy through all of the block's fields so the
+ # reconstruction doesn't silently drop them to a bare
+ # {type, index}. Mirrors the sibling reconstructor
+ # `_reconstruct_anthropic_response`, which does `dict(block)`.
+ for _k, _v in block.items():
+ if _k != "type":
+ current_block[_k] = _v
blocks_by_index[block_index] = current_block
elif event_type == "content_block_delta":
diff --git a/tests/test_sse_thinking_blocks.py b/tests/test_sse_thinking_blocks.py
index 1efdaf579..376bbe1dc 100644
--- a/tests/test_sse_thinking_blocks.py
+++ b/tests/test_sse_thinking_blocks.py
@@ -79,6 +79,36 @@ def test_thinking_delta_accumulated() -> None:
assert block["thinking"] == "Let me consider the question carefully."
+def test_non_standard_block_fields_preserved() -> None:
+ # A block whose type isn't text/tool_use/thinking/redacted_thinking (e.g.
+ # server_tool_use, web_search_tool_result) must keep its fields on
+ # reconstruction, not collapse to a bare {type, index}. The sibling
+ # _reconstruct_anthropic_response already does this via dict(block).
+ parser = _Parser()
+ events = [
+ {"type": "message_start", "message": {"id": "msg_1", "model": "claude-opus-4"}},
+ {
+ "type": "content_block_start",
+ "index": 0,
+ "content_block": {
+ "type": "server_tool_use",
+ "id": "srvtoolu_1",
+ "name": "web_search",
+ "input": {"query": "headroom proxy"},
+ },
+ },
+ {"type": "content_block_stop", "index": 0},
+ ]
+ sse = _build_sse(events)
+ response = parser._parse_sse_to_response(sse, "anthropic")
+ assert response is not None
+ block = response["content"][0]
+ assert block["type"] == "server_tool_use"
+ assert block["id"] == "srvtoolu_1"
+ assert block["name"] == "web_search"
+ assert block["input"] == {"query": "headroom proxy"}
+
+
def test_signature_delta_preserved() -> None:
parser = _Parser()
events = [