From b6b7caafd3351a5f2ee2442c1631ba6c40866331 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Wed, 16 Sep 2026 23:30:02 +0800 Subject: [PATCH] fix(api): preserve nullable stream compatibility --- nanobot/api/server.py | 4 ++-- tests/test_api_stream.py | 28 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/nanobot/api/server.py b/nanobot/api/server.py index c22233107..ac7709572 100644 --- a/nanobot/api/server.py +++ b/nanobot/api/server.py @@ -316,9 +316,9 @@ async def handle_chat_completions(request: web.Request) -> web.Response | web.St return _error_json(400, "Invalid JSON body") body = cast(dict[str, Any], body) stream_value = body.get("stream", False) - if not isinstance(stream_value, bool): + if stream_value is not None and not isinstance(stream_value, bool): return _error_json(400, "stream must be a boolean") - stream = stream_value + stream = stream_value is True 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 9646722ea..a3d77d32d 100644 --- a/tests/test_api_stream.py +++ b/tests/test_api_stream.py @@ -173,8 +173,9 @@ async def test_stream_default_is_false(aiohttp_client) -> None: @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.""" +@pytest.mark.parametrize("stream", ["false", "true", 0, 1, [], {}]) +async def test_stream_rejects_non_boolean_values(aiohttp_client, stream) -> None: + """Wrong JSON types must not select a response protocol or invoke the agent.""" agent = MagicMock() agent.process_direct = AsyncMock(return_value="normal reply") agent.aclose = AsyncMock() @@ -185,7 +186,7 @@ async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None: resp = await client.post( "/v1/chat/completions", headers=AUTH_HEADERS, - json={"messages": [{"role": "user", "content": "hi"}], "stream": "false"}, + json={"messages": [{"role": "user", "content": "hi"}], "stream": stream}, ) assert resp.status == 400 @@ -194,6 +195,27 @@ async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None: agent.process_direct.assert_not_called() +@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed") +@pytest.mark.asyncio +async def test_stream_null_preserves_non_streaming_compatibility(aiohttp_client) -> None: + """OpenAI clients may explicitly serialize the optional flag as null.""" + agent = MagicMock() + agent.process_direct = AsyncMock(return_value="normal reply") + agent.aclose = AsyncMock() + client = await aiohttp_client(create_app(agent, model_name="m", api_key=API_KEY)) + + resp = await client.post( + "/v1/chat/completions", + headers=AUTH_HEADERS, + json={"messages": [{"role": "user", "content": "hi"}], "stream": None}, + ) + + assert resp.status == 200 + assert resp.content_type == "application/json" + assert (await resp.json())["choices"][0]["message"]["content"] == "normal reply" + agent.process_direct.assert_awaited_once() + + @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: