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): import Response at runtime in the Bedrock handler (#4397)

HetCreep51e4ae36a575
diff --git a/headroom/proxy/handlers/bedrock.py b/headroom/proxy/handlers/bedrock.py
index 24c5d510a..6933e605c 100644
--- a/headroom/proxy/handlers/bedrock.py
+++ b/headroom/proxy/handlers/bedrock.py
@@ -70,7 +70,7 @@ class BedrockHandlerMixin:
URL path (may contain ``.``, ``:`` and ``/``).
stream: ``True`` for ``invoke-with-response-stream``.
"""
- from fastapi.responses import JSONResponse
+ from fastapi.responses import JSONResponse, Response
from headroom.proxy.auth_mode import classify_client
from headroom.proxy.helpers import (
diff --git a/tests/test_proxy/test_bedrock_passthrough.py b/tests/test_proxy/test_bedrock_passthrough.py
index 10f23205c..18364030b 100644
--- a/tests/test_proxy/test_bedrock_passthrough.py
+++ b/tests/test_proxy/test_bedrock_passthrough.py
@@ -352,6 +352,30 @@ def test_oversized_body_returns_413_and_never_forwards(monkeypatch):
http.send.assert_not_called()
+def test_client_disconnect_during_body_read_returns_204(monkeypatch):
+ """A client that hangs up mid-body must not crash the handler: the
+ ClientDisconnect branch returns an empty 204 and never forwards.
+ """
+ from starlette.requests import ClientDisconnect
+
+ import headroom.proxy.helpers as helpers_mod
+
+ async def _disconnect(request):
+ raise ClientDisconnect()
+
+ monkeypatch.setattr(helpers_mod, "read_request_json_with_bytes", _disconnect)
+
+ app = create_app(_make_config())
+ with TestClient(app) as client:
+ proxy = client.app.state.proxy
+ http = _install_fake_client(proxy, _FakeUpstream())
+ resp = client.post(INVOKE, json={"messages": [], "max_tokens": 8})
+
+ assert resp.status_code == 204
+ http.build_request.assert_not_called()
+ http.send.assert_not_called()
+
+
@pytest.mark.parametrize(
"route", [INVOKE, INVOKE_STREAM], ids=["invoke", "invoke-with-response-stream"]
)