Skip to content

Your lightweight open-source personal agent.

fix(api): require boolean stream values

FanouZeng-TT12c2c95cbb68
diff --git a/nanobot/api/server.py b/nanobot/api/server.py
index 9743fbd1e..c22233107 100644
--- a/nanobot/api/server.py
+++ b/nanobot/api/server.py
@@ -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")
diff --git a/tests/test_api_stream.py b/tests/test_api_stream.py
index a555e5dde..9646722ea 100644
--- a/tests/test_api_stream.py
+++ b/tests/test_api_stream.py
@@ -171,6 +171,29 @@ async def test_stream_default_is_false(aiohttp_client) -> None:
assert body["object"] == "chat.completion"
[email protected](not HAS_AIOHTTP, reason="aiohttp not installed")
+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: