fix(api): require boolean stream values

Validate the OpenAI-compatible stream flag before choosing the response mode so string values such as "false" are rejected instead of enabling SSE.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
FanouZeng-TTandClaude Code authored and Xubin Ren committed 2026-09-17 21:20:58 +08:00
1 parent 122a0f5d6f
commit 12c2c95cbb
2 files changed
+27 -1

No files matched your search

+4 -1
View File
@@ -315,7 +315,10 @@ async def handle_chat_completions(request: web.Request) -> web.Response | web.St
if not isinstance(body, dict):
return _error_json(400, "Invalid JSON body")
body = cast(dict[str, Any], body)
stream = body.get("stream", False)
stream_value = body.get("stream", False)
if not isinstance(stream_value, bool):
return _error_json(400, "stream must be a boolean")
stream = stream_value
requested_model = body.get("model")
text, media_paths = _parse_json_content(body)
session_id = body.get("session_id")
+23
View File
@@ -171,6 +171,29 @@ async def test_stream_default_is_false(aiohttp_client) -> None:
assert body["object"] == "chat.completion"
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
@pytest.mark.asyncio
async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None:
"""The OpenAI-compatible stream flag must be a JSON boolean."""
agent = MagicMock()
agent.process_direct = AsyncMock(return_value="normal reply")
agent.aclose = AsyncMock()
app = create_app(agent, model_name="m", api_key=API_KEY)
client = await aiohttp_client(app)
resp = await client.post(
"/v1/chat/completions",
headers=AUTH_HEADERS,
json={"messages": [{"role": "user", "content": "hi"}], "stream": "false"},
)
assert resp.status == 400
body = await resp.json()
assert body["error"]["message"] == "stream must be a boolean"
agent.process_direct.assert_not_called()
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
@pytest.mark.asyncio
async def test_stream_sse_chunk_ids_are_consistent(aiohttp_client) -> None: