Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions miles/rollout/session/session_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import setproctitle
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.responses import Response

from miles.rollout.session.sessions import get_worker_stats, setup_session_routes
Expand Down Expand Up @@ -95,25 +94,21 @@ async def do_proxy(
def build_proxy_response(self, result: dict) -> Response:
content = result["response_body"]
status_code = result["status_code"]
# Strip framing headers so JSONResponse / Response recompute them
# from the actual rendered body. Forwarding upstream's content-length
# verbatim breaks uvicorn h11 with "Too much data for declared
# Content-Length" whenever our re-serialization differs in even one
# byte. Mirrors the strip already done on the request path in do_proxy.
# Also strip "server": uvicorn adds its own Server header; passing
# the upstream one through produces two Server headers, which strict
# HTTP parsers (aiohttp/llhttp via litellm) reject as malformed.

headers = {
k: v
for k, v in result["headers"].items()
if k.lower() not in ("content-length", "transfer-encoding", "server")
}
content_type = headers.get("content-type", "")
try:
data = orjson.loads(content)
return JSONResponse(content=data, status_code=status_code, headers=headers)
except (orjson.JSONDecodeError, UnicodeDecodeError):
return Response(content=content, status_code=status_code, headers=headers, media_type=content_type)

content_type = headers.get("content-type")

return Response(
content=content,
status_code=status_code,
headers=headers,
media_type=content_type,
)


async def _stats_logger_loop(worker_port, interval_seconds: float = 30.0):
Expand Down
2 changes: 1 addition & 1 deletion miles/rollout/session/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async def chat_completions(request: Request, session_id: str):
else:
return backend.build_proxy_response(result)

response = orjson.loads(result["response_body"])
response = await asyncio.to_thread(orjson.loads, result["response_body"])

choice = response.get("choices", [{}])[0]

Expand Down
Loading