Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(api): preserve nullable stream compatibility
Xubin Renb6b7caafd335
diff --git a/nanobot/api/server.py b/nanobot/api/server.pyindex 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.Streturn _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 Truerequested_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.pyindex 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."""[email protected]("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()[email protected](not HAS_AIOHTTP, reason="aiohttp not installed")+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.asyncioasync def test_stream_sse_chunk_ids_are_consistent(aiohttp_client) -> None: